Home > Software engineering >  Why does my scanner.next().charat(0) read more than one char when the user inputs something with a s
Why does my scanner.next().charat(0) read more than one char when the user inputs something with a s

Time:11-07

I want my code to read only the first letter of what the user inputs, but for some reason it reads more than that when there is a space in between words. If there is no space it'll work fine by storing only the first char, but if there are 2 words it'll store the first variable, go to the next line of code, then jump back quickly and override that variable with the first char of the second word and so on so forth with multiple words. It doesn't make any sense for it to do that because I thought that space was a char, so I don't understand why it restarts the char count.

Here is the code:

import java.util.Scanner;

public class test1 {

    static Scanner scn = new Scanner(System.in);
    static char yn;

    public static void main(String[] args) {

        System.out.println("Would you like to change anything? (Y/N)");
        yn = scn.next().charAt(0);
        yn = Character.toUpperCase(yn);

        answer();
    }

    static void answer(){
        // Loop until their answer is one of the options
        while (yn != 'Y' && yn != 'N') {
            System.out.println("Please input Y for yes or N for no");
            System.out.println("Would you like to change anything? (Y/N)");
            yn = scn.next().charAt(0);
            yn = Character.toUpperCase(yn);
        }
    }

I tried to create a string variable, then after the user inputs their answer, the string variable would only store the first word then store the first char in the char variable, but that didn't seem to work.

Here is the code that I tried:

import java.util.Scanner;

public class test1 {

    static Scanner scn = new Scanner(System.in);
    static char yn;
    static String yesno;

    public static void main(String[] args) {
        System.out.println("Would you like to change anything? (Y/N)");
        yesno = scn.next();
        yn = yesno.charAt(0);
        yn = Character.toUpperCase(yn);

        answer();
    }

    static void answer(){
        // Loop until their answer is one of the options
        while (yn != 'Y' && yn != 'N') {
            System.out.println("Please input Y for yes or N for no");
            System.out.println("Would you like to change anything? (Y/N)");
            yesno = scn.next();
            yn = yesno.charAt(0);
            yn = Character.toUpperCase(yn);
        }
    }
}

CodePudding user response:

The class java.util.Scanner is used to "parse primitive types and strings". The documentation also states that "A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace."

(see https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Scanner.html)

That means that Scanner does not simply read in characters, but also parses the input by using whitespace to find the next item to parse.

You would have to use Scanner.nextLine() to fetch the whole next line of input instead of just the next token.

CodePudding user response:

I think that it would be better to read userInput using Scanner with nextLine() method, then you could easily get its first character.

Here is the code that is in my mind:

public static void main(String[] args) {
    static Scanner scn = new Scanner(System.in);
    
    System.out.println("Would you like to change anything? (Y/N)");
    String input = scn.nextLine();
    Character character = input.charAt(0);
    answer();
    
}

And just another thing that made me confused was you While() loop condition!! I think that you need to use ||(OR) instead of &&(AND):

while (!character.toString().equalsIgnoreCase("y") ||
!character.toString().equalsIgnoreCase("n")) {
//some logic...
}

hope these help you to solve your problem!

  • Related