#Why does it do this?

1 messages · Page 1 of 1 (latest)

pallid river
#
import java.util.Scanner;
public class Moshproject {

    public static void main(String[] args) {

        System.out.print("Enter your principle value(1k-1m): ");
        Scanner principle = new Scanner(System.in);
        String principleValue = principle.nextLine();
        while (!principle.hasNextInt()) {
            System.out.println("Please enter a real value between 1000 and 1000000");
            System.out.print("Enter your principle value(1k-1m): ");
            principle.next();
        }
        int Principle = principle.nextInt();


    }}

I did this but yet it required me to input the scanner twice, it won't read it on the first try, why does it do this?

tulip heronBOT
# pallid river ```java import java.util.Scanner; public class Moshproject { public static ...

Detected code, here are some useful tools:

Formatted code
import java.util.Scanner;

public class Moshproject {
  public static void main(String[] args) {
    System.out.print("Enter your principle value(1k-1m): ");
    Scanner principle = new Scanner(System.in);
    String principleValue = principle.nextLine();
    while (!principle.hasNextInt()) {
      System.out.println("Please enter a real value between 1000 and 1000000");
      System.out.print("Enter your principle value(1k-1m): ");
      principle.next();
    }
    int Principle = principle.nextInt();
  }
}
#

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

tulip heronBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

pallid river
#

More specifically, I have to input the text again for the program to read it, why?

tulip heronBOT
#

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.

tulip heronBOT
#

Closed the thread due to inactivity.

If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍

pallid river
#

but how specifically can I solve my problem

#

because i haven't declared an integer yet

remote spear
#

that's just one example

#

reread the paragraph from the bot

#

ur using
hasNextInt()
next()
nextInt()
and all 3 of them are bad.

#

they cause ur problem. replace all 3 of them and it will work

pallid river
#

How should i replace them

remote spear
#

have u actually fully read and understood the explanation the bot gave u?

#

if so, whats unclear to u?

pallid river
remote spear
#

please answer my question first, thanks

pallid river
#

and not read the actual input

#

requirng you to do it again

remote spear
#

and it explains what to do instead

#

it explains that ur usage of
next()
nextInt()
hasNextInt()
is incorrect and has to be replaced. and it shows how

pallid river
#

I understand that you have to replace any .nextInt with Integer.parseInt(scanner.nextLine)

#

to get rid of the .nextInt

#

but I'm confused about what to do with hasNextInt

#

and how to replace it

remote spear
#

not only that

#

it explains that any console input of that kind is a full-line-input

#

so any interaction is always a full line

#

so u must read a full line

#

and check for full lines

pallid river
#

I don't understand

#

How can i replaec the hasNextInt method

remote spear
#

again. u would ask for "is there another line of input".

that part doesn't even make sense to me. the whole loop seems flawed to me

#

why does it loop? if the user enters bad stuff? that has to be done entirely different

#

by catching the error thrown by Integer.parseInt

pallid river
remote spear
#

yeah but the way u did that doesn't work at all

#

u have to approach it differently

#
public static OptionalInt tryParseInt(String text) {
  try {
    return OptionalInt.of(Integer.parseInt(text));
  } catch (NumberFormatException e) {
    return OptionalInt.empty();
  }
}
#

sth like that

pallid river
#

Can you also loop the try catch method?

remote spear
#

u would loop whether it ran into the catch or not