3
votes

Imagine the following scanario: I have a program which ask for an integer input, followed by a String input.

int age=0;
String name;
Scanner sc = new Scanner(System.in);

System.out.print("Enter Age: ");
age = sc.nextInt();
System.out.print("Enter Name: ");
name= sc.nextLine();

With the aobe codes, I was not given a chance to enter the name. So normally I will declare 2 scanner objects as follows:

int age=0;
String name;
Scanner sc = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);    //2nd Scanner object

System.out.print("Enter Age: ");
age = sc.nextInt();
System.out.print("Enter Name: ");
name= sc2.nextLine();                    //Using 2nd Scanner Object

My question is: Is it necessary to declare multiple scanner objects to accept inputs of different types?? Am I doing the proper way as aobve?

I have this question in mind for years already. (Several questions in SO mention multiple scanner, but their questions only used one scanner object, so I am asking this today.)

6
reading your coment below, have you tried reading name 1st and after reading age? - Frakcool
@Frakcool If we read string before reading int, it will work. - user3437460

6 Answers

6
votes

@skiwi is right about only using one Scanner, so you're doing that right. The reason it doesn't work is that nextInt() consumes all characters that make up the integer, but it does not touch the end-of-line character. So when nextLine() is called, it sees that there are no characters before the end-of-line character, so it thinks that an empty line was entered, and you get an empty String back. However, nextLine() does consume the end-of-line character, so if you call sc.nextLine(); once before you do name = sc.nextLine();, it should work.

3
votes

You were not given a chance to enter the name because nextInt() doesn't read the new-line character '\n' (inputted by user after pressing Enter), whereas nextLine() does. So as soon as you call name = sc.nextLine();, it will just read the '\n' character that the nextInt() didn't read already.

Definitely do not create a new Scanner if the Scanner if you're scanning the same thing (like System.in) - only change Scanners if you are scanning something else, like different files or something.

To get your code working (with only one Scanner instance), use this:

int age = 0;
String name;
Scanner sc = new Scanner(System.in);

System.out.print("Enter Age: ");
age = sc.nextInt();
System.out.print("Enter Name: ");

sc.nextLine(); // "dispose" of the '\n' character
               // so that it is not recorded by the next line

name = sc.nextLine();

// print your findings
System.out.println("------\nAge: " + age + "\nName: " + name);

Example input/output:

Enter Age: 17
Enter Name: Michael
------
Age: 17
Name: Michael
2
votes

You should use only one Scanner instance per object that you are scanning. In this case you are reading from the System.in, so opening two scanners on the same them concurrently does not even make sense.

So you definately want to go with your first option, then the question comes, what is wrong with it:

Well, you ask for sc.nextInt(), an integer, and a name rarely is an integer. You are most likely looking for either name = sc.next() for one word or for name = sc.nextLine() for a whole sentence (until the enter key has been pressed).

Also be aware that after sc.nextInt(), actually after any sc.next***(), you need to press Enter.

0
votes

You can also use:

name = sc.next();
0
votes

This must work perfectly. I tested it out.

int age=0;
String name;
Scanner sc = new Scanner(System.in);

System.out.print("Enter Age: ");
age = sc.nextInt();
System.out.print("Enter Name: ");
name= sc.nextLine();
0
votes

why this code has not work?

package Programs;
import java.util.Scanner;
public class LibraryManagement {
    Scanner s = new Scanner(System.in);
    String Sname, Bname, Bauthor;
    int Sid,Sclass,Bid;
    void StudInfo()
    {
    
    System.out.print("Enter the Student Id: ");
    Sid=s.nextInt();
    
    System.out.print("Enter the Student name: ");
    Sname=s.nextLine();
    
    System.out.print("Enter the Student Class: ");
    Sclass=s.nextInt();
    
    System.out.println("Student detail is saved:  \n Id : "+ Sid + " Name : "+ Sname+ " Class : ");
    
    }
    public static void main(String[] args) {
          LibraryManagement obj= new LibraryManagement();
          obj.StudInfo();
        }
    
    }

output is :
Enter the Student Id: 10
Enter the Student name: Enter the Student Class: