#how do i fix a line of code that prints before it gives a user a chance to give input
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
post the code
Please use this format for posting code:
```java
// Example java program
int value = 5;
System.out.println(value);
```
Which results in:
// Example java program
int value = 5;
System.out.println(value);
For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.
System.out.println();
System.out.println("Jelly information");
System.out.println("Enter the name of the jelly.");
String jN1 = keyboard.nextLine();
keyboard.nextLine();
System.out.println("Enter the number of calories.");
int jC1 = keyboard.nextInt();
keyboard.nextLine();
System.out.println("Enter the type of jelly. Must be \"Apple\", \"Blueberry\"," +
" \"Grape\", \"Strawberry\", or \"Tomato\".");
String jT1 = keyboard.nextLine();
Jelly jelly1 = new Jelly(jN1, jC1, jT1);
Detected code, here are some useful tools:
System.out.println();
System.out.println("Jelly information");
System.out.println("Enter the name of the jelly.");
String jN1 = keyboard.nextLine();
keyboard.nextLine();
System.out.println("Enter the number of calories.");
int jC1 = keyboard.nextInt();
keyboard.nextLine();
System.out.println("Enter the type of jelly. Must be \"Apple\", \"Blueberry\"," + " \"Grape\", \"Strawberry\", or \"Tomato\".");
String jT1 = keyboard.nextLine();
Jelly jelly1 = new Jelly(jN1, jC1, jT1);
i fixed the problem i was having with th e keyboard.nextline after string jN1, but it is not storing the inputted string to jN1
wdym?
It is storing it to jN1
Also prefer parsing rather then using nextInt() since it does not skip the new line
For example
int jC1 = Integer.parseInt(keyboard.nextLine());
System.out.println("-----Sandwich 1-----");
System.out.println("PBJ Sandwich");
System.out.println("Top Slice:");
System.out.println(topSlice1.toString());
System.out.println("Peanut Butter:");
System.out.println(peanutButter1.toString());
System.out.println("Jelly:");
System.out.println(jelly1.toString());
System.out.println("Bottom Slice:");
System.out.println(bottomSlice1.toString());
Detected code, here are some useful tools:
System.out.println("-----Sandwich 1-----");
System.out.println("PBJ Sandwich");
System.out.println("Top Slice:");
System.out.println(topSlice1.toString());
System.out.println("Peanut Butter:");
System.out.println(peanutButter1.toString());
System.out.println("Jelly:");
System.out.println(jelly1.toString());
System.out.println("Bottom Slice:");
System.out.println(bottomSlice1.toString());
so im trying to print it out with this code and the name of the jelly is blank
could you post your Jelly class
public class Jelly {
private String name;
private int calories; //Between 50-200, def is 50
private String fruitType; //Apple, blackberry, GRAPE, blueberry, and tomato
public Jelly() {
name = "none";
calories = 50;
fruitType = "Grape";
}
public Jelly(String aN, int aC, String fT) {
this.setName(aN);
this.setCalories(aC);
this.setFruitType(fT);
}
public String getName() {
return this.name;
}
public int getCalories() {
return this.calories;
}
public String getFruitType() {
return this.fruitType;
}
public void setName(String aN) {
if(aN != null)
name = aN;
else
name = "none";
}
public void setCalories(int aC) {
if(aC >= 50 && aC <= 200)
calories = aC;
else
calories = 50;
}
public void setFruitType(String fT) {
if(fT != null && (fT.equalsIgnoreCase("Apple") || fT.equalsIgnoreCase("Blackberry") || fT.equalsIgnoreCase("Grape") || fT.equalsIgnoreCase("Blueberry") || fT.equalsIgnoreCase("Tomato")))
fruitType = fT;
else
fruitType = "Grape";
}
public boolean equals(Jelly j) {
return j != null &&
name.equals(j.getName()) &&
calories == j.getCalories() &&
fruitType.equals(j.getFruitType());
}
public String toString() {
return "Jelly Name: "+getName()+" Calories: "+getCalories()+" Fruit Type: "+getFruitType();
}
}
Detected code, here are some useful tools:
In this code, why are you calling nextLine()
twice in each request
Jelly information
Enter the name of the jelly.
hello
Enter the number of calories.
500
Enter the type of jelly. Must be "Apple", "Blueberry", "Grape", "Strawberry", or "Tomato".
tomato
Jelly Name: hello Calories: 50 Fruit Type: tomato
besides it works for me
i didn't have it and this was happening
you are getting the number of calories
and not inputting anything
and so it's trying to parse t into a number
yeah that's why i added the extra nextLine()
is that what's fucking up the storing of the name of the jelly
no it should work just fine
Scanner keyboard = new Scanner(System.in);
System.out.println();
System.out.println("Jelly information");
System.out.println("Enter the name of the jelly.");
String jN1 = keyboard.nextLine();
System.out.println("Enter the number of calories.");
int jC1 = Integer.parseInt(keyboard.nextLine());
System.out.println("Enter the type of jelly. Must be \"Apple\", \"Blueberry\"," + " \"Grape\", \"Strawberry\", or \"Tomato\".");
String jT1 = keyboard.nextLine();
Jelly jelly1 = new Jelly(jN1, jC1, jT1);
System.out.println(jelly1);
It works
Is this replit?
ah
i created a separate class and it works in that class but once I put the code back into my whole FE class it doesnt work
Your entire code
Yeah I know the issue
You are using keyboard.nextBoolean()
always parse the result from nextLine()
don't use those methods
they are bad
When you do keyboard.nextBoolean()
it will read in a boolean but not the resulting new line - \n
so your String jn1 = keyboard.nextLine() is getting passed the new line character
because the cursor in the keyboard is all messed up
Mixing any nextXXX method with nextLine from the Scanner class for user input, will not ask you for input again but instead result in an empty line read by nextLine.
To prevent this, when reading user input, always only use nextLine. If you need an int, do
int value = Integer.parseInt(scanner.nextLine());
instead of using nextInt.
Assume the following:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Hello " + name + ", you are " + age + " years old");
When executing this code, you will be asked to enter an age, suppose you enter 20.
However, the code will not ask you to actually input a name and the output will be:
Hello , you are 20 years old.
The reason why is that when you hit the enter button, your actual input is
20\n
and not just 20. A call to nextInt will now consume the 20 and leave the newline symbol \n in the internal input buffer of System.in. The call to nextLine will now not lead to a new input, since there is still unread input left in System.in. So it will read the \n, leading to an empty input.
So every user input is not only a number, but a full line. As such, it makes much more sense to also use nextLine(), even if reading just an age. The corrected code which works as intended is:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age:");
// Now nextLine, not nextInt anymore
int age = Integer.parseInt(scanner.nextLine());
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Hello " + name + ", you are " + age + " years old");
The nextXXX methods, such as nextInt can be useful when reading multi-input from a single line. For example when you enter 20 John in a single line.
thank you so much i just switched it
my professor uses it so i didnt realize that it messed my code up
Yeah it's a tricky thing, most schools teach to use nextInt() and the corresponding methods
It just doesn't work like you would expect, that's why the best practice for this is just to ALWAYS parse it
i didnt know boolean messed it up, i assumed it worked like nextLine() bc my prof always uses nextLine() as a fix-up when using nextInt()
Yeah that's just an ugly way to fix the cursor problem, it works just not optimal