So I have my code compile but when I run it it combines my lines
System.out.print("Enter ID Number");
double ID=s.nextDouble();
if(ID >9999|| ID<0)
ID=0;
System.out.print("Enter Make of Vehicle");
String make=s.nextLine();
System.out.print("Enter Model of Vehicle");
String model=s.nextLine();
System.out.print("Enter Color of Vehicle");
String color=s.nextLine();
System.out.print("Enter Year");
double year=s.nextDouble();
if(year <2000 || year>2017)
year=0;
When I run the program it combines the lines to say
enter make of vehicle enter model of vehicle
How do I stop this as they are supposed to be separate entries
Use System.out.println instead of System.out.print
Put s.nextLine() after reading double. The problem is that s.nextDouble(); does not consume new line character, so s.nextLine(); after System.out.println("Enter Make of Vehicle"); is called automatically, you don't see anything more because there is only this one new line character (and it is being consumed).
double ID=s.nextDouble();
s.nextLine();
if(ID >9999|| ID<0)
ID=0;
Second thing is to replace System.out.print with System.out.println to print output in new line.
Call s.nextLine() after double ID=s.nextDouble(), couse nextDouble() doesn't consume the \n
Related
I am having a problem where my program is scanning for two different inputs at the same time.
import java.util.Scanner;
public class Person {
public static void main(String[] args){
Person p1 = new Person();
System.out.println("1: Add Person");
System.out.println("2: Delete Person");
System.out.println();
System.out.print("Please make a selection: ");
Scanner keyboard = new Scanner(System.in);
int selection = keyboard.nextInt();
switch(selection){
case 1:
System.out.print("Please enter name: ");
String name = keyboard.nextLine();
p1.addPerson(name);
break;
}
}
public Person(){
}
public void addPerson(String name){
int day, month, year;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter date of birth in the format dd mm yyyy: ");
day = keyboard.nextInt();
month = keyboard.nextInt();
year = keyboard.nextInt();
}
}
This is the output:
1: Add Person
2: Delete Person
Please make a selection: 1
Please enter name: Please enter date of birth in the format dd mm yyyy:
The program does not wait for the name to be entered, how do i fix this?
The problem is when you do nextInt() it scans an integer, but not the new line character(\n), so when you call nextLine() it just consumes \n you typed when was doing selection and returns empty string.
Several ways to fix it:
First is to call nextLine() after nextInt. To fix your code you would do this:
int selection = keyboard.nextInt();
keyboard.nextLine();
Second option is to call nextLine() but when you need int wrap in in Integer.parseInt(). So, for example, your selection would look like:
int selection = Integer.parseInt(keyboard.nextLine());
Other option would be to use next() instead of nextLine, however, this approach wouldn't work if name contains spaces.
You should use keyboard.next()
From the java docs:
next(): Finds and returns the next complete token from this scanner.
This is what keyboard.nextLine() does:
nextLine(): Advances this scanner past the current line and returns the input that was skipped.
I'm trying to get the user to enter two strings and store them in two variables called studentName and studentNum, but when I run the program, the first string input is skipped over and only the second string input is allowed to be entered.
It prints out normally, but won't allow me to enter a value for studentName. could anyone help me with this problem?
System.out.print("Please enter your name: ");
studentName = in.nextLine();
System.out.print("\nPlease enter your student number: ");
studentNum = in.nextLine();
Has to do with where the scanner starts and where it puts up a newline character
You can fix this if you change System.out.print to System.out.println
We all know that using method input.nextLine() will allow spaces for the string while running the program, but.. When I use this method inside a loop the run skips the statement. Can anyone please explain why?
I'm trying it with menu:
Code:
do {
System.out.print("Enter your choice: ");
choice = input.nextInt();
switch (choice) {
case 4:
System.out.println("Please give the full information of the project ");
System.out.print("Project code: "); int code = input.nextInt();
System.out.print("Project Title: "); String title = input.nextLine();
System.out.print("Project Bonus in Percent: "); double bip = input.nextDouble();
Project proj4 = new Project(code4,title4,bip4);
System.out.print("Employee ID you're adding project to: "); String id4=input.next();
ers.addProjectToEmployee(id4,proj4);
break;
/* more cases */
}
} while (choice !=13);
Check statement 3 in case 4.
Here's what happened while running the program:
Enter your choice: 4
Please give the full information about the project
Project code: 66677
Project Title: Project Bonus in Percent:
input.nextInt() does not read the end-of-line, so that' why the effect that .nextLine() is being escaped, while actually that .nextLine() is reading the end-of-line left unread in the input-stream by the .nextInt(). You need an extra .nextLine() after .nextInt().
Update:
Do the following:
System.out.print("Project code: "); int code = input.nextInt();
input.nextLine(); // this is what you need to add
System.out.print("Project Title: "); String title = input.nextLine();
I have created a Scanner with system.in.
How do I allow an input to be able to have commas in it without going to the next input?
For example:
System.out.print("Enter City, State.");
String location = scan.nextLine();
I cannot enter city,state because the language thinks I want to proceed to the next scanning input question. How do I allow commas in a scanning string?
*Whole Code
Scanner scan1 = new Scanner (System.in);
System.out.print ("City, State: ");
String location1 = scan.nextLine();
System.out.print ("Enter number: ");
number1 = scan.nextDouble();
System.out.print ("Enter number: ");
number2 = scan.nextDouble();
System.out.print ("City, State: ");
String name2 = scan1.nextLine();
System.out.print ("Enter #: ");
number3 = scan.nextDouble();
System.out.print ("Enter #: ");
number4 = scan.nextDouble();
scan.nextLine(); will return the entire line, commas or not.
If that isn't what's happening, then the problem must be elsewhere and you should provide more code.
Re: full code: That still works. What is the error you're getting / unwanted behavior?
What I think is happening is that the nextLine() is catching the end-of-line character from your previous input.
What happens:
Suppose you enter a number like 12.5 and press enter.
The buffer that Scanner reads from now contains 12.5\n where \n is the newline character.
Scanner.nextDouble only reads in 12.5 and \n is left in the buffer.
Scanner.nextLine reads the rest of the line, which is just \n and returns an empty string. That's why it skips to the next input: it already read "a line".
What I'd do to fix it:
System.out.print ("City, State: ");
String name2;
do{
name2 = scan1.nextLine();
}while( name2.trim().isEmpty() );
What this loop does is it keeps reading the next line until there is a line with something other than whitespace in it.
One possible solution: get the next line as you're doing and then use String#split on it to split on the comma (either that or use a second Scanner object that takes that String as input). If there is only one comma, then splitting on "," will give you an array that holds two Strings. You'll need to trim the second String to get rid of whitespace, either that or split on a more fancy regular expression.
You can either use split as mentioned above, or you could create another scanner on the next line, this would especially be useful if you have more than one fields separated by a ",".
System.out.print ("City, State: ");
Scanner temp = new Scanner(scan.nextLine());
temp.useDelimiter(",");
while(temp.hasNext()){
//use temp.next();
//Do whatever you want with the comma separated values here
}
But I still suggest that if you are just looking at something as simple as "City,State" , go for split.
My problem is when i run the program it runs all the System.out.print right but when i run
it for the second student some of it overlaps like so:
"Enter the second student's name: Enter the student's score: "
instead of
"Enter the second students's name: "
"Enter the student's score: "
I also can not input data into the System.out.print method of the second student
My main code where the error is:
System.out.print("Enter the first student's name: ");
name = reader.nextLine();
student1.setName(name);
for (int i = 1; i <= 3; i++){
System.out.print("Enter the students's score: ");
score = reader.nextInt();
student1.setScore(i, score);
}
System.out.print("Enter the second student's name: "); //overlaps(stays on same line)
//also wont let me enter data here
name = reader.nextLine();
student2.setName(name);
for (int i = 1; i <= 3; i++){
System.out.print("Enter the students's score: "); //program skips to here to input
//data
score = reader.nextInt();
student2.setScore(i, score);
}
the part that deals with the error from the class is as follows
public void setName(String nm){
name = nm;
}
You should call println() to print a newline character, after reading the input.
If by "overlapping" you mean they appear on the same line then you want System.out.println, rather than System.out.print. println emits a trailing newline.
Could you more clearly describe this 'overlapping'? In your original question your 'one versus the other' were identical. Make it a code sample if need be to preserve spacing and such,
I believe you are talking about how all the lines are printed on the same line. You need to either use line break characters '\n' in Java, or use System.out.println
Try inserting:
reader.nextLine();
...before
System.out.print("Enter the second student's name: ");
.nextInt() doesn't swallow the newline from your input, so the next call to nextLine() just gets the newline character and returns you an empty string - and the program continues, printing the next line of output.
If what I understand is right, you may probaly have problems with the input of the java console. I can't tell much because there's not enough information, but maybe you can refer this link for getting input in java console: http://www.abbeyworkshop.com/howto/java/readLine/index.html
And I recommend clearing the un-affected code (about class Student), just check the input & output from console only.