-2

I am building an interface that executes an action depending on the user's input. If the user inputs '1', the program will send the sensor data by MQTT, if the user inputs '2', the sensor is sent by CoAP. Below is my code:

import java.util.Scanner;
import org.eclipse.paho.client.mqttv3.MqttException;

public class MainProgram {

public static void main(String[] args) throws MqttException {

    System.out.println("Choose the communication protocol");
    System.out.println("1. MQTT");
    System.out.println("2. CoAP");

    try(Scanner scanner = new Scanner(System.in)){

        if(scanner.nextLine().equals("1")){
            try {
                new UltrasonicSensorMQTT().run();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        else if(scanner.nextLine().equals("2")){
            try {
                new UltrasonicSensorCoAP().run();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        else
            System.out.println("Invalid input. Client destroyed !");    


    }catch(Exception e){
        e.printStackTrace();
    }
}

}

However, the problem is that, the program can only work if I input '1', then it will execute the class UltrasonicSensorMQTT. If I input '2' and any other letter, nothing happens, the class UltrasonicSensorCoAP doesn't run (this class runs perfectly fine in a standalone version), but when I press ENTER again, the line "Invalid input. Client destroyed !" is printed. Does anyone has an idea how to fix this and make it work as I intended

2
  • 2
    Don't swallow nextLine as you're doing. Put it first into a String variable and then run tests on the variable. But more important you need to learn the skill of mentally walking through your code, asking what it and the data is doing as the code is running. Do this and your problem would be obvious to you as it is to myself and @Pshemo. Commented Aug 15, 2017 at 11:36
  • 2
    Each time you call nextLine() you are moving to next line. You need to store its result and compare that stored value in your if statements, not directly calling nextLine(). (now where is that duplicate). Commented Aug 15, 2017 at 11:36

3 Answers 3

0

Your current code requires that you input "2" twice to trigger the second if. After you input it the first time, the code is sitting on the second if condition's call to scanner.nextLine() and awaiting input.

but when I press ENTER again, the line "Invalid input. Client destroyed !" is printed.

Because you didn't input anything the second time.

Don't scan for input twice. Just scan for it once and store the result in a variable. Then perform your logic on that variable:

String inputValue = scanner.nextLine();
if(inputValue.equals("1")) {
  // ...
}
else if(inputValue.equals("2")) {
  // ...
}
// etc.
Sign up to request clarification or add additional context in comments.

Comments

0

you should store nextLine() output in a variable and then do the if/elseif comparison.

Otherwise you are using first user input to check the if and then request a second user input for the else.

Comments

0

You have read and check condition, and firs line is lost, else clause check next, not the same value. Keep in variable.

  String wrk = scanner.nextLine();
  if(wrk.equals("1")){
     ...
    }

    else if(wrk.equals("2")){
       ....
   }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.