Archived

This topic is now archived and is closed to further replies.

Paul R

Help with a simple java selection method

Recommended Posts

hey there, im trying to write a simple java selection method. If the value the user enters doesnt any of the values in the array, it loops until a valid one is entered. However it states a value is invalid even if the value is valid! The code is below int[] array = {1,2,3}; int selection = 0; //selection method, this is the bit causing the trouble screen.println("please select a number, 1, 2 or 3"); selection = new Integer(keyboard.readLine()).intValue(); screen.println("selection : "+selection); if (selection != array[0] || selection != array[1] || selection != array [2]) { do { screen.println("Invalid selection, please enter the correct one"); selection = new Integer(keyboard.readLine()).intValue(); } while(selection != array[0] || selection != array[1] || selection != array[2]) } Any Suggestions are VERY much appreciated [edited by - Paul R on April 30, 2003 7:11:47 PM] [edited by - Paul R on April 30, 2003 7:12:50 PM]

Share this post


Link to post
Share on other sites
if (selection != array[0] || selection != array[1] || selection != array [2])

That is the line with problems. It matches true if any of them don''t match, which is always the case because the number can''t be 1, 2 and 3 simultaneously. Try changing them to &&, should work (although it is late here and I am sleep )

Stu

Share this post


Link to post
Share on other sites