1

I'm a beginner in Java and I just wrote a program to get the user's name, phone number and address. The problem is after reading the phone number the program will not proceed to read the address, it's like if it's skipping it. Here is my code:

public static void main(String [] arge){
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter your name, phone number and address");
    String name = sc.nextLine();
    long phone = sc.nextLong();

    String address = sc.nextLine();

    System.out.println("\n *** Here is your info ***");
    System.out.println("Name: "+name+"\nPhone number: "+phone+"\n Address: "+address);
}
sashkello
  • 15,161
  • 19
  • 74
  • 101
Maher Nabil
  • 1,307
  • 1
  • 12
  • 18
  • 3
    possible duplicate of [Skipping nextLine() after use nextInt()](http://stackoverflow.com/questions/13102045/skipping-nextline-after-use-nextint) – Rohit Jain Oct 02 '13 at 07:30
  • possible duplicate of [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx) – Dennis Meng Dec 02 '13 at 22:51

5 Answers5

3
long phone = sc.nextLong();

change this to

long phone = Long.parseLong(sc.nextLine());

Because after giving the phone number, the enter you hit is being consumed as the nextLine which is set to your address. Thus, the blank address(in the sense, the program doesn't prompt to you enter the address).

Another way to make your code work(without changing anything, YES without any changes!) is to provide your phone number and address in the same line as input. Scanner will take the space as the default delimiter and do the job for you. This is because nextLong() will only scan for a long value.

RainMaker
  • 41,717
  • 11
  • 78
  • 97
0

Your program will read all the three inputs. But i believe you are hitting enter key after doing the input for phone number. Try doing the input as mentioned here:

myName

100000 myAddress

sc.nextLong() method will accept the long value and then sc.nextLine() will wait for input on the same line. And if you press enter after entering the long value, sc.nextLine() will simply read empty.

Juned Ahsan
  • 63,914
  • 9
  • 87
  • 123
0

Try this:

    String name = sc.nextLine();
    long phone = Long.parseLong(sc.nextLine());
    String address = sc.nextLine();
siledh
  • 3,000
  • 1
  • 14
  • 28
0

try to read values one by one

 Scanner sc = new Scanner(System.in);
    System.out.println("Enter your name");
    String name = sc.nextLine();
    System.out.println("Enter your phone number");
    long phone = sc.nextLong();
         sc.nextLine();//to catch the buffer"ENTER KEY" value
    System.out.println("Enter your  address");
    String address = sc.nextLine();
    System.out.println("\n *** Here is your info ***");
    System.out.println("Name: "+name+"\nPhone number: "+phone+"\n Address: "+address);
KhAn SaAb
  • 5,007
  • 5
  • 26
  • 48
0

Try this.

import java.util.Scanner;

class ScannerTest{

 public static void main(String args[]){

   Scanner sc=new Scanner(System.in);

   System.out.println("Enter your rollno");

   int rollno=sc.nextInt();

   System.out.println("Enter your name");

   String name=sc.next();

   System.out.println("Enter your fee");

   double fee=sc.nextDouble();

   System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee);


 }

} 
Rajan
  • 199
  • 1
  • 5
  • 20