Question

I've been working on a programming assignment that acts as a Scrabble dictionary for a while now. The program takes input from the user and outputs a file with a list of words, depending on what the user requests from a menu. The problem I've been having has to do with Scanner.nextLine().

I'm not aexactly sure why, but for some reason I have to press enter once sometimes before my code will take my input and store it as the variable. Essentially, I end up entering the input twice. I tried inserting Scanner.nextLine() around the code to "take up" the empty enter/spaces but it doesnt work, and I have to press enter multiple times to get it to process what I want.

Does anybody have any suggestions? I'd appreciate any and all help.

Here is a bit of the code:

System.out.println("Enter the length of the word you are" + " searching for.");
int n = -1;
while(!(n >=0)) {
    if(in.hasNextInt())
        n = in.nextInt();
    else {
        System.out.println("You have not entered a valid number. 
                            Please enter a real number this  time.");
        in.nextLine();
    }   
}
in.nextLine();
System.out.println("Enter the first letter of the words" + " you are searching for.");
String firstLetter = "";
while(!(firstLetter.length() == 1)) {
    if(in.nextLine().length() > 1) {
        System.out.println("You have not entered a valid letter. 
                            Please press enter and enter only one real letter.");
    } 
    else if(in.hasNextInt()) {
        System.out.println("Do not enter a number. Please enter one real letter.");
    }
    else {
        in.nextLine();
        firstLetter = in.nextLine();
        break;
    }
}

At the end of this, I have to press enter once and then input to get it to store anything in the variable firstLetter. I assume it has something to do with the nature of nextLine(), as the conditions using nextInt() give no issues.

Was it helpful?

Solution 2

I think you're overcompensating with too many nextLines. You may want to do that once to clear the line after the int is inputted, for example, to clear the newline, but the second time here just absorbs an extra line of input:

System.out.println("You have not entered a valid number. Please enter a real number this time.");
            in.nextLine();//first time
            }   
        }
        in.nextLine();//this second time is unnecessary.

The same thing happens with your duplicate uses here:

in.nextLine();
firstLetter = in.nextLine();
break;

You should only add an extra in.nextLine() immediately between inputting nextSOMETHINGELSE() and another nextLine().

EDIT:

Additionally, note that whenever you call in.nextLine(), you are absorbing a line of input. For example, this line should be fixed:

        if(in.nextLine().length() > 1){

because it reads in a line, using it up, and then checks whether that (now used-up) line is long enough.

OTHER TIPS

It's because you're using both nextLine() and nextInt(), what's going on is that nextLine() is searching for a new line (enter) and nextInt will automatically stop the search if any integer is typed through System.in.

Rule of thumb: Just use Scanner.nextLine() for your input, then convert your string from Scanner.nextLine() accordingly through Integer.parseInt(string), etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top