Danny O'sullivan Danny O'sullivan - 1 month ago 25
Java Question

Java Scanner Error Inputting

import java.util.Scanner;

public class asdf {

public static void main(String[] args) {
Scanner firstName = new Scanner(System.in);
//To store a first name

Scanner Surname = new Scanner(System.in);
//To store a surname

System.out.println("Input First Name: ");
firstName.nextLine();
System.out.print("Input your surname: ");
Surname.nextLine();


System.out.println("Your name is " + firstName + " " + Surname);

firstName.close();
Surname.close();
}
}


//output for this is Your name is :


java.util.Scanner[delimiters=\p{javaWhitespace}+][position=7][match
valid=true][need input=false][source
closed=false][skipped=false][group separator=\,][decimal
separator=.][positive prefix=][negative prefix=\Q-\E][positive
suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=12][match
valid=true][need input=false][source
closed=false][skipped=false][group separator=\,][decimal
separator=.][positive prefix=][negative prefix=\Q-\E][positive
suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]

Answer

You are just moving the cursor not storing the data in variable to display also you are printing the object itself which will not give you the string representation of data you are reading. Note:You can use a single Scanner to read both input

 Scanner sc = new Scanner(System.in);
 System.out.println("Input First Name: ");
 String firstName = sc.nextLine();
 System.out.print("Input your surname: ");
 String surname = sc.nextLine();

 System.out.println("Your name is " + firstName + " " + surname);

 sc.close();

DEMO