1
package u7a1_numofoccurrinsevenints;
/**
 * 
 * @author SNC78
 */
import java.util.Scanner;

public class U7A1_NumOfOccurrInSevenInts {

public static void main(String[] args) {
    //constant for number of entries
    final int MAX_INPUT_LENGTH = 7;
    // array to hold input
    int[] inputArray = new int[MAX_INPUT_LENGTH];

    Scanner input = new Scanner(System.in);

    System.out.print("Enter seven numbers (separated by spaces):");

    int max = Integer.MIN_VALUE;
    // loop to read input, store in array and find highest value
    for(int i = 0; i < MAX_INPUT_LENGTH; i++) {
        // Read a single int - Scanner has no nextInt()
        inputArray[i] = (input.next().charAt(0));
        if(inputArray[i] > max) {
            max = inputArray[i];
        }
    }

    // Use highest value + 1 to determine size of count array.
    // Use +1 because highest index in array is always 1 less than size
    int[] countArray= new int[max + 1];

    // Loop through input and count by mapping value in input array to
    // index number in count array. Increment value in count array at that
    // location.
    for(int i = 0; i < MAX_INPUT_LENGTH; i++) {
        countArray[ (int) inputArray[i]]++;
    }

    // Loop countArray to produce output of counts.
    // Loop needs to run as long as < max + 1
    // Could also use i <= max
    for(int i = 0; i < max + 1; i++) {
        // Only print out counts for numbers that occur in input.
        if(countArray[i] > 0) {
            System.out.println( "Number " + i + " occurs "
            + countArray[i] + " times.");
        }
    }


}

}

So this is what I'm working with ^^. My output is as follows: run:

Enter seven numbers (separated by spaces):22 23 22 78 78 59 1
Number 49 occurs 1 times.
Number 50 occurs 3 times.
Number 53 occurs 1 times.
Number 55 occurs 2 times.

I've built this current program off of a similar one but can't locate why it's returning odd numbers back instead of the ones entered. Like how it's returning 49, 50, 53... instead of any of the numbers I've entered

2
  • It looks like we have two different interpretations of what you're attempting to do. Did you want to count the digits of the numbers, or the numbers themselves? Commented Dec 21, 2018 at 20:10
  • The goal was to count the numbers themselves and to see how many times they were entered. Commented Dec 21, 2018 at 21:51

2 Answers 2

1

The problem is that you are using:

input.next().charAt(0);

to scan in the next int. This is taking a char, converting it to it's int value (Or ascii value - not what you want) and then adding it to the int Array. So when you enter 20, it takes the first char, 2, and converts it to it's ascii value, 50 (You entered three values in the 20's, so 50 was recorded three times).

You want to use the nextInt() method to read in int's:

inputArray[i] = input.nextInt();
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thank you so much!, as I had read over the code several times and couldn't for the life of me realize that I was still using a char value there. This is because I was converting an older chunk of code from a similar program that did what I'm doing here for int for char. The removal of charAt also seemed necessary as rgettman stated, but that line of code replacing it does just that.
0

There appears to be an issue when you read in the integers from the input. Try using input.nextInt() rather than (input.next().charAt(0));. Scanner does have a nextInt.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.