0

Here I have the code to receive input to the speed. However I am wanting to make it so when type "more" it will give 5 MPH and the next time I type move it will be 10 MPH, so adding 5 to the previous output.

public static void readInputSpeed(String prompt) {
        System.out.print(prompt);
        String inputSpeed = new Scanner(System.in).nextLine();
        Car.speed(inputSpeed);

    }

What I am looking for is

User types "more" Output: 5 User types "more" Output: moves from 5 to 10 (so the code stores the previous value and then add 5 to the previous value that it was from the last iteration)

I have tried to insert the block of code into a while loop and that works to get multiple inputs. As for input I am trying to implement, I have tried to use the while loop with a fori loop. I have also tried to use arrays to try to maybe store the input into the array and reference back to that same spot in the array.

Here is the code for the array and loops I used:

public static void readInputSpeed(String prompt) {
        String input = "";
        int[] array = new int[1];
        array[0] = 0;
        while (input != "quit") {
            System.out.print(prompt);
            input = new Scanner(System.in).nextLine();
                for (int i = 0; i < 1; i++) {
                    array[i] = i;
                    if (input == "more")
                        i += 5;
                    else if (input == "less")
                        i -= 5;
                    System.out.println(i + " MPH");
            }
        }
    }
Stewl3
  • 1
  • 2
  • [How do I compare strings in Java](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Abra Mar 12 '23 at 05:09
  • `input = new Scanner(System.in).nextLine();` Create a `Scanner` object once only, before the `while` loop. – Abra Mar 12 '23 at 05:13
  • Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Progman Mar 12 '23 at 09:45

2 Answers2

0

Do this:

public static void readInputSpeed(String prompt) {
    Scanner sc = new Scanner(System.in); // create scanner outside loop
    int speed = 0; // need a variable to hold speed
    String input = "";
    while (!input.equalsIgnoreCase("quit")) { // comparing strings case insensitively
        System.out.print(prompt);
        input = sc.nextLine();
        
        if (input.equalsIgnoreCase("more")) // comparing strings case insensitively
            speed += 5;
        else if (input.equalsIgnoreCase(“less"))
            speed -= 5;
        System.out.println(speed + " MPH");
    }
}
John Williams
  • 4,252
  • 2
  • 9
  • 18
0

I believe your problem is the way you're comparing strings. The reason why it doesn't keep storing increments of 5 into your 'i' variable is because when you use the '==' operator in java, you're telling the program to check to see if two strings are the same object, but that's not what you want. You want to use the 'str.equals()' method when you want to see if two strings share the exact same characters. Since your if statement checks to see if these strings are equal and it's not passing because of your conditions not being setup properly, it's not able to increment the value of 'i'. Also, I'm not sure if you need the for loop or array since I can't see the rest of your code, but here's some sort of way I would go about coding it. Hope this helps.

package readInputSpeed;
import java.util.Scanner;

public class rInputSpeed {
    public static void main(String args[]) {
        int inputSpeed = new Scanner(System.in).nextInt();
        readInputSpeed(inputSpeed);
    }
    
    public static void readInputSpeed(int prompt) {
        String input = "";
        int i = prompt;
        while (!(input.equals("quit"))) {
            System.out.println(prompt);
            input = new Scanner(System.in).nextLine();
            if (input.equals("more"))
                i += 5;
            else if (input.equals("less"))
                i -= 5;
            System.out.println(i + " MPH");
        }
    }
}

Kierzoh
  • 1
  • 1