简体   繁体   中英

How to fix the logic in my do-while loop, and then applying the try-catch block to catch and display an error message from another class?

The homework instructs to create a loop requesting for an String input. If the String has less than 20 characters, it displays what was just inputted. If if has more than 20 characters, the catch block will display a message stating that the String has too many characters. The only way to end the program is to input DONE. Otherwise, it continues to ask the user for Strings.

The catch block will display a message from another class.

I've attempted to do both do-while and while loops.

    do
    {
        System.out.println("Enter strings, enter DONE when finished:");
        userInputLength = input.nextLine();
        try {
        while(userInputLength.length() > 20)
        {
            System.out.println("Please try again:");
            userInputLength = input.nextLine();
        }
        }
        catch(StringTooLongException e) //Error here
        {
            //Not sure how to call the super() in StringTooLongException class.
        }
        while(userInputLength.length() <= 20)
        {
            String message = userInputLength;
            System.out.println("You entered: " + message);
            userInputLength = input.nextLine();
        }
        }
    while(userInputLength.toString() == "DONE");
    }
}

public StringTooLongException()
{
    super("String has too many characters!");
}

Before I started getting the error on my catch block after adding both try-catch blocks, I was able to output long strings, then short strings. But if I attempt to write long strings after the short strings, the program ends.

it's gonna work. Look at my code and compare with yours. First: don't compare strings with ==, always choose the equals method. You do not need 3 whiles block, you need only one while and 2 IF'S one for string > 20 and another for string < 20 (look, if the string contains exactly lenght of 20, the program will output nothing) And you need to create your own exception, it is very easy.

import java.util.Scanner;

public class ReadString {

public static void main(String[] args) {

    String userInputLength;
    Scanner input = new Scanner(System.in);

    /*
     * . If the String has less than 20 characters, it displays what was just
     * inputted. If if has more than 20 characters, the catch block will display a
     * message stating that the String has many characters. The only way to end the
     * program is to input DONE. Otherwise, it continues to ask the user for
     * Strings.
     */

    do {
        System.out.println("Enter strings, enter DONE when finished:");
        userInputLength = input.nextLine();
        try {
            if (userInputLength.length() > 20) {
                throw new StringTooLongException("String is too long");
            } else {
                System.out.println(userInputLength);
            }

        } catch (StringTooLongException e) // Error here
        {
            System.out.println(e.getMessage());
        }

    } while (!userInputLength.toString().equals("DONE"));

}

Exception class

public class StringTooLongException extends RuntimeException{

public StringTooLongException(String message) {
    super(message);
 }
}

Try to understand it :D !!!

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