1

So I have a while-loop where you have 3 options to choose from and you choose them by inserting a number on standard input using a scanner, my code is like this:

    int option;
    String request;
    Scanner input2 = new Scanner(System.in);
    System.out.println("Choose an option:\n" + "1-Get camera information\n" + "2-Submit Data\n"
         + "3-Exit");
    while(true){
        try {
            option = input2.nextInt();
            if (option == 1) {
                System.out.println("Camera name:");
                request = input2.nextLine();
                while (request.length() < 3 || request.length() > 15) {
                    System.out.println("Name has to be between 3 and 15 characters, insert a new one:");
                    request = input2.nextLine();
                }
                CamInfoRequest info_request = CamInfoRequest.newBuilder().setName(request).build();
                if (stub.camInfo(info_request).getReturn() != 0) {
                    System.out.println("Camera does not exist");
                } else {
                    System.out.println(stub.camInfo(info_request).getLatitude() + " " + stub.camInfo(info_request).getLongitude());
                }
            } else if (option == 2) {
                System.out.println("submit");
            } else if(option ==3){
                break;
            } else{
                System.out.println("Invalid option.");
            }
        }catch(InputMismatchException e){
            System.out.println("Invalid input");
        }
    }

So the way this is the code enters in an infinite loop when it catches the exception where it keeps printing "Invalid input", I tried using input2.next() at the catch but then he waits for another input I don't want, I can't use input2.close() either. What can I do?

MuchoG
  • 43
  • 5
  • Your first mistake is using `while(true)`. Use a boolean flag instead. Would look like `while(exitLoop)` and then set `exitLoop = true` where you want to leave the loop. – user0000001 Apr 01 '20 at 13:31

2 Answers2

1

I can't use input2.close() either.

You should never close the Scanner instance for System.in as it also closes the System.in.

I tried using input2.next() at the catch but then he waits for another input I don't want

Use Scanner::nextLine instead of Scanner::next, Scanner::nextInt etc. Check Scanner is skipping nextLine() after using next() or nextFoo()? to learn why.

Also, try to use do...while wherever you need to ask the user to enter the data again in case of an invalid entry.

Given below is a sample code incorporating these points:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int option;
        boolean valid;
        Scanner input2 = new Scanner(System.in);
        do {
            valid = true;
            System.out.println("Choose an option:\n" + "1-Get camera information\n" + "2-Submit Data\n" + "3-Exit");
            try {
                option = Integer.parseInt(input2.nextLine());
                if (option < 1 || option > 3) {
                    throw new IllegalArgumentException();
                }
                // ...Place here the rest of code (which is based on the value of option)
            } catch (IllegalArgumentException e) {
                System.out.println("This is an invalid entry. Please try again.");
                valid = false;
            }
        } while (!valid);
    }
}

A sample run:

Choose an option:
1-Get camera information
2-Submit Data
3-Exit
abc
This is an invalid entry. Please try again.
Choose an option:
1-Get camera information
2-Submit Data
3-Exit
6
This is an invalid entry. Please try again.
Choose an option:
1-Get camera information
2-Submit Data
3-Exit
2

Feel free to comment in case of any further doubt/issue.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
0

Just Put the Scanner statement inside your try block

while (true) {
            try {
                Scanner input2 = new Scanner(System.in);
                option = input2.nextInt();
                if (option == 1) {
  • The infinite printing stops that way, the problem is that imagine I start the loop, choose an invalid input and then I choose option 1, it immediately enters in the while and prints "Name has to be between 3 and 15 characters, insert a new one:" before I can even write something – MuchoG Apr 01 '20 at 13:39
  • Try using request = input2.next(); instead of request = input2.nextLine(); – Sandeep Lakdawala Apr 01 '20 at 13:43