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?
Aske B.
  • 6,419
  • 8
  • 35
  • 62
user1646373
  • 149
  • 1
  • 2
  • 6
  • 7
    In java, class names usually begin with an uppercase letter - but I would strongly advise against calling your class "String". – arshajii Sep 04 '12 at 14:08
  • possible duplicate of [Why doesn't this for-loop let me input text the first cycle?](http://stackoverflow.com/questions/1815396/why-doesnt-this-for-loop-let-me-input-text-the-first-cycle) – NominSim Sep 04 '12 at 14:11
  • Also to make this code compile you need to change the parameter of the main method to something other than a. – KennyBartMan Sep 04 '12 at 14:13

13 Answers13

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.

NominSim
  • 8,447
  • 3
  • 28
  • 38
  • @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. – NominSim Sep 13 '12 at 14:16
  • 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? – Igbanam Sep 18 '15 at 15:54
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);
    }
}
Yalla T.
  • 3,707
  • 3
  • 23
  • 36
  • 2
    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 Sep 04 '12 at 14:17
2

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

Marcelo
  • 11,218
  • 1
  • 37
  • 51
  • @user1646373 scan.nextLine() reads a sentence until you press Enter! scan.next() reads one word –  Jan 06 '15 at 21:00
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();

Apurv Pandey
  • 217
  • 2
  • 7
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......

user1646373
  • 149
  • 1
  • 2
  • 6
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.

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
Umair
  • 117
  • 1
  • 2
1

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

Ajay Anto
  • 11
  • 2
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);
Melih Altıntaş
  • 2,495
  • 1
  • 22
  • 35
0

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

Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43
esskay
  • 43
  • 6
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);
    }
}
Nithin Raja
  • 1,144
  • 12
  • 11
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);
    }
}
fonfonx
  • 1,475
  • 21
  • 30
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.

-1
 s=scan.nextLine();

It returns input was skipped.

so you might use

 s=scan.next();
Ami
  • 4,241
  • 6
  • 41
  • 75
  • than tell me use of scan.nextLine(); – user1646373 Sep 13 '12 at 14:17
  • 1
    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 http://www.tutorialspoint.com/java/util/scanner_nextline.htm – Ami Sep 13 '12 at 16:04