Why can't I enter a string in Scanner(System.in), when calling nextLine()-method?

137,860

Solution 1

.nextInt() gets the next int, but doesn't read the new line character. This means that when you ask it to read the "next line", you read til the end of the new line character from the first time.

You can insert another .nextLine() after you get the int to fix this. Or (I prefer this way), read the int in as a string, and parse it to an int.

Solution 2

This is a common misunderstanding which leads to confusion if you use the same Scanner for nextLine() right after you used nextInt().

You can either fix the cursor jumping to the next Line by yourself or just use a different scanner for your Integers.

OPTION A: use 2 different scanners

import java.util.Scanner;

class string
{

    public static void main(String a[]){
    int a;
    String s;
    Scanner intscan =new Scanner(System.in);


    System.out.println("enter a no");
    a=intscan.nextInt();
    System.out.println("no is ="+a);


     Scanner textscan=new Scanner(System.in);
    System.out.println("enter a string");
    s=textscan.nextLine();
    System.out.println("string is="+s);
        }
}

OPTION B: just jump to the next Line

class string
{
    public static void main(String a[]){
        int a;
        String s;
        Scanner scan =new Scanner(System.in);

        System.out.println("enter a no");
        a = scan.nextInt();
        System.out.println("no is ="+a);
        scan.nextLine();

        System.out.println("enter a string");
        s = scan.nextLine();
        System.out.println("string is="+s);
    }
}

Solution 3

This is because after the nextInt() finished it's execution, when the nextLine() method is called, it scans the newline character of which was present after the nextInt(). You can do this in either of the following ways:

  1. You can use another nextLine() method just after the nextInt() to move the scanner past the newline character.
  2. You can use different Scanner objects for scanning the integer and string (You can name them scan1 and scan2).
  3. You can use the next method on the scanner object as

    scan.next();

Solution 4

You only need to use scan.next() to read a String.

Solution 5

Scanner's buffer full when we take a input string through scan.nextLine(); so it skips the input next time . So solution is that we can create a new object of Scanner , the name of the object can be same as previous object......

Share:
137,860
user1646373
Author by

user1646373

Updated on July 09, 2022

Comments

  • user1646373
    user1646373 almost 2 years

    How does this program actually work...?

    import java.util.Scanner;
    
    class string
    {
        public static void main(String a[]){
            int a;
            String s;
            Scanner scan = new Scanner(System.in);
    
            System.out.println("enter a no");
            a = scan.nextInt();
            System.out.println("no is ="+a);
    
            System.out.println("enter a string");
            s = scan.nextLine();
            System.out.println("string is="+s);
        }
    }
    

    The output is:

    enter the no
    1234
    no is 1234
    enter a string
    string is=         //why is it not allowing me to enter a string here?
    
  • NominSim
    NominSim over 11 years
    I would hesitate to call this a "problem", the issue is just what he expects the methods to do, and what they actually do.
  • NominSim
    NominSim over 11 years
    @user1646373 Care to enlighten me as to why? I prefer parsing because it allows you to catch when the user has entered in incorrect text.
  • user1646373
    user1646373 over 11 years
    than tell me use of scan.nextLine();
  • Ami
    Ami over 11 years
    The java.util.Scanner.nextLine() method advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line. Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.example tutorialspoint.com/java/util/scanner_nextline.htm
  • Admin
    Admin over 9 years
    @user1646373 scan.nextLine() reads a sentence until you press Enter! scan.next() reads one word
  • Igbanam
    Igbanam over 8 years
    I get why parsing wouldn't be a good solution. For starters, it's one extra operation. But how can one go around this problem?