Reputation: 41
I am a student at a local tech in Ireland first year. We are as you guessed doing java and the at the moment we are handling Scanners at the moment. I have added my code at the bottom and a little confused.
When I run the program it works fine till I get to the "Enter country" and the "Enter course" and these two line land up printing together no matter if I change the scanner to f.nextLine or f.next.
Anyone able to help me out with this as I am missing something and I can't figure out what it is.
Many thanks
Matthew
public static void main(String[] args)
{
String name; //declaring string name
String town; //declaring string town
double dist; //declaring double distance from AIT the person lives
int age; //declaring their age
double height; //declaring their height
double weight; //declaring their weight
String country; //declaring their country that they are from
String course; //declaring their course that they are taking
int years; //declaring the amount of years they are in the course
String sNum; //declaring there email address string
Scanner f = new Scanner(System.in);
//Scanner s = new Scanner(System.in);
// below is the prompt for all the information and there keyboard input with scanner
System.out.print("Enter your name here & press enter: ");
name = f.nextLine();
System.out.print("Enter the name of the town or city you live in & press enter: ");
town = f.nextLine();
System.out.print("Enter the distance from AIT that you live in kilometers & press enter: ");
dist = f.nextDouble();
System.out.print("Enter your age & press enter: ");
age = f.nextInt();
System.out.print("Enter your height & press enter: ");
height = f.nextDouble();
System.out.print("Enter your weight & press enter: ");
weight = f.nextDouble();
System.out.print("Enter the name of country that you are from & press enter: ");
country = f.nextLine();
System.out.print("Enter the name of course you are taking & press enter: ");
course = f.nextLine();
System.out.print("Enter the number of years the course is & press enter: ");
years = f.nextInt();
System.out.print("Enter your student number & press enter: ");
sNum = f.next();
//all the outputs of the information collected
System.out.println("Your name: "+name+" Town: "+town);
System.out.println(town+" is "+dist+" away from AIT.");
System.out.println("You are "+height+" meters tall & "+weight+" kgs & you are a "+country+" citizen.");
System.out.println("Your studying "+course+" for "+years+" years at AIT.");
System.out.println("Your student number is "+sNum+" & your student email address is "+sNum+"@student.ait.ie");
System.out.println("Please review the information.");
}
Upvotes: 1
Views: 246
Reputation: 13407
If you use
System.out.print("Enter the name of country that you are from & press enter: ");
country = f.next();
System.out.print("Enter the name of course you are taking & press enter: ");
course = f.next();
it will work. You can do this for the other nextLine
s also. For some reason you read the student number as a string instead of a number, but since you treat all you input as strings, you could just use next
for everything.
Edit:
Don't forget to call f.close()
to close the scanner.
Upvotes: 0
Reputation: 1215
nextDouble() scans the next token and parses it into a double. That is why you are facing this issue. You can simply move you nextLine() above the nextDouble(). Its a temporary solution to solve the problem.
Try below code. I just edited your code.
public static void main(String[] args)
{
String name; //declaring string name
String town; //declaring string town
double dist; //declaring double distance from AIT the person lives
int age; //declaring their age
double height; //declaring their height
double weight; //declaring their weight
String country; //declaring their country that they are from
String course; //declaring their course that they are taking
int years; //declaring the amount of years they are in the course
String sNum; //declaring there email address string
Scanner f = new Scanner(System.in);
//Scanner s = new Scanner(System.in);
// below is the prompt for all the information and there keyboard input with scanner
System.out.print("Enter your name here & press enter: ");
name = f.nextLine();
System.out.print("Enter the name of the town or city you live in & press enter: ");
town = f.nextLine();
System.out.print("Enter the name of country that you are from & press enter: ");
country = f.nextLine();
System.out.print("Enter the name of course you are taking & press enter: ");
course = f.nextLine();
System.out.print("Enter the number of years the course is & press enter: ");
years = f.nextInt();
System.out.print("Enter your student number & press enter: ");
sNum = f.next();
System.out.print("Enter the distance from AIT that you live in kilometers & press enter: ");
dist = f.nextDouble();
System.out.print("Enter your age & press enter: ");
age = f.nextInt();
System.out.print("Enter your height & press enter: ");
height = f.nextDouble();
System.out.print("Enter your weight & press enter: ");
weight = f.nextDouble();
//all the outputs of the information collected
System.out.println("Your name: "+name+" Town: "+town);
System.out.println(town+" is "+dist+" away from AIT.");
System.out.println("You are "+height+" meters tall & "+weight+" kgs & you are a "+country+" citizen.");
System.out.println("Your studying "+course+" for "+years+" years at AIT.");
System.out.println("Your student number is "+sNum+" & your student email address is "+sNum+"@student.ait.ie");
System.out.println("Please review the information.");
}
}
Upvotes: 1