Tan Kai Sheng Tan Kai Sheng - 22 days ago 10
Java Question

Same code but different output

First i want user to input somethings(name,contact,idnumber),and i will show 2 different file but code are same.The first code problem is "name" input place disappear,and the second but doesn't disappear.Can anyone tell me the problem?Im new in java.

public class Admin {
static Scanner scan= new Scanner(System.in);
static Client client = new Client();
public void admin(){
newClient []nc = new newClient[10];
\\login();
while(true){
System.out.println("Select 1:add Client\n 2:add Account\n 3:login as Client");
try{
int selection = scan.nextInt();
switch(selection)
{
case 1: addClient(nc);
break;
case 2: \\addAccount(nc);
break;
case 3: ;
break;
default: System.out.println("INvalid selection");
}
}
catch(InputMismatchException ex){
System.out.println("Invalid input");
scan.nextLine();
}
}
}
public void addClient(newClient []nc){
for(int i=0;i<nc.length;i++){
System.out.println("Enter name");
String name = scan.nextLine();
System.out.println("Enter contact");
String contact = scan.nextLine();
System.out.println("Enter id number");
String idNumber = scan.nextLine();
nc[i]=new newClient(name,contact,idNumber);
System.out.println(nc[i]);
}
}


Output of the first code is

Enter name

Enter contact

Why the name input place is missing?
There are the second code

public static void main(String[]args){
Scanner scan =new Scanner(System.in);
for(int i=0;i<nc.length;i++){
System.out.println("Enter name");
String name = scan.nextLine();
System.out.println("Enter contact");
String contact = scan.nextLine();
System.out.println("Enter id number");
String idNumber = scan.nextLine();
nc[i]=new newClient(name,contact,idNumber);
System.out.println(nc[i]);
}
}


The second code is working correctly.

Enter name

Enter contact

Enter idNumber

Answer

The two pieces of code are in fact different.

In the second code, the scan object is a brand new one. And the only method you call is nextLine(). In the first code, the scan object is created an class level, and has been used before addClient is called.

"Has been used" here is very important. By that I mean you called nextInt on scan and then nextLine:

    int selection = scan.nextInt(); <-- You called nextInt here!
    switch(selection)
    {
    case 1: addClient(nc);
            break;
    case 2: \\addAccount(nc);
            break;  
    case 3: ;
            break;
    default: System.out.println("INvalid selection");
    }
    }
    catch(InputMismatchException ex){
    System.out.println("Invalid input");
     scan.nextLine();
        }
    }    
} 
public void addClient(newClient []nc){
    for(int i=0;i<nc.length;i++){
        System.out.println("Enter name");
        String name = scan.nextLine(); <-- then you called nextLine here!
        System.out.println("Enter contact");
        String contact = scan.nextLine();
        System.out.println("Enter id number");
        String idNumber = scan.nextLine();
        nc[i]=new newClient(name,contact,idNumber);
        System.out.println(nc[i]);
    }
}

This makes the nextLine method return an empty string.

Why?

Let's look at the documentation for both nextInt and nextLine:

nextInt()

Scans the next token of the input as an int.

nextLine()

Advances this scanner past the current line and returns the input that was skipped.

I have created a simpler code that reproduces this situation to explain why calling nextLine immediately after nextInt can cause problems:

Scanner s = new Scanner(System.in);
s.nextInt();
System.out.println(s.nextLine());

I will use a | character to denote where the scanner's current position is.

When the program starts,

|

Then I enter the number 20:

|20

Now the scanner reads the 20, according the documetation, the scanner should be at this position:

20|

Here comes the interesting part, nextLine "Advances this scanner past the current line and returns the input that was skipped."

20
|

So what input has the scanner skipped? Nothing! As a result, an empty string is returned.