0

I wrote a post before with a much more rudimentary version of the following code.

I rearranged it, but it still doesn't work. Whenever I input a new string, it doesn't go in either of the two lists. It gives me this :

Here are your strings in ascending order : [ ]

Here are your strings in descending order : [ ]

public class Stringseries {

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    System.out.println("Start the sequence by inputting a string DIFFERENT than 'quit'. When you DO want to end it, input 'quit'");
    String encore = scanner.nextLine(); 

    List<String> ascending = new ArrayList<>();
    List<String> descending = new ArrayList<>();

    int loop = 0;

    String longest = "";
    String lastInput = "";

    boolean inserted = false;

    while (!encore.equalsIgnoreCase("quit")) {

        loop = ++loop;

        encore = encore.replaceAll("\\s+",""); // this way, the length of the strings is only defined by the characters in the string, and not characters + whitespaces.

        for(int i = 0; i < ascending.size(); i++) {
            if(ascending.get(i).length() > encore.length()) {
                ascending.add(i, encore);
                inserted = true;
            } if(!inserted) { 
            ascending.add(encore); }
        } for(int i = 0; i > descending.size(); i++) {              
            if(descending.get(i).length() < encore.length()) {
                descending.add(i, encore);
                inserted = true;
            } if(!inserted) { 
            descending.add(0, encore); }
                }

        if (longest.length() < encore.length()) {
            longest = encore; }

        System.out.println("Enter the string you want to put in your sequence of strings");

        encore = scanner.nextLine();
        }

    if (descending != null) { // we check to see if the "descending" string is empty (we could do this with "ascending" mind you).
        System.out.println("Here are your strings in ascending order : " + ascending);
        System.out.println("Here are your strings in descending order : " + descending);
        System.out.println("Here is the longest string : " + longest);
    } else if (descending == null) { 
        System.out.println("You have not entered any strings, therefore the program doesn't display any string :("); // customised message.
        }
    }
}
2
  • You do not need the null check for 'descending' as you initialized it with 'new ArrayList' Commented Oct 12, 2017 at 14:50
  • 2
    How about you don't save descending and just simply print out the collection in reverse order? Commented Oct 12, 2017 at 14:56

2 Answers 2

1

I would suggest you to sort the list using Collections.sort(); and Collections.reverse(); Also, you don't need the else if (descending == null) since you already initialized descending. Your code will look something like,

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Test2 {
 public static void main(String[] args) {

  Scanner scanner = new Scanner(System.in);
  System.out.println("Start the sequence by inputting a string DIFFERENT than 'quit'. When you DO want to end it, input 'quit'");
  String longest = "";

  List<String> ascending = new ArrayList<String>();
  List<String> descending = new ArrayList<String>();
  int loop = 0;
  Comparator<String> comparator = new Comparator<String>() {
   public int compare(String o1, String o2) {
    return o1.length() - o2.length();
   }
  }


  String encore = "";
  while(true){
   loop++;
   System.out.println("Enter the string you want to put in your sequence of strings");
   encore = scanner.nextLine();
   if (encore.equalsIgnoreCase("quit")) {
    break;
   }

   encore = encore.replaceAll("\\s+", ""); // this way, the length of the strings is only defined by the characters in the string, and not characters + whitespaces

   ascending.add(encore);
   descending.add(encore);
   Collections.sort(ascending, comparator);
   Collections.sort(descending, comparator);
   Collections.reverse(descending);
  }

  for (String str: ascending) {
   if (str.length() > longest.length()) {
    longest = str;
   }
  }

  if (ascending.size() > 0) {
   System.out.println("Here are your strings in ascending order : " + ascending);
   System.out.println("Here are your strings in descending order : " + descending);
   System.out.println("Here is the longest string : " + longest);
  } else {
   System.out.println("You have not entered any strings, therefore the program doesn't display any string :("); // customised message.
  }

  scanner.close();
 }
}

However I would use only one list instead of 2, Since they both have same elements. Like,

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Test2 {
 public static void main(String[] args) {

  Scanner scanner = new Scanner(System.in);
  System.out.println("Start the sequence by inputting a string DIFFERENT than 'quit'. When you DO want to end it, input 'quit'");
  String longest = "";

  List < String > list = new ArrayList < > ();
  int loop = 0;

  String encore = "";
  while(true){
   loop++;
   System.out.println("Enter the string you want to put in your sequence of strings");
   encore = scanner.nextLine();
   encore = encore.replaceAll("\\s+", ""); // this way, the length of the strings is only defined by the characters in the string, and not characters + whitespaces

   if (encore.equalsIgnoreCase("quit")) {
    break;
   }
   list.add(encore);
  }

  for (String str: list) {
   if (str.length() > longest.length()) {
    longest = str;
   }
  }

  if (list.size() > 0) {
   Collections.sort(list, new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
     return o1.length() - o2.length();
    }
   });
   System.out.println("Here are your strings in ascending order : " + list);
   Collections.reverse(list);
   System.out.println("Here are your strings in descending order : " + list);
   System.out.println("Here is the longest string : " + longest);
  } else {
   System.out.println("You have not entered any strings, therefore the program doesn't display any string :("); // customised message.
  }

  scanner.close();
 }
}

Hope it helps!

Thanks to @phflack for pointing out the sort should be on length & not on lexical order.

Sign up to request clarification or add additional context in comments.

3 Comments

In your first example you're constantly reversing descending without ever sorting it, you may also want to move the sort and reverse to outside the loop. You also need to implement a comparator to sort by length instead of lexicographical
@phflack I did that in my second example (marked as optimzed).
@phflack I think I messed when I added compartor. Sorry :(
1

Your code is almost correct To implement the insertion sort, you just need to move your if statement out of your loop, and to reset your inserted variable

inserted = false;
for(int i = 0; i < ascending.size(); i++)
    if(ascending.get(i).length() > encore.length())
    {
        ascending.add(i, encore);
        inserted = true;
        break;
    }
if(!inserted)
    ascending.add(encore);

inserted = false;
for(int i = 0; i > descending.size(); i++)
    if(descending.get(i).length() < encore.length())
    {
        descending.add(i, encore);
        inserted = true;
        break;
    }
if(!inserted)
    descending.add(0, encore);

Other things of note with your code:

  • loop = ++loop; is normally written as loop++; instead
  • if(descending != null) will never be false, you're setting it to something with List<String> descending = new ArrayList<>(); at the top, instead it looks like you meant to write if(!descending.isEmpty())
  • Writing if(descending != null){ A } else if(descending == null){ B } is the same as (if descending != null){ A } else { B }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.