We’re rewarding the question askers & reputations are being recalculated! Read more.
2 Removed unnecessory text
source | link

Java score validation Score Validation

This was originally posted in SO but I was not aware of this website at the time so I figured I could re-post it.

Was just wondering if I was using nested if statements to much. I've been looking around and it seems that people try to not use them. Also does the code look messy in any way? Anyways here it is:

Java score validation

This was originally posted in SO but I was not aware of this website at the time so I figured I could re-post it.

Was just wondering if I was using nested if statements to much. I've been looking around and it seems that people try to not use them. Also does the code look messy in any way? Anyways here it is:

Score Validation

Was just wondering if I was using nested if statements to much. I've been looking around and it seems that people try to not use them. Also does the code look messy in any way? Anyways here it is:

1
source | link

Java score validation

This was originally posted in SO but I was not aware of this website at the time so I figured I could re-post it.

Was just wondering if I was using nested if statements to much. I've been looking around and it seems that people try to not use them. Also does the code look messy in any way? Anyways here it is:

CheckScores.java

import java.util.Arrays;

public class CheckScores {

private int maxScore = 6;
private int sum;

public void CheckScores(String userScores) {
    String[] scoresArray;

    // Check if userScores is valid
    // ^ Match with beginning of line | [0-9] Allow 0-9 | , Allow comma | + Match one or more | $ Match End of line
    if (userScores.matches("^[0-9,]+$")) {

        // Check if string starts with an ,
        if (userScores.charAt(0) == ',') {
            // If it does parse and substring to remove them
            // otherwise the following regex leaves one , behind
            int i = 0;
            while (!Character.isDigit(userScores.charAt(i))) i++;
            int j = userScores.length();
            userScores = userScores.substring(i, j);
        }

        // (.) Match any character) | \1 If it is followed by itself | + Match one or more | $1 replace by the first captured char.
        userScores = userScores.replaceAll("(.)\\1+", "$1");

        System.out.println(userScores);

        // Split at the ',' and put each number in it's own cell in the array
        scoresArray = userScores.split(",");
        System.out.println(Arrays.toString(scoresArray));

        // Check if scoresArray is equal to maxScores
        if (scoresArray.length == maxScore) {
            int[] uiArray = new int[scoresArray.length];

            // Parse String[] into int[]
            for (int i = 0; i < scoresArray.length; i++) {
                try {
                    uiArray[i] = Integer.parseInt(scoresArray[i]);
                } catch (NumberFormatException nfe) { // If triple checking isn't enough...
                }
            }
            System.out.println("scoresArray array(String): " + Arrays.toString(scoresArray));
            System.out.println("uiArray(int): " + Arrays.toString(uiArray));

            // Add up all elements in uiArray
            for (int j = 0; j < uiArray.length; j++) {
                sum += uiArray[j];
            }
            System.out.println("Scores sum:" + sum + " Number of scores:" + uiArray.length + " Number of ends:" + uiArray.length / 6);
        } else {
            System.out.println("You have " + scoresArray.length + " scores. Acceptable amount is " + maxScore);
        }
    } else {
        System.out.println("Invalid Input. (Only #'s and ,'s allowed)");
    }
  }
}

Main.java

public class Main {

public static void main(String[] args) {
    CheckScores cs = new CheckScores();
    cs.CheckScores("1,M,7,10,,4,8,");
    cs.CheckScores("1,6,7,10,,4,8,,,,,,,1,2,6,10,2,10");
    cs.CheckScores(",,,,,,,1,2,6,10,2,10");
    cs.CheckScores("10,2,1,5,7,1");
    cs.CheckScores("6,2, ,,5,6,1");
    }
}