Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java – Cannot get loop to recognize newly generated array values

I am creating a program for class where I can bet on a horse race. The winners are determined by a method given to us by our professor. here is the method:

void readySetGo() {
    System.out.println(array.length);
    int length = array.length;
    Random rgen = new Random();
    for (int i = 0; i < array.length; i++) {
        int randomValue = i + rgen.nextInt(length - i);
        int randomElement = array[randomValue];
        array[randomValue] = array[i];
        array[i] = randomElement;
    }
}

I created a string-based menu using a while loop. In order to make things easier on myself, I created a cheat box that displays the winners before you place your bet.

System.out.print("Cheat: ");
System.out.println(Arrays.toString(racer.getArray()));

Here is the main method:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public static void main(String[] args) {
    race racer = new race();
    wallet balance = new wallet();

    Scanner input = new Scanner(System.in);
    double pick1;
    double pick2;
    String response;

    balance.setUSDbalance(200);

    racer.readySetGo();

    System.out.print("Cheat: ");
    System.out.println(Arrays.toString(racer.getArray()));

    System.out.println("Welcome to Charlie's Horse Racing Bets!");
    System.out.println("\nType one of the strings below and add your horse numbers after:");
    System.out.println("Example: 'Exacta Box 2 3'");
    System.out.println("\n=> Exacta");
    System.out.print("\nEnter your choice: ");

    response = input.nextLine();

    String words[] = response.split(" ");

    while (true) {
        if (words[0].equalsIgnoreCase("Exit")) {
            System.out.println("Thanks for playing! \nSee you soon!");
        } else if (words[0].equalsIgnoreCase("Exacta")) {
            pick1 = Double.parseDouble(words[1]);
            pick2 = Double.parseDouble(words[2]);

            boolean way1 = (pick1 == racer.first()) && (pick2 == racer.second());
            boolean way2 = (pick1 == racer.second()) && (pick2 == racer.first());
            if (way1 || way2) {
                System.out.println("You won the exact box");
                System.out.print("The winning order was: ");
                System.out.println(Arrays.toString(racer.getArray()));
                System.out.print("\nCheat for next round: ");
                racer.readySetGo();
                System.out.println(Arrays.toString(racer.getArray()));
                System.out.print("\nEnter your new choice: ");
                response = input.nextLine();
            } else {
                System.out.println("You chose the wrong order. Play again?");
                System.out.print("\nEnter your new choice: ");
                response = input.nextLine();
            }
        }
    }
}

The problem is that the system recognizes the correct user input the first time, but doesn’t work at all the second time. Example:

Cheat: [4, 1, 3, 2]
Welcome to Charlie's Horse Racing Bets!

Type one of the strings below and add your horse numbers after:
Example: 'Exacta Box 2 3'

=> Exacta

Enter your choice: exacta 4 1
You won the exact box
The winning order was: [4, 1, 3, 2]

Cheat for next round: [3, 1, 4, 2]

Enter your new choice: exacta 3 1
You chose the wrong order. Play again?

>Solution :

As your code currently stands, you’re only parsing the user’s input the first time. Inside the loop you’re reading the user’s input again, but you’re not updating the words variable based on the new input.

You should move this line inside your loop:

String words[] = response.split(" ");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading