I'm writing a simple dictionary program in Java, and I have a problem, so I'm asking you a question.

Asked 2 years ago, Updated 2 years ago, 24 views

public class Main Window {
    public static void main(String[] args) {
        int selectMenu = 0;
        String inputWord = null;
        String[] wordArray = new String[20];

        WordPlusMinus wordPlusMinus = new WordPlusMinus();
        WordSearch wordSearch = new WordSearch();
        Scanner scanner = new Scanner(System.in);

        /*
         * Words to be entered: thornbush, crushed, fine hawk, gojuri mijuri, straight autumn, straight autumn, straight autumn, thimble, gombangdae, gomsakda, gomsakda, gomsalgut, gomsalgida, weird, military jongipjeong, gisurak, gilajap, gilaseop, napul, napul, stream, stream
         */

        while(true) {
            System.out.println("==============================================");
            System.out.println ("Pre-program".\t\n");
            System.out.println ("0.Word Type".\n1. Word lookup.\n2.Find the meaning of the word.\n3.Add and clear words.\n4.End Program.");
            System.out.print ("Enter the desired menu number: ");

            selectMenu = scanner.nextInt();
            switch(selectMenu) {
                case 0:
                    for(int i = 0; i < wordArray.length; i++) {
                        System.out.print ("Please enter a word: ");
                        inputWord = scanner.nextLine();
                        wordArray[i] = inputWord;
                    }
                    break;
                case 1:
                    for(int i = 0; i < wordArray.length; i++) {
                        System.out.println(i + "First word: " + wordArray[i]);
                    }
                    break;
                case 2:
    //              //              wordSearch.main(null);
                    break;
                case 3:
    //              //              wordPlusMinus.main(null);
                    break;
                case 4:
                    scanner.close();
                    System.exit(0);
                    break;
            }
        }
    }
}

This is how we make a simple dictionary. However, during the word input process, there is a problem with the input as follows, and if you search after entering the word, the value is null in the 0th array index, but I don't know why.

Also, if you use scanner.close() and System.exit(0) outside the while() statement, it says Unreachable, but doesn't it matter if it's in the same main method?

java

2022-09-22 12:52

1 Answers

For nextInt, it does not contain open characters when reading the entered values, unlike nextLine. So if you type 0 and type enter, the opening character is entered and it looks like an empty value.

The simplest way is to add scanner.nextLine() to read the opening character as shown below.

selectMenu = scanner.nextInt();
scanner.nextLine(); //Added
            switch(selectMenu)

The other way is to parse as below.

selectMenu = Integer.parseInt(scanner.nextLine());


2022-09-22 12:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.