This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to validate my array properly when searching for a particular array? 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 "how to validate my array properly when searching for a particular array?" Watch "how to validate my array properly when searching for a particular array?" New topic
Author

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

Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Apr 17, 2013
Posts: 31
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
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

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 - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

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.
Kenneth Van Gysegem
Ranch Hand

Joined: Mar 30, 2013
Posts: 33
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
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7494
    
  18

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to validate my array properly when searching for a particular array?
 
Similar Threads
How to display a particular array?
And the winners are ...
The winners are ....
How to get my arrays alphabetically sorted?
AND THE WINNERS ARE!!!