Before I start I'd like to say; I have asked this question before and was flooded by ways I could change my program instead of explicit answers to my question - I am happy with my code I just want to validate it :)
Like the title implies I am trying to validate my string array, I am new to Java and have been trying to put several boolean conditions within the for loop however the code always seems to ignore the loop and proceed as normal for example if I write "if (words == null) print "Error"" the program runs as normal as though the condition was not there even when an array place is empty.
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
if(words.equals(null)){
System.out.println("Null");
}
else
System.out.println(words[0].trim() + " [" + words[2].trim() + "]" + " | " + words[1].trim() + " [" + words[3].trim() + "]"); // Formats and prints the output
}
}
}
}
}
My code creates a sports result table from user input. The user enters input like so "Home_Team : Away_Team : Home_Score : Away_Score", I want to produce an error message and stop the program once one of the slots is empty.
New error
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
if (words.length != 4) { // If the length of the array elements does not equal 4 then print error message
System.out.println("Input was not valid");
//counter--;
//i--;
} else
{
System.out.println(words[0].trim() + " [" + words[2].trim() + "]" + " | " + words[1].trim() + " [" + words[3].trim() + "]"); // Formats and prints the output
}
}
}
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" Totals ");
System.out.println("-------------------------");
System.out.println("Total games played: " + counter);
}