File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes My Program Problem (Array checks) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "My Program Problem (Array checks)" Watch "My Program Problem (Array checks)" New topic
Author

My Program Problem (Array checks)

Nolan Bradshaw
Greenhorn

Joined: May 13, 2010
Posts: 3
My problem with this code that I've written it only checks the existing number. I can't create another array to and compare them or check them. It cannot be done because my instruction explicitly asked us to use only one array. I have the assignment pretty much wrapped up the only problem is if you enter in the same number it will get put in the indices anyhow. meaning I type 11 it enters it in then I type 11 again, it says hey that's already there re-enter another number I type 11 again and well it enters 11 anyhow
Check out my code, advice is welcome. I don't want to have to use a trigger either to make this work.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Your code always adds the number, regardless of the if statement--perhaps walk through it on paper and see what's really happening.

What's a "trigger" in this context?
david meng
Greenhorn

Joined: Sep 17, 2007
Posts: 4
In your 2nd for iterator, you should add check-- as below:
for( int check = 0; check < arrayNumbers.length ; check++)
{
if( arrayNumbers[check] == number)
{
System.out.println("Repeat numbers, try again");
System.out.println("Re-enter number:");
number = input.nextInt();
check--;
}
}

Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3013
    
  35

"david th", please check your private messages regarding an important administrative matter.


Founder of ExamLab and Systemup
See how I can help you to become an awesome programmer
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

check--?
Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
hey i was doing pretty much the same problem earlier today. It was in an exercise from the book Java How to Program, is this where you got it from?


Do you want it to check against all the numbers in the array? If so have you considered using a boolean variable to indicate whether it's already there? You could change the boolean variable to true if the value is already in the array.

Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
So would i be right in saying that if the number is in the array it asks you to enter another then it carries on with the for loop where it left off, therefore not checking the array indexes it has already checked? Because it should check them again.

This is the first time i've ever attempted to actually help someone as i'm still very new to java so sorry if my answer completely sucked! haha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My Program Problem (Array checks)
 
Similar Threads
Head-Tail Counter
Else without if error
Making Decisions Lottery application
simple array object
fields of visibility