Results 1 to 5 of 5
  1. #1
    mxsar is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default "Not a digit" error

    Java Code:
    import java.io.*;
    
    public class Menu
    {	public static void main(String args[]) throws IOException
    	{
    		
    		// print menu
    		for (int i=1; i<=5; i++)
    			System.out.println(i+". Menu item #" +i);
    		System.out.println("0. Quit");
    		// handle user commands
    		boolean quit=false;
    		int menuItem;
    		do {
    			System.out.print("Choose menu item: ");
    			menuItem=nextInt();
    			switch (menuItem) {
    			
    			case 1:
    			{	System.out.println("You've chosen item #1");
    				int myInt;
    			System.out.println("Give a number ");
    			myInt=nextInt();
    			System.out.println("The number is: "+myInt);
    			}
    				break;
    			
    /*			case 2:
    				System.out.println("You've chosen item #2");
    				// do something...
    				break;
    			case 3:
    				System.out.println("You've chosen item #3");
    				// do something...
    				break;
    			case 4:
    				System.out.println("You've chosen item #4");
    				// do something...
    				break;
    			case 5:
    				System.out.println("You've chosen item #5");
    				// do something...
    				break;
    */				
    			case 0:
    				quit = true;
    				break;
    			default:
    				System.out.println("Invalid choice.");				
    			}
    		} while (!quit);
    		System.out.println("Bye-bye!");
    	}
    
    
    	public static void count(int i)	throws IOException		//method count()
    	{ 
    		System.out.println("Will print all numbers until the given integer");
    		System.out.println("Please give an integer ");
    		
    		int a=0, b=nextInt();
    		while (a<=b)
    		{	System.out.println(a);
    			a=a+1;
    		}	
    	}
    	public static int nextInt() throws IOException  //method nextInt()
    	{	int result=0; int tmp, tmp1;
    		boolean isdigit;
    		tmp=System.in.read();
    		while (tmp!=13) //13 number of enter
    		{	tmp1=tmp-48;  //48 number of 0
    			isdigit=isDigit(tmp1); 
    			if (isdigit==false)
    				{	while (System.in.read()!=13)
    					{}
    					System.out.println("Not a digit.");
    					return -1;
    				}
    			result=result*10+tmp1;
    			tmp=System.in.read();
    		}
    		return result;
    	}
    
    
    	public static boolean isDigit(int x) //method isDigit()
    	{	boolean isdigit;
    		if ((x>=0)&&(x<=9))
    		isdigit=true;
    		else isdigit=false;
    		return isdigit;
    	}
    
    }
    Hello all! :)
    I'm trying to make a program which reads the given integer (method nextInt()) and according to which case you will choose does some other things like method count(). (The other cases are empty cos I'll put another methods in there). I'm trying to put count() in case 1 of switch statement but I get this annoying "Not a digit" no matter what i press: digit or not.
    So I have tried something even easier than inserting the method count() in case 1 but it insists on "Not a digit". I hope that if I solve the problem in the easy statement (in which I only tell it to print what I typed) I'll be able to use the method count() correctly.
    I guess there is a problem in nextInt() and isDigit() and probably the problem is in isDigit(). I'm trying 2 days to solve it and I have written the program 5 times and I'm always ending to "Not a digit". :confused:

    If you are curious why I don't use Scanner, it is not supported where I want to execute it (previous version of jdk) and I have no rights to install the newer version. SO there is no way I can use sweet and simple Scanner. I'm stuck with nextInt(). :mad:

    Does anyone have any suggestions?

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Not sure how you're getting this error.

    1) I had to surround your statements with try{}catch(){} because of your method that throws IOException.
    2) Your nextInt method needs to check 10 as well as 13 for endline. CR = 10, LF = 13.
    3) Here is my run data after addressing #1 and #2 above:
    Java Code:
    run:
    1. Menu item #1
    2. Menu item #2
    3. Menu item #3
    4. Menu item #4
    5. Menu item #5
    0. Quit
    Choose menu item: 3
    Invalid choice.
    Choose menu item: 1
    You've chosen item #1
    Give a number 
    0
    The number is: 0
    Choose menu item: 0
    Bye-bye!

  3. #3
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    I don't know what's causing the problem either, but I did notice that this method contains a lot of redundancy:

    Java Code:
    	public static boolean isDigit(int x) //method isDigit()
    	{	boolean isdigit;
    		if ((x>=0)&&(x<=9))
    		isdigit=true;
    		else isdigit=false;
    		return isdigit;
    	}
    It could be replaced with:
    Java Code:
    	public static boolean isDigit(int x) {
    		return(x>=0 && x<=9);
    	}

  4. #4
    mxsar is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Sorry for the late reply but unfortunately my internet provider was on and off for more than a week... :mad:

    Both your replies helped a million and it worked like a charm!

    You saved the day for once more...

    Thank you!

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,568
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by mxsar View Post
    Both your replies helped a million and it worked like a charm!
    Please use the static method Character.digit( ... ) instead of all that primitive number fiddling. It's just as efficient, it's correct and it's far more readable.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. "parse error at or before "}""
    By constden in forum New To Java
    Replies: 32
    Last Post: 04-25-2009, 09:49 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •