简体   繁体   中英

Problem with how to print “ALL” the longest string which have SAME longest length() that stored in array(JAVA)

Write a program where the user types in a number of Strings which are stored in an array of Strings and then the program prints out all the longest Strings entered by the user.

HAVE A PROBLEM HOW TO PRINT ALL SAME LONGEST LENGTH() According to this sentence that required "the program prints out all the longest Strings"

PS. My code can only print out ONE longest length string NOT ALL longest String. How to fixed it.

 public static void method4(){
    Scanner console = new Scanner(System.in);
    String[] list = new String[5];

    int maxLength = 0;
    String longestString = null;
    String longestString1 = null;
    for (int i = 0; i < list.length; i++) {
        System.out.println("Enter a string 5 times: ");
        list[i] = console.next();

        if (list[i].length() > maxLength){
            maxLength = list[i].length();
            longestString = list[i];


        }
    }
    System.out.println("Longest string: "+longestString+ "\n\t\t"+longestString1);

}

The problem with your code is:
inside the loop that you are getting the user's input, you print the "longest string" at that moment.
This means that the 1st string that the user enters will be a "longest string" and will be printed because its length is compared to 0 and of course it is longer.
You must separate the input of the strings and the output (printing of the longest strings) in 2 separate loops.
The 1st loop gets the strings and by comparing calculates the length of the longest string and the 2nd iterates through all the string and prints it only if its length is equal to the maximum length that was calculated in the 1st loop:

String[] list = new String[5];

int maxLength = 0;

Scanner scanner = new Scanner(System.in);

for (int i = 0; i < list.length; i++) {
    System.out.println("(" + (i + 1) + ") Enter a string: ");
    list[i] = scanner.nextLine();

    if (list[i].length() > maxLength){
        maxLength = list[i].length();
    }
}

scanner.close();

int counter = 0;
for (String s : list) {
    if (s.length() == maxLength){
        counter++;
        System.out.println("(" + counter + ") Longest string: " + s + "\n");
    }
}

You have to do it in two pass. First loop finds the max length of string. Then second loop can iterate and print only strings that have max length,

public static void method4(Scanner console){
    String[] list = new String[5];

    int maxLength = 0;
    System.out.println("Enter a string 5 times: ");
    for (int i = 0; i < list.length; i++) {
        list[i] = console.next();

        if (list[i].length() > maxLength){
            maxLength = list[i].length();
        }
    }
    for (int i = 0; i < list.length; i++) {
        if (list[i].length() == maxLength) {
            System.out.println("Longest string: "+list[i]);
        }
    }

}

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    method4(sc);
    sc.close();
}

There are other approaches too where you can store the longest strings in a set as you encounter them and reset the set the moment you find a new longer string and finally print all the strings in the Set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM