0

I wish to limit the input of an integer value, between certain values using a while loop, with a couple of if-else if-else statements inside it! It's kinda working, but not exactly as it should... thought of using a switch as well, but I'm too "green" to know how! If someone's up for it and knows how... I'd welcome the use of a switch as well! Even a nested switch if need be... Here's my code:

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);
    //System.out.println("Engine is on!");
    System.out.println("Do you wish to depart? Shift in to the first gear then and accelerate!");
    System.out.println("Type in the speed: ");
    Honda.accelerate(sc);
    System.out.println("We are departing! Shifting in to " + Honda.currentGear + 
                       "st gear and accelerating to " + Honda.currentSpeed + " km per hour!");
}

Constructor & 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) {

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

public int accelerate(Scanner in){
        while(in.hasNextInt()){
            currentSpeed = in.nextInt();
            if(isEngineOn && currentSpeed > 0){
                currentGear++;
            } else if(currentSpeed > 50){
                System.out.println("We cannot accelerate to more than 50 km per hour, when shifting in the 1st gear!");
            } else{
                System.out.println("We cannot depart at 0 km per hour!");
            }
        }
        return 0;
    }
}

It's taking the input, but it's not going further with it as it should, neither does it give an error message or stop the app, what's my mistake?

Dragos
  • 111
  • 1
  • 8
  • The point is, if the user types in 0, then it gives the 0 km/h error message, if the user types in more than 50, then it gives the "too much" error message. Basically I want to limit the integer value the user can type in, between 1 and 50. Typing in 50+ is not allowed and the Scanner has to display the error messages over and over, until the user types in the correct value. – Dragos Dec 09 '19 at 21:15
  • About `switch`: You should use it only for switching, not for condition checking. See [this answer](https://stackoverflow.com/a/18510494/2158271) for more information. – haba713 Dec 09 '19 at 21:18
  • @user85421 So you're basically saying that nextInt() and nextLine(), do not kiss and make up...?? Should I convert the int to string? – Dragos Dec 09 '19 at 21:25
  • @user85421 Well I specified at the end that: "It's taking the input, but it's not going further with it as it should, neither does it give an error message or stop the app" – Dragos Dec 09 '19 at 21:27
  • Quote: "unless planing some additional functionality"... ofc I am! =) – Dragos Dec 09 '19 at 21:43

1 Answers1

1

Changing the order of your if statement will work.

In your current method:

if(isEngineOn && currentSpeed > 0)

Will always return true with any value that you enter.

Using this method will get you a little further, although I suspect it will still won't be what you are expecting, but I hope it helps you in the right direction.

public int accelerate(Scanner in){
    while(in.hasNextInt()){
        currentSpeed = in.nextInt();
        if(currentSpeed > 50 && currentGear <= 1){
            System.out.println("We cannot accelerate to more than 50 km per hour, when shifting in the 1st gear!");
        } else if(isEngineOn && currentSpeed > 0){
          currentGear++;
          break; /* I've added this to break out of the method to progress in your flow */
        } else{
            System.out.println("We cannot depart at 0 km per hour!");
        }
      }
      return 0;
   }
}
R Wri
  • 222
  • 1
  • 13
  • Why did you add "final" before the Scanner method? – Dragos Dec 09 '19 at 21:33
  • 1
    You can ignore that. In my work environment, we mark all variables that are never changed as final, so when I ran your code it just added it automatically. I'll remove it here. – R Wri Dec 09 '19 at 21:37
  • 1
    Thumbs up! Thank you! It's working perfectly now! This is what I needed and user85421's link to the stackoverflow.com/a/13102066/85421 issue! Now I simply allow it to be typed in as a String, then parse it to an Integer and it's working as it should! Thanks again! Cheers! =) – Dragos Dec 09 '19 at 22:08
  • No worries, glad to help you make progress on this. – R Wri Dec 09 '19 at 22:15