• 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

how to validate my array properly when searching for a particular array?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For some reason my validation seem to not work properly where if i type an element that's not in the array it repeats the word invalid
I want the validation to say invalid once and then it should ask the user to input again.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sujath Mohammed wrote:For some reason my validation seem to not work properly where if i type an element that's not in the array it repeats the word invalid



That "some reason" is because that's exactly what you're telling it to do. Your loop says, "For every element in the winners array, either print out the corresponding element in the years array (if the winners element matches the input), or print out 'invalid' (if it doesn't match the input)."

There's nothing there that says anything about "if the input wasn't found at all in the winners array." You need to work out how to do that without any reference to Java. Just work out--clearly and precisely--how to to it "by hand." Then you can translate those steps to Java.

It's an odd requirement though. What you're saying is, if somebody enters a team that has never won, the response will be "Invalid," instead of something more sensible like, "That team has never won."

And finally, I'll note that the winners/years parallel arrays is a horrible way to manage this data.
 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Sujath Mohammed wrote:For some reason my validation seem to not work properly where if i type an element that's not in the array it repeats the word invalid



That "some reason" is because that's exactly what you're telling it to do. Your loop says, "For every element in the winners array, either print out the corresponding element in the years array (if the winners element matches the input), or print out 'invalid' (if it doesn't match the input)."

There's nothing there that says anything about "if the input wasn't found at all in the winners array." You need to work out how to do that without any reference to Java. Just work out--clearly and precisely--how to to it "by hand." Then you can translate those steps to Java.

It's an odd requirement though. What you're saying is, if somebody enters a team that has never won, the response will be "Invalid," instead of something more sensible like, "That team has never won."

And finally, I'll note that the winners/years parallel arrays is a horrible way to manage this data.



You see i have no choice its the requirement of my assignment to use parallel arrays
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sujath Mohammed wrote:You see i have no choice its the requirement of my assignment to use parallel arrays


The problem has nothing to do with your use of parallel arrays. It has to do with the placement of the statement that prints the message within a for-loop. How many times are you seeing the "Invalid" message? How many iterations are executed by the for-loop?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm curious about your "requirements" though. Would you care to share the exact text of the problem as it was given to you by your instructor? It just seems like an odd thing to teach since there other data structures that would serve the same purpose better, especially in Java.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This is because your else () is inside the for loop.
So when the input is wrong it will execute for i=0 to i<winners.length-times the else.

what you can do for instance is put the for-loop inside the if(). in that case it checks if the statement is true and then starts the for loop.
if the statement's not true it goes to the else and just executes it once.

regards,
Kenneth
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I'm curious about your "requirements" though. Would you care to share the exact text of the problem as it was given to you by your instructor?


I too would be curious to see the text of the problem exactly as it was given to you; because unless it says that you must use the two arrays that were given to you throughout your program, then you're doing it wrong (or at least you're making an awful lot of work for yourself).

This is now the 3rd thread you've started on this same subject (perhaps you didn't like the advice you were given in the previous ones), and Campbell already hinted to you in your second one how you could do it better - create a Winner class that contains both team name and year.

You could still put those in an array, and the whole business then becomes a LOT simpler.

Winston
 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Junilu Lacar wrote:I'm curious about your "requirements" though. Would you care to share the exact text of the problem as it was given to you by your instructor?


I too would be curious to see the text of the problem exactly as it was given to you; because unless it says that you must use the two arrays that were given to you throughout your program, then you're doing it wrong (or at least you're making an awful lot of work for yourself).

This is now the 3rd thread you've started on this same subject (perhaps you didn't like the advice you were given in the previous ones), and Campbell already hinted to you in your second one how you could do it better - create a Winner class that contains both team name and year.

You could still put those in an array, and the whole business then becomes a LOT simpler.

Winston



Well I did like the answers given to me they were helpful i managed to get most of my program done because of them
Thank you for everyone who helped especially Winston
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic