kingstreet73
kingstreet73

Reputation: 13

How do I make my array print repetitions of an input once? (JAVA)

I have to design a program that takes in arbitrary input from 0-50, prints out all the inputs ONCE, and then prints out the occurrence of each input.

I have it working to some degree, but, when the input is: 1 , 2 , 3 , 3 , 3 , 6 , 9 , 0 , 0

It prints out:

Input : Occurrence

     Number   Times
      1         1
      2         1
      3         1
      3         2
      3         3
      6         1
      9         1
      0         1
      0         1

instead of:

Input : Occurrence

     Number Times
       0    2
       1    1
       2    1
       3    3
       6    1
       9    1

This is a beginner course and most of the solutions I've seen online seem to be advanced using some kind of mapping technique that I haven't learned yet.

 public static void main(String [] args)
{

   int[] array = new int[51];
   Scanner scan = new Scanner(System.in);
   System.out.println("Number \t   Times");

   while (scan.hasNext()){    
    int x = scan.nextInt();
    if (x>=0 && x<=50){
        array[x]++;
  System.out.println(x + "\t      " + array[x]);
      }
    }
  }
}

I've tried multiple ways of formatting the loops but I can't seem to find out how to get it to print a number that is input multiple times just once.

Upvotes: 1

Views: 48

Answers (3)

Jonty Morris
Jonty Morris

Reputation: 807

Here's another answer if you're still looking. I'll leave the hashmaps answer as other people might find that useful, but I decided to also get your current code working.

int[] numbers = new int[51];

// Small loop to get the number input
Scanner scanner = new Scanner(System.in);
for (int i=0; i<10; i++) {
    System.out.print("> ");
    int x = scanner.nextInt();

    if (x >= 0 && x <= 50) {
        numbers[x] += 1;
    }
}

// Now display the results after getting input
System.out.println("Number \t     Times");
for (int i=0; i<numbers.length; i++) {
    if (numbers[i] != 0) {
        System.out.println(i + "\t\t" + numbers[i]);
    }
}

Upvotes: 0

Jonty Morris
Jonty Morris

Reputation: 807

Welcome to the StackOverflow community! I know you mentioned that you haven't learnt about 'advanced mapping techniques' yet, but why not learn about them now? There's a good chance you will be needing them again in the future anyway.

We can easily solve this problem by using something called a 'hashmap'. A hashmap is useful because it lets you store two values at each index, a key and a value. This is useful because the key is related to the value (which means you can find the value if you have the key), and there can't be duplicate keys.

Here's an example of using hashmaps to solve your problem.

// Here we create our hashmap. Be adding <Integer, Integer>, we are telling the hashmap
// that the the key and value will be of type Integer (note that we can't just pass in int)
HashMap<Integer, Integer> numbers = new HashMap<Integer, Integer>();

Scanner scan = new Scanner(System.in);
System.out.println("Number \t   Times");

while (scan.hasNext()){    
  int x = scan.nextInt();
  if (x>=0 && x<=50){

      // Check if the number has already been added
      // to the hash map
      if (numbers.containsKey(x)) {
          // If so, we will get the existing value
          // and increase it by 1
          numbers.put(x, numbers.get(x) + 1);
      }

      else {
          // Otherwise we will add the value
          // to the hash map
          numbers.put(x, 1);
      }

      System.out.println(x + "\t      " + numbers.get(x));
  }
}

Upvotes: 0

sprinter
sprinter

Reputation: 27986

Welcome to SO. The simplest way to solve this without using maps, or even storing the values anywhere is to first sort the array (the example you give is already sorted) and then just count up the number of adjacent duplicates.

In pseduo-code the algorithm should look something like

count = 1
value = array[0];
for each item from 1 to length
    if item == value
        increment count
    else
        print value: count
        count = 1
        value = item
print value: count

Note that there needs to be 2 outputs - each time the value changes and at the end of the list. Ideally you'd store the value and count in an object to avoid code duplication but I'm assuming that's too advanced at this stage.

Hopefully you can convert that to code relatively easily.

Upvotes: 1

Related Questions