#Error

1 messages · Page 1 of 1 (latest)

sharp geodeBOT
#

<@&987246399047479336> please have a look, thanks.

livid rivet
#

System.out.println("Type in your operation "add or +" "substract or -" "multiply or " "divide or /" ");
String operation = scanner.next();
System.out.println("Enter your first number");
int no1 = scanner.nextInt();
System.out.println("Enter your second number");
int no2 = scanner.nextInt();
int result = 0;
switch (operation) {
case "add", "+":
result = no1 + no2;
break;
case "substract", "-":
result = no1 + no2;
break;
case "multiply", "
":
result = no1 + no2;
break;
case "divide", "/":
result = no1 + no2;
break;
default:
result = 0;
break;
}
System.out.println("Your result of" + no1 + operation + no2 + result);

#

i was trying to make a simple calculator i came across a problem after the first output its not waiting to reply a string

#

System.out.println("Type in your operation "add or +" "substract or -" "multiply or " "divide or /" ");
String operation = scanner.next(); here is the error i think

sharp geodeBOT
#

Pong!

#

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.

hollow vault
#

next() and nextInt() aren't really intended for entry by a user. If the user has to enter a line (type something and hit enter) then read a line - then parse it as the type you're expecting.

livid rivet
hollow vault
#
String s = scanner.nextLine();
int i = Integer.parseInt(scanner.nextLine());
livid rivet
#

ohh

#

Integer.parseInt(scanner.nextLine()); i never heard of this im new

hollow vault
#

That's okay. it's essentially saying

Get a line of input from the scanner. Then, we'll assume it is a valid number and parse it as an int

livid rivet
#

System.out.println("Type in your operation "add or +" "substract or -" "multiply or " "divide or /" ");
String operation = scanner.nextLine();
System.out.println("Enter your first number");
int no1 = Integer.parseInt(scanner.nextLine());
System.out.println("Enter your second number");
int no2 = Integer.parseInt(scanner.nextLine());
int result = 0;
switch (operation) {
case "add", "+":
result = no1 + no2;
break;
case "substract", "-":
result = no1 + no2;
break;
case "multiply", "
":
result = no1 + no2;
break;
case "divide", "/":
result = no1 + no2;
break;
default:
result = 0;
break;
}
System.out.println("Your result of" + no1 + operation + no2 + result);

livid rivet
hollow vault
#

I take it there's some earlier input as well.

livid rivet
#

wait i will make a new project

hollow vault
#

boolean b = Boolean.parseBoolean(scanner.nextLine())

simple hemlock
#

dejavu. now u ran into the issue we tried to tell u to fix 2 days ago

livid rivet
simple hemlock
#

u need to replace ALL uses of next() nextInt() nextDouble() nextBoolean() next whatever by nextLine()

#

in ur entire code

#

then it works

livid rivet
livid rivet
#

i just created new project

#

@hollow vault ?

livid rivet
#

idk bro i am just trying to learn java for minecraft plugins

#

and i followed a guide on yt

hollow vault
#

I would suggest a more structured learning approach. eg the Modern Java online book, or the free Helsinki university online course

sharp geodeBOT
#

For learning Java, we recommend the ebook Modern Java:
https://javabook.mccue.dev/

It is completely free, meant for beginners and covers all content relevant for the first year. It is a great way to learn Java from the ground up.
If you run into any questions during your journey, you may ask us in #1051826284008853505 and we will help cat_thumbs_up

If you prefer a more traditional learning experience, we also recommend MOOC:
https://java-programming.mooc.fi/
This is the course used by the University of Helsinki to teach their students programming in their first year, also completely free.

hollow vault
#

As to your immediate problem...

hollow vault
#

You need to have a scanner to read. Because you want that input from the user you want to 'scan' the System.in inputstream

Scanner scanner = new Scanner(System.in);
hollow vault
#

Or in modern Java (Java 25 or 26) you can just use the IO class.

String operation = IO.readln("Type in your operation: ");
int value = Integer.parseInt(IO.readln("Enter a number: "));