Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My Program Problem (Array checks)

 
Nolan Bradshaw
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4262
229
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"david th", please check your private messages regarding an important administrative matter.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check--?
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic