943,639 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 573
  • Java RSS
Nov 9th, 2008
0

Java help - RE Arrays

Expand Post »
Hi, I am having some trouble with arrays.

I have an array of 5 elements. The values of the array are integers. I want to validate the input using an if statement. (the numbers must be between 10 and 100).

I then want to compare the user input to the unique numbers in the array using a for statement. I only want to store the new number if it is unique.

Please help, I'm desperate!!
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
stevenp123 is offline Offline
12 posts
since Nov 2007
Nov 9th, 2008
0

Re: Java help - RE Arrays

show us your effort with some code and we will help
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Nov 9th, 2008
0

Re: Java help - RE Arrays

This is the code so far

public class array
{
//This gerts 5 unique numbers from the user
public void getNumbers()
{
Scanner input = new Scanner( System.in );

new int[5] //Constructing the array

int[] number = new int[5]; //Declaration of the array variable
int count = 0; // Number of uniques read
int entered = 0; // Number of numbers entered

//Checks whether the number entered has already been entered into the array, if it has then the following message is displayed:
while( entered < numbers.length )
{
System.out.print( "Enter number:" );
int[0] = input.nextInt;

if (int[] == number)
Reputation Points: 10
Solved Threads: 1
Newbie Poster
stevenp123 is offline Offline
12 posts
since Nov 2007
Nov 9th, 2008
0

Re: Java help - RE Arrays

I'll go through each of the errors I see.

1) new int[5] //Constructing the array
This line does nothing useful and is missing a semicolon. You should remove the line completely.

2) int[0] = input.nextInt
This line should be using the variable name, not the variable type to reference the array. Also, you're missing parentheses after nextInt , also, you want to change the index each loop so you don't rewrite over element 0. Probably should be: number[entered] = input.nextInt();

3) if (int[] == number)
I have no idea what you're trying to do here.
Last edited by destin; Nov 9th, 2008 at 10:20 pm.
Reputation Points: 32
Solved Threads: 10
Junior Poster in Training
destin is offline Offline
94 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: calculating three dice
Next Thread in Java Forum Timeline: Help adding Intergers in array(s)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC