Reputation: 45
I just need to find a way to validate the input so that if the first array input (hometeam) is missing, it displays the message "no home team entered".
Sample input = "Everton : Liverpool : 1 : 1" missing hometeam example " : Liverpool : 1 : 1"
As you can see, my attempt is below, I can't think how to work this out:
if (stats.get(i)[0] == null){
System.out.println("no home team name entered");
}
Here is my full code:
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
ArrayList<String[]> stats = new ArrayList<>(); //initialize a container to hold all the stats
System.out.println("please enter match results:");
while(sc.hasNextLine())
{
String input = sc.nextLine();
String[] results = input.split(" : ");
if(results.length == 4)
{
stats.add(results);
}
else if(input.equals("stop"))
break;
else
System.out.println("Error reading input");
}//end of while
for(int i = 0; i < stats.size(); i++)
{
if (stats.get(i)[0] == null){
System.out.println("no home team name entered");
}
try{
System.out.println(stats.get(i)[0] + " " + Integer.valueOf(stats.get(i)[2]) + " : " +
Integer.valueOf(stats.get(i)[3]) + " " + stats.get(i)[1]);
}catch (Exception e) {
//do nothing with any invalid input
}
}
}
Upvotes: 1
Views: 6447
Reputation: 10959
The split array creates the first element as an empty string - not a null string.
So you need to change stats.get(i)[0] == null
to stats.get(i)[0].isEmpty()
You could also at add in a trim()
so any extra spaces are removed first, or change your split regex to \\s+:\\s+
, this then matches on 1 or more occurrences of space either side of the colon.
If you want to account for cases when the input data may not have spaces then you can change your split regex string to be \\s*:\\s*
, this then matches on 0 or more occurrences of space either side of the colon.
Upvotes: 1
Reputation: 1230
Quite simple. If there is no input your for-loop won't be entered and therefore no check will occur.
Replace your for-loop after reading the content with:
if (stats.isEmpty()){
System.out.println("no home team name entered");
}
else {
try{
for(int i = 0; i < stats.size(); i++){
System.out.println(stats.get(i)[0] + " " + Integer.valueOf(stats.get(i)[2]) + " : " +
Integer.valueOf(stats.get(i)[3]) + " " + stats.get(i)[1]);
}
}
catch (Exception e) {
//do nothing with any invalid input
}
}
Hope that helps.
I read the comments from and understand you problem:
You won't have any String[] that contain more or less than 4 elements. You are ensuring this by saying results.length == 4 otherwise nothing will be added to the ArrayList. Next step is to ensure that a valid Team name is entered and that the last 2 elements are Numbers. Copy the function from How to check if a String is numeric in Java either the green answer or that with the most upvotes.
Upvotes: 0
Reputation: 422
Try using the StringUtils package. You can use StringUtils.isBlank(stats.get(i)[0])
StringUtils.isBlank checks if a string is either null or entirely whitespace.
Additionally, you need to check if stats is empty to display a message that the entire input is blank
Here is the code for isBlank if you're wondering:
public static boolean isBlank(final CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (Character.isWhitespace(cs.charAt(i)) == false) {
return false;
}
}
return true;
}
Upvotes: 0