Alright so my code doesn't work : I'm trying to arrange inputted strings in both a "descending" and an "ascending" but sometimes strings just won't go in the lists (either in the right order or it doesn't go in the descending/ascending strings at all)
import java.util.Scanner;
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();
int loop = 0;
String smallest = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; // we set a "smallest" string to know where to put the new string in the "descending" and "ascending" strings.
String longest = "";
String ascending = "";
String descending = "";
String lastInput = "";
while (!encore.equals("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.
if (loop == 1) {
descending = encore;
ascending = encore;
} if (loop >= 2) {
if (encore.length() < smallest.length()) {
descending = descending + " " + encore;
ascending = encore + " " + ascending;
} if (encore.length() > longest.length()) {
descending = encore + " " + descending;
ascending = ascending + " " + encore;
}
}
if (longest.length() < encore.length()) {
longest = encore;
} if (smallest.length() > encore.length()) {
smallest = encore;
}
System.out.println("Enter the string you want to put in your sequence of strings");
lastInput = encore;
encore = scanner.nextLine();
}
if (descending != null && !descending.isEmpty()) { // 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 | descending == "") {
System.out.println("You have not entered any strings, therefore the program doesn't display any string :("); // customised message.
}
} // end Method
} // end Class