1

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
                }
            }

}

fdelafuente
  • 1,114
  • 1
  • 13
  • 24
JHurst
  • 45
  • 1
  • 10
  • So. What does your attempt do that it shouldn't do? What does it not do that it should? – Hovercraft Full Of Eels Jun 28 '17 at 00:02
  • It should tell the user that they didn't enter a value into [0], but it just doesn't say anything, it skips past the if statement – JHurst Jun 28 '17 at 00:05
  • 2
    `if (stats.get(i)[0] == null || stats.get(i)[0].trim().isEmpty()){`? - Either adding a `System.out` statement to test the value or a simple run through with a debugger probably would of helped – MadProgrammer Jun 28 '17 at 00:10
  • Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – StackFlowed Jun 28 '17 at 00:34

3 Answers3

1

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.

Java Devil
  • 10,629
  • 7
  • 33
  • 48
  • This truly has helped a lot! although it only give the "no home team entered" msg if you put a space in the first element (" : Arsenal : 2 : 1")... if you don't put a space (": chelsea : 2 : 1") it just comes back with " vs Arsenal : 2 - 1"? – JHurst Jun 28 '17 at 00:43
  • That is because it is expecting a space in the split, if there is no space then the split will not occur on the first `:`, just remove the space requirement or change your regex to `\\s*:\\s*` (0 or more spaces) – Java Devil Jun 28 '17 at 01:15
  • Thanks for your time!:) – JHurst Jun 28 '17 at 01:26
0

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.

sascha10000
  • 1,245
  • 1
  • 10
  • 22
0

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;
}
gwcoderguy
  • 392
  • 1
  • 13