• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays  RSS feed

 
Cece Nickson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to complete this problem. I am able to get the integer in the array printed but I am lost on how to hold and print the distinct numbers. ANY GUIDANCE OR HELP would be appreciated. Thanks!

This program will read in 10 numbers from the user via the console. It will display on the
console only the distinct numbers, i.e. if a number appears multiple times in the input it is
displayed only once.

Here is a sample test run:
Enter an integer: 2
Enter an integer: 6
Enter an integer: 1
Enter an integer: 8
Enter an integer: 6
Enter an integer: 1
Enter an integer: 2
Enter an integer: 4
Enter an integer: 7
Enter an integer: 5
The number of distinct values is 7
2 6 1 8 4 7 5
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing what you already have there is what you want to happen, but don't have the code for it figured out quite yet.

An idea of what you could do is to accept each user input as an integer, check your array to see if that integer is already there and if it is not you can add it to the array.
This way only one copy of each integer can be held in the array during the execution of the program. For this, take a look at class ArrayList<E>.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skye Antinozzi wrote: For this, take a look at class ArrayList<E>.

I would suspect that collections and generics are a bit advanced for this member at this juncture.

Cece, welcome to the Ranch.

Could you show us what code you have so far and be explicit about the problems that you are having with it?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!