CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: java error

Threaded View

  1. #11
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: java error

    With a Scanner you can check the token before reading it:
    Code:
    Scanner sc = new Scanner(System.in);
    int n;
    do {
    	System.out.append("Enter a number: ");
    	if (sc.hasNextInt()) {
    		n = sc.nextInt();
    		break;
    	} else {System.out.format("%nERROR: %s is not an integer number!%n%n", sc.nextLine());}
    } while (true);
    This will loop until the user types an integer number, rejecting strings or other numbers that cannot be read as integer.
    Last edited by jcaccia; September 15th, 2009 at 11:03 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
  •  





Click Here to Expand Forum to Full Width

Featured