System.out.print is overlapping the questions why? - java

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.

Related

Can you have a boolean expression repeat in a while loop?

I have found a way to make my program recognize if something is a palindrome or not, but when it is not, the program will not repeat.
I already have the boolean section of this implemented before my provided code.
Originally was set with if statements, wanted to replace with while but something like while (!yespalin(potentialp)) doesn't work either
Scanner scan = new Scanner(System.in);
String potentialp;
System.out.println("Please enter a potential palindrome: ");
potentialp = scan.nextLine();
while (yespalin(potentialp))
{
System.out.println("That string is a palindrome");
}
while (!yespalin(potentialp))
System.out.println("That string is not a palindrome, please enter another: ");
potentialp = scan.nextLine();
}
I want the program to repeat if not a palindrome
For starters I see you are missing "{" on your second while.
However I do not undetstand exactly why do you need 2 while loops.
If you would read your code line by line, you can understand why that the first loop is the only one who prints the "correct" line.
However if you enter a bad palindrome, it will never enter the first loop, therefore won't print the desired sentence.
You want 1 loop which will end if you put the correct value if im not mistaken
Which would look something like this:
Scanner scan = new Scanner(System.in);
String potentialp;
System.out.println("Please enter a potential palindrome: ");
potentialp = scan.nextLine();
while (!yespalin(potentialp)){
System.out.println("That string is not a palindrome, please enter another: ");
potentialp = scan.nextLine();
}
System.out.println("That string is a palindrome");
Hope it was helpful.

Java scanner expecting more inputs than needed

Im having some trouble with scanning user input in one of my first java programs. When I compile and run this, I am immediately prompted for input (i.e the command line stops and blinks). When I enter anything, the first line is printed, asking me to enter an integer. Then the second line is printed and I'm prompted to enter another value.
The output from this program is the first two values that I input. This is hard to explain, but it basically asks for 3 input values and only uses two.
import java.util.Scanner;
public class objects
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer please...");
int input = sc.nextInt();
System.out.println("Enter your name please...");
String name = sc.nextLine();
System.out.println("The read values: " + input + ", " + name);
sc.close();
}
}
Put a System.out.flush() command after your println statements if you're reading from the console directly afterward
just use this:
Scanner sc = new Scanner(System.in);
System.out.print ("Enter your name please... ");
String name = sc.nextLine();
System.out.print ("Enter an integer please... ");
int input = sc.nextInt();
System.out.println ("The read values: " + input + ", " + name);
i just moved the integer below the name and it sorta fixed it. hahaha
When you introduce a number you press enter key, nextInt() uses the number but the enter (\n) remains buffered. After this if you call again nextInt(), Java tries to convert \n into a number giving you a NumberFormatException, but if you invoke nextLine() they read the enter as empty string
Here you have a better explanation and one solution
Can't use Scanner.nextInt() and Scanner.nextLine() together
It seems this is an error to do with my installation of VirtualBox. No matter what I try, the problem persists. Even if i try to only read ONE integer, it will ask me to input two values.
Thanks for everyone who tried to help, I learned a lot just trying to debug this.

java - reading two strings from the user

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

Scanner input skipping next input

I am now having a difficult time fixing this problem.
So, I have this code snippet here which asks 3 input from the user.
case 0:
String accnum,pin,name;
System.out.print("Enter account name: ");
name = input.nextLine();
System.out.print("Enter account number: ");
accnum = input.next();
System.out.print("Enter account PIN: ");
pin = input.next();
atm.set_accinfos(name,accnum,pin);
//System.out.print(atm.return_acc() + "\t" + atm.return_pin() + "\n");
break;
But everytime I run it, it always skips the input for the String "name", I tried using input.next(); on it and it works but then it will now skip the input for String "accnum".
EDIT: It also just happens if the input from "name" has a space on it, for example : John Doe.
Am I missing something here?
The nextLine() gobbles the newline character produced by the next() of account pin (when you hit enter). So, it won't ask for name. Add input.nextLine() after pin = input.next(); and try
Try This. i think problem with next(). since next reads input as tokens with default delimiter(i think space is the default delimiter).:
case 0:
String accnum,pin,name;
System.out.print("Enter account name: ");
name = input.nextLine();
System.out.print("Enter account number: ");
accnum = input.nextLine();
System.out.print("Enter account PIN: ");
pin = input.nextLine();
atm.set_accinfos(name,accnum,pin);
//System.out.print(atm.return_acc() + "\t" + atm.return_pin() + "\n");
break;
If you're using input anywhere before this block of code, then you need to be sure to call nextLine earlier on.
If you use next instead, then your input stream will still contain a new line character (\n). Thus, when you start this block of code with nextLine, that call will take the previous \n and not the name which was just entered.
In general, if your Scanner is reading System.in, then you should use nextLine everywhere. next, nextInt, and the other next methods are meant for more predictable data.
Okay, thats for the help everyone, I managed to fix it by adding "input.nextLine();" before "name = input.nextLine();"

scanner nextLine() allowing input that is either one or two words

I know this has been asked to death, but I have yet to search and find a case quite like mine so i figured i'd ask...I have this little code here...
System.out.print("What would you like the name of your new recipe to be? ");
Recipe tempRecipe = new Recipe(name);
name = scan.nextLine();
scan.nextLine();
tempRecipe.setName(name);
System.out.print("How many ingredients does this recipe have? ");
ingredientCount = scan.nextInt();
scan.nextLine();
Now obviously I was running into the issue where the println statements were on the same line and not allowing input, so i threw in that scan.nextLine() to solve the problem. But now the issue is when i read something in it just gives me blank because of too much nextLine()! if I change it to name = scan.next() i can only read in one word and i need to be able to read in both 1 or 2 words indiscriminately if need be, it might also help to know that following this code are these lines
Ingredient tempIngredient = new Ingredient(name, quantity, null);
System.out.print("Enter the name of ingredient number " + (i+1) + ": ");
name = scan.nextLine();
tempIngredient.setName(name);
System.out.print("Enter the quantity of ingredient number " + (i+1) + ": ");
quantity = scan.nextDouble();
scan.nextLine();
tempIngredient.setQuantity(quantity);
System.out.print("Enter the unit of measurement of ingredient number " + (i+1) + ": ");
unit = scan.nextLine();
tempIngredient.setUnit(UnitOfMeasurement.valueOf(unit));
Both tempRecipes name and tempIngredients name need to be capable of holding 1 or 2 words if need be, how do i do this while still fixing the nextLine() problem!?
Call the extra scan.nextLine() only after scan.nextInt(), and not after scan.nextLine(). The idea of using the extra scan.nextLine() is to skip to new-line character and go to the next line (since scan.nextInt() does not do that).
See Scanner issue when using nextLine after nextXXX

Categories

Resources