简体   繁体   中英

How can I restrict the "new Scanner(System.in)" system in Java, to not go further with the user input data, unless it's the correct inputted value?

I'm learning Java and I've gotten somewhat further with my knowledge. I want the app to go further with the user inputted value, only if it's the correct one, otherwise I want for the same question(console input) to repeat it self(not go further with the app) until the user types in the correct value! My code looks like this:

package oop.project;

import java.util.Scanner;

/**
 *
 * @author Dragos
 */
public class OOPProject {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner sc = new Scanner(System.in);
        Car Honda = new Car(2018, 20000, "Honda", "Civic", 200, 6, 0, 0);
        System.out.println("Manufacturer is: " + Honda.maker + ", model: " + Honda.model + 
                           ", year of fabrication: " + Honda.year + ", price: " + Honda.price + 
                           "!");
        System.out.println("Please start the engine of your vehicle, by typing in 'Yes' or 'Start' 
                           or 'Turn on'!");
        System.out.print("Do you wish to start the engine?");
        System.out.println(" ");
        Honda.StartEngine(sc);
}

Constructor class & it's functions:

public class Car {

    public int year;
    public int price;
    public String maker;
    public String model;

    public int maximumSpeed;
    public int numberOfGears;
    public int currentSpeed;
    public int currentGear;
    public boolean isEngineOn;

    public Car(int year, int price, String maker, String model, int maximumSpeed,
               int numberOfGears, int currentSpeed, int currentGear) {
        this.year = year;
        this.price = price;
        this.maker = maker;
        this.model = model;
        this.maximumSpeed = maximumSpeed;
        this.numberOfGears = numberOfGears;
        this.currentSpeed = currentSpeed;
        this.currentGear = currentGear;
    }

    public String StartEngine(Scanner in) {
        String input = in.nextLine();
        do{
            isEngineOn = true; 
        } while(input.equals("Yes") || input.equals("Start") || input.equals("Turn on"));

        if(isEngineOn) {
            System.out.println("Engine is on!");
        } else {
            System.out.println("Your input is not correct! Please start the engine!");
            isEngineOn = false;
        }  
        return input;
    }

Before the do & while, I had only the if-else statements with the (input.equals("Yes") || input.equals("Start") || input.equals("Turn on")) in the if statement, but regardless of the fact that the user inputs the wrong words in the IDE console, the app keeps going further anyway, it does display the correct messages, when the wrong word(s) are input, but does not stop or constrain the user to type in the correct value! Attempted to restrict the user to type in the correct value, by adding the do-while statement, but it's not working very well.

You have to be on loop untill you get the valid input. Try the snippet below.

while(in.hasNext()) {
    String input = in.nextLine();
    if(input.equals("Yes") || input.equals("Start") || input.equals("Turn on")) {
        System.out.println("Engine is on!");
        break;
    } else {
        System.out.println("Your input is not correct! Please start the engine!");
    }
}

If you want to return input from the function, you can do like the one as follows.

while (in.hasNext()) {
    String input = in.nextLine();
    if (input.equals("Yes") || input.equals("Start") || input.equals("Turn on")) {
        System.out.println("Engine is on!");
        return input;
     } else {
        System.out.println("Your input is not correct! Please start the engine!");
     }
}
return null;
    while(!isEngineOn) {
      String input = in.nextLine();
      isEngineOn = input.equals("Yes") || input.equals("Start") || input.equals("Turn on"));
      if (!isEngineOn) System.out.println("Your input is not correct! Please start the engine!");
    }

Or like this if you need the answer later:

    String input;
    while(!isEngineOn) {
      input = in.nextLine();
      isEngineOn = input.equals("Yes") || input.equals("Start") || input.equals("Turn on"));
      if (!isEngineOn) System.out.println("Your input is not correct! Please start the engine!");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM