-1

I want to add an integer to a list based on user input. The user has to type all the integers he/she wishes then press enter. if they finish inputting integer, they are supposed to press the "enter" button without typing anything.

I have made my code, but there are several mistakes

the exception keeps popping up because every time say for example I enter integer 10, then I finish. I press "enter" with nothing. this raises the exception. how do I tackle this problem?

and another thing, how do I make the program so that if the user puts invalid input, instead of crashing or breaking. It asks the user again to prompt the correct input.

this is what I have done

package basic.functions;
import java.util.*;
import java.text.DecimalFormat;

public class Percent {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        reader.useDelimiter(System.getProperty("line.separator"));
        List<Integer> list = new ArrayList<>();
        System.out.println("Enter Integer: ");

        while (true) {
            try {
                int n = reader.nextInt();
                list.add(Integer.valueOf(n));
            } catch (InputMismatchException exception) {
                System.out.println("Not an integer, please try again");
                break;

            }
        }
        reader.close();
   }
}

output

Enter Integer: 
10

Not an integer, please try again 
[10]
Ousmane D.
  • 50,173
  • 8
  • 66
  • 103
Kutam
  • 89
  • 1
  • 12
  • Possible duplicate of [exception handling java user input](http://stackoverflow.com/questions/43760671/exception-handling-java-user-input) – jschnasse May 03 '17 at 14:43

2 Answers2

1

I'd suggest you utilise Scanner#hasNextInt to identify whether an integer has been entered or not. As for when the "user presses enter without typing anything", we can simply use the String#isEmpty method.

while (true) {
     if(reader.hasNextInt()) list.add(reader.nextInt());
     else if(reader.hasNext() && reader.next().isEmpty()) break;
     else System.out.println("please enter an integer value");
}

note - in this case, you don't need to catch InputMismatchException because it won't be thrown.

Ousmane D.
  • 50,173
  • 8
  • 66
  • 103
  • I think youve mistaken what I mean. Its not the user typing enter. its if the user press enter without putting any input. then stop – Kutam May 03 '17 at 14:37
  • thanks, this is what I had at the start, but some guy told me to change it. the guy in the bottom. Because i want to throw exception. the idea is so that if user enter invalid input, there will be an error message, asking the user to re prompt. – Kutam May 03 '17 at 14:51
  • @Kutam see my update which notifies the user to enter correct data when he/she has not. Also, because the `Scanner` has methods that help you know whether what is entered is an `int`, `double`, `string` etc, we don't need to concern ourselves with `try/catch` and that makes your code looks messy. This is a simple task and should be accomplished just using the methods of the `Scanner`. – Ousmane D. May 03 '17 at 14:55
0

while (true) is generally a bad sign, if you ever have that in your code you are almost certainly wrong.

What you probably want is something like this:

String input;
do {
  input = reader.next();
  // Parse the input to an integer using Integer.valueOf()
  // Add it to the list if it succeeds
  // You will need your try/catch etc here
while (!input.isEmpty());

Here the loop is checking the exit condition and running until it meets it. Your processing is still done inside the loop as normal but the program flow is a lot cleaner.

Tim B
  • 38,707
  • 15
  • 73
  • 123