• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

My Program Problem (Array checks)

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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--;
}
}

 
Sheriff
Posts: 7135
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"david th", please check your private messages regarding an important administrative matter.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check--?
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic