lundi 19 décembre 2016

What is the correct syntax for validating user input?

My code creates a set of sport results using a scanner, the user enters input in this format "Home team : Away team : Home score : Away score" - each part is split into a string in the array. I want to create an error message if one part is missing for example "Error, Home team seems to be missing" for each corresponding section however;

String [] football_list = new String [100];         //Declare the array 
int counter = 0;                                //Initialize counter integer

scanner = new Scanner(System.in);{      //Import Scanner
System.out.println("Input as follows; "); {     //User instructions
System.out.println("Home team : Away team : Home score : Away score");

String line = null; { // Creates a blank string

while (!(line = scanner.nextLine()).equals("")) { // The code in the loop will process only if it is not empty

    if (line.equals("quit")) { // When the user is finished this exits the program
           break;
    } else {
        football_list[counter] = line; // The below String is printed for the amount of values inside the counter integer

        System.out.println("Home team : Away team : Home score : Away score");
    }

    counter++;  // counter is incremented
}

for (int i = 0; i < counter; i++) { // A loop to control the Array 
    String[] words = football_list[i].split(":"); // Splits the input into 4 strings
    System.out.println(words[0].trim() + " [" + words[2].trim() + "]" + " | " + words[1].trim() + " [" + words[3].trim() + "]");

I am a beginner and have been trying to put an else-if condition in the for loop to help make this error message however I am doing something wrong judging by the amount of errors I am getting.

Aucun commentaire:

Enregistrer un commentaire