Question : Java Array issue...simple application not accepting input?

I am learning about arrays.  I have an application that prompts a user to input a number between 1 and 10.  Based on the input, it shows a message.  If the number that is inputted is over 10, it returns an error.

My application compiles fine.  It does not return any errors.  When I run it, I am prompted to enter a value between 1 and 10.  From there it immediately returns an error.  Its as if it doesn't 'see' what I entered.

Can someone please review my code and tell me where it is that I am going wrong?

Thanks!
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
public class JavaArray 
{
	public static void main(String[] args)throws Exception 
	{
   	String[] reasons = {"Easier to understand than VB.Net",
                          "Textbook is pretty decent",
                          "Very flexible",
                          "Alan makes it fun",
                          "Don't have to draw flowcharts for it",
                          "JGrasp is pretty cool",
                          "Made my first successful app",
                          "KISS approach works great",
                          "Great support",
                          "Jock asks ALOT of questions"};
		int[] num = {1,2,3,4,5,6,7,8,9,10};
		char choice;
		int i;
		System.out.print("\nEnter a number between 1 and 10: ");
      choice = (char)System.in.read();
      System.in.read();
      System.in.read();
		for (i = 0; i < 10; i++)
			if (choice == num[i])
			{
				System.out.println("One of the Top 10 Reasons why I like Java: " + reasons[i]);
				return; 
			}
		System.out.println("Invalid Entry");
   } 
}

Answer : Java Array issue...simple application not accepting input?

When you are reading the int value from the input stream, you read ascii value of the character that you have read.

Just change the line 19 to
int choice = System.in.read();
choice = choice - 48;

Change the datatype of the choice to int from char
Random Solutions  
 
programming4us programming4us