Why can't I enter a string in Scanner(System.in), when calling nextLine()-method? [duplicate]
Asked Answered
S

13

10

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?
Sulfa answered 4/9, 2012 at 14:5 Comment(2)
In java, class names usually begin with an uppercase letter - but I would strongly advise against calling your class "String".Kalynkam
Also to make this code compile you need to change the parameter of the main method to something other than a.Denyse
B
20

.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.

Babu answered 4/9, 2012 at 14:9 Comment(2)
@Sulfa Care to enlighten me as to why? I prefer parsing because it allows you to catch when the user has entered in incorrect text.Babu
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?Sympathizer
P
7

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);
    }
}
Protagoras answered 4/9, 2012 at 14:14 Comment(1)
I would hesitate to call this a "problem", the issue is just what he expects the methods to do, and what they actually do.Babu
M
2

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

Merrilee answered 4/9, 2012 at 14:9 Comment(1)
@Sulfa scan.nextLine() reads a sentence until you press Enter! scan.next() reads one wordNegress
C
2

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();

Chervil answered 9/7, 2016 at 15:46 Comment(0)
S
1

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......

Sulfa answered 13/9, 2012 at 14:24 Comment(0)
W
1

Don't try to scan text with nextLine(); AFTER using nextInt() with the same scanner! It doesn't work well with Java Scanner, and many Java developers opt to just use another Scanner for integers. You can call these scanners scan1 and scan2 if you want.

Waverley answered 17/9, 2013 at 9:58 Comment(0)
S
1

use a temporary scan.nextLine(); this will consume the \n character

Selfheal answered 25/4, 2017 at 7:14 Comment(0)
C
0

if you don't want to use parser :

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(); // This line you have to add (It consumes the \n character)
System.out.println("enter a string");
s = scan.nextLine();
System.out.println("string is=" + s);
Concomitant answered 17/9, 2013 at 10:11 Comment(0)
L
0

Incase you don't want to use nextint, you can also use buffered reader, where using inputstream and readline function read the string.

Lemire answered 30/5, 2016 at 11:21 Comment(0)
M
0

Simple solution to consume the \n character:

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);

        scan.nextLine();
        System.out.println("enter a string");
        s = scan.nextLine();
        System.out.println("string is="+s);
    }
}
Mime answered 6/6, 2017 at 2:39 Comment(0)
A
0
import java.util.*;

public class ScannerExample {

    public static void main(String args[]) {
        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.next();
        System.out.println("string is=" + s);
    }
}
Apulia answered 18/7, 2017 at 22:20 Comment(0)
I
0

.nextInt() will not read the 'nextline' till the end if you declare .nextLine() after it. I would recommend try to scan 'String' before 'int'

s = scan.nextLine();
a = scan.nextInt();

In that order.

Introduce answered 9/6, 2021 at 17:41 Comment(0)
P
-1
 s=scan.nextLine();

It returns input was skipped.

so you might use

 s=scan.next();
Parasol answered 4/9, 2012 at 14:17 Comment(2)
than tell me use of scan.nextLine();Sulfa
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.htmParasol

© 2022 - 2024 — McMap. All rights reserved.