-2

I am hoping it looks like this:

Lottery analysis

How many days will you analyze? 5

Day 1: 123
Day 2: 1234
too many digits
Day 2: abc
non-numeric data

Day 2: 456
Day 3: 789
Day 4: 012
Day 5: 345

Frequencies

0: 1
1: 2
2: 2
3: 2
4: 2
5: 2
6: 1
7: 1
8: 1
9: 1

public static void main(String[] args) {
    System.out.println("Lottery Analysis\n");

    // Create Scanner
    Scanner days = new Scanner(System.in);

    // Ask the user for how many days they will analyze
    System.out.print("How many days will you analyze? ");
    int analyze = days.nextInt();

    // Ask the user to enter 3 digits for each day
    System.out.println("\nEnter 3 digits for each day \n");
    Scanner scanDigits = new Scanner(System.in);
    int i = 1;

    // Make a loop that prints the number of days so the user can input the digits each time into that day
    String digits3;
    do {
        System.out.print("Day " + i + ": ");
        digits3 = scanDigits.nextLine();

        // Make sure the string used holds only 3 digits
        if (isNumeric(digits3)) {
            i++;
            // Store all digits used in digits3 in an array
            int[] frequencies = new int[10];
            int val = Character.digit(digits3.charAt(0),10);
        }
    }
    while (i <= analyze);

    // Print the Numbers and Frequencies from 0 to 9
    System.out.println("\nFrequencies\n");
    System.out.println(val);
}

public static boolean isNumeric(String digits3)
{
    boolean ret = !digits3.equals("");
    for(int i = 2; i < digits3.length(); i++)
    {
        // Checks if the characters in digits3 are only digits
        if (!Character.isDigit(digits3.charAt(i)))
        {
            System.out.println("non-numeric data");
            ret = false;
        }
    }
    // Tells the user if the characters they input are not over 3 digits
    for (int j = 3; j < digits3.length(); j++)
    {
        System.out.println("too many digits");
        ret = false;
    }
    return ret;
}
Abra
  • 19,142
  • 7
  • 29
  • 41

1 Answers1

1

First the code. Then the explanations.

import java.util.Scanner;

public class LotteryAnalysis {

    public static boolean isNumeric(String digits3) {
        boolean ret = !digits3.equals("");
        for (int i = 2; i < digits3.length(); i++) {
            // Checks if the characters in digits3 are only digits
            if (!Character.isDigit(digits3.charAt(i))) {
                System.out.println("non-numeric data");
                ret = false;
            }
        }
        // Tells the user if the characters they input are not over 3 digits
        for (int j = 3; j < digits3.length(); j++) {
            System.out.println("too many digits");
            ret = false;
        }
        return ret;
    }

    public static void main(String[] args) {
        System.out.println("Lottery Analysis\n");

        // Create Scanner
        Scanner days = new Scanner(System.in);

        // Ask the user for how many days they will analyze
        System.out.print("How many days will you analyze? ");
        int analyze = days.nextInt();

        // Ask the user to enter 3 digits for each day
        System.out.println("\nEnter 3 digits for each day \n");
        Scanner scanDigits = new Scanner(System.in);
        int i = 1;

        // Store all digits used in digits3 in an array
        int[] frequencies = new int[10];

        // Make a loop that prints the number of days so the user can input the digits each time into that day
        String digits3;
        do {
            System.out.print("Day " + i + ": ");
            digits3 = scanDigits.nextLine();

            // Make sure the string used holds only 3 digits
            if (isNumeric(digits3)) {
                i++;
                char[] digits = digits3.toCharArray();
                for (char digit : digits) {
                    int index = digit - '0';
                    frequencies[index]++;
                }
            }
        }
        while (i <= analyze);

        // Print the Numbers and Frequencies from 0 to 9
        System.out.println("\nFrequencies\n");
        for (int j = 0; j < frequencies.length; j++) {
            System.out.printf("%d: %d%n", j, frequencies[j]);
        }
    }
}

I only changed code in method main.

  1. The frequencies array needs to be declared before the do-while loop and not inside the loop. Note that Java initializes each element in the array to zero.
  2. Inside the do-while loop you need to update the frequencies array.
  3. You want to get the frequencies of all the digits but in your code you are only checking the first digit in each three-digit number that the user enters.
  4. After the do-while loop terminates, you want to print all the elements in frequencies. In your code you are printing just the last, three-digit number that was entered by the user.
  5. You only really need one Scanner (and not two) but if you use a single Scanner, then you need to add a call to method nextLine before you enter the do-while loop. Refer to Scanner is skipping nextLine() after using next() or nextFoo()
  6. The code in your question does not compile because you reference [local] variable val outside of its scope. You declare it inside the following if statement (inside the do-while loop):
if (isNumeric(digits3)) {

Hence its scope is the block associated with the if statement and you reference it after the do-while loop. The variable val only exists in the if statement block. In any case, the variable is not required and does not appear in my code, above.

Below is output from a sample run.

Lottery Analysis

How many days will you analyze? 5

Enter 3 digits for each day 

Day 1: 123
Day 2: 1234
too many digits
Day 2: abc
non-numeric data
Day 2: 456
Day 3: 789
Day 4: 012
Day 5: 345

Frequencies

0: 1
1: 2
2: 2
3: 2
4: 2
5: 2
6: 1
7: 1
8: 1
9: 1

Edit

I didn't really look at the code of method isNUmeric, since it gave correct result for sample data in your question, but looking at it now, I see that it may give incorrect result, for example for the string a12, the method will return true and this is wrong. Here is my implementation:

public static boolean isNumeric(String digits3) {
    boolean ret;
    try {
        Integer.parseInt(digits3);
        ret = digits3.length() == 3;
        if (!ret) {
            System.out.println("Please enter a three digit number.");
        }
    }
    catch (NumberFormatException x) {
        System.out.println("non-numeric data");
        ret = false;
    }
    return ret;
}
Abra
  • 19,142
  • 7
  • 29
  • 41