1

I need this program to accept user input in the following format: "team1 : team2 : score1 : score2" then assign it to the array variables below, which is working fine but now I need to make it so when the user enters 'stop' the program displays the results and stats of the data in the array. The problem is that when you enter stop it tries to add it to the array but as I have split the input into 4 parts, it just gives me an error (exception)

"java.lang.ArrayIndexOutOfBoundsException" error on the "awayteam = results[1];" line.

My question is: how can I make it so if the user enters 'stop' it doesn't try to add it to the array.

// Sample input: Chelsea : Arsenal : 2 : 1

public static final String SENTINEL = "stop";

public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

        String hometeam = new String();
        String awayteam = new String();
        String homescore = new String();
        String awayscore = new String();

        int result0;
        int result1;

        int sum = 0;

        System.out.println("please enter match results:");

        for (int b = 0; b < 100; b++) {

            String s = sc.nextLine();

            String results[] = s.split(" : "); // parse strings in between the
                                                // dash
                                                // character

            for (String temp : results) {
                hometeam = results[0];
                awayteam = results[1];
                homescore = results[2];
                awayscore = results[3];
            }
            //convert 'score' strings to int value.
            result0 = Integer.valueOf(results[2]);
            result1 = Integer.valueOf(results[3]);

            //stop command 
            if (s.equals(SENTINEL)) {
                System.out.println("stopped");
            }


            // print results

        }

    }
}
Krunal
  • 77,632
  • 48
  • 245
  • 261
JDHS
  • 13
  • 3
  • Put the check for SENTINEL before you try to assign the variables, right after you read the next line. You could surround the assignments in the if-statement, or you could simply break. – Dewick47 Jun 26 '17 at 15:47

4 Answers4

2

You just need to check for the sentinel and break out of the for loop before trying to parse it.

Also, your other for loop is incorrect. You are placing the same values into your variables N times. (I have added a comment labeling it incorrect)

System.out.println("please enter match results:");

for (int b = 0; b < 100; b++) {

    String s = sc.nextLine();

    //CHECK FOR SENTINAL MATCH RIGHT AFTER READING THE NEXT LINE
    if (s.equals(SENTINAL)) {
      break;
    }

    String results[] = s.split(" : "); // parse strings in between the
                                        // dash
                                        // character



    for (String temp : results) { // You don't need this for loop
        hometeam = results[0]; 
        awayteam = results[1];
        homescore = results[2];
        awayscore = results[3];
    }
    //convert 'score' strings to int value.
    result0 = Integer.valueOf(results[2]);
    result1 = Integer.valueOf(results[3]);

    //stop command 
    if (s.equals(SENTINEL)) {
        System.out.println("stopped");
    }


    // print results

}
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
1

Move the part "Stopped" with a break to break out of the loop.

if (s.equals(SENTINAL)) {
      System.out.println("Stopped");
      break;
    }

before split(" : ")

And also you don't need a dummy for loop.

You may do this instead :

while ( sc.hasNextLine() )
{
    String s = sc.nextLine();

    if (s.equals(SENTINAL)) {
          break;
        }

    .....

}

sc.close();

Also remember to close the scanner - sc.close();

SomeDude
  • 13,876
  • 5
  • 21
  • 44
0

You just need to test if it's "stop" before adding in the array :

while (sc.hasNextLine()){ // better than the for loop

    String line = sc.nextLine();

    if ("stop".equals(line)){
        System.out.println("stopped");
        return; // exit
    }

    String[] results = line.split(" : ");

    // ...
}
Didi Bear
  • 356
  • 3
  • 13
0

You need to check if "s" equals or contains "stop" before you try to parse it. The reason you are getting an array out of bounds is because your program is attempting to parse before checking if the user entered stop.

Try changing your code to be something like

    String s = sc.nextLine();
    if(!s.contains("stop")){
                String results[] = s.split(" : "); // parse strings in between the
                                        // dash
                                        // character
        for (String temp : results) {
            hometeam = results[0];
            awayteam = results[1];
            homescore = results[2];
            awayscore = results[3];
        }
        //convert 'score' strings to int value.
        result0 = Integer.valueOf(results[2]);
        result1 = Integer.valueOf(results[3]);

        //stop command 
        if (s.equals(SENTINEL)) {
            System.out.println("stopped");
        }
    } else {
        //display results
    }

This will tell your program that only if the next line does not contain "stop" should it try to parse the line. If it contain stop then it should display the results instead

Edit: Noticed ControlAltDel's answer he explained one of the for loops weren't needed so I removed it from my answer

Rohlex32
  • 120
  • 9