Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: I am Facing problem in Scanner Class

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I am Facing problem in Scanner Class

    Dear Friends,
    If I enter the wrong input(example , if I enter String instead of Integer) loop is not ending, it wont get input next time. can you please help this?. Thanks in advance!!!

    import java.util.InputMismatchException;
    import java.util.Scanner;
     
    /**
     * If we enter the wrong input(example , if we enter sting instead of integer) it goes unending loop
     * 
     * @author Nithish
     *
     */
    public class Sample2 {
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    		for (int i = 0; i < 1; i++) {
    			try {
    				System.out.println("Enter the value");
    				int obj = scanner.nextInt();
    				System.out.println(obj);
    			} catch (InputMismatchException e) {
    				i--;
    				e.printStackTrace();
    			}
    		}
    	}
    }
    Last edited by KevinWorkman; October 11th, 2011 at 09:34 AM. Reason: please use highlight tags when posting code!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,283
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: I am Facing problem in Scanner Class

    From what I can tell, Scanner does not advance past that element if it throws an InputMismatchException. So you're going to keep bumping into the same value, which is going to throw the same Exception, over and over again.

    Also, it looks like you're looking for a do while loop, not a for loop. What you have might do the trick, but it sure does look funny.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: I am Facing problem in Scanner Class

    The loop doesn't end when you enter the wrong input because of i--;

    The wrong input causes the InputMismatchException to be thrown and the i--;
    is causing an infinate loop.

    Even by removing this, you won't be able to enter another value though as the InputMismatchException is thrown.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Java Web Browser Term Project- I am facing multiple problem...pls help
    By tazbinur in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 8th, 2010, 09:05 AM
  2. Basic Java File I/O with Scanner Class Problem
    By miss confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 26th, 2010, 08:04 AM
  3. Facing problem with posting Excel file for download - Content in browser
    By ragz_82 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: February 9th, 2010, 07:28 AM
  4. [SOLVED] Problem in Coin-counter with scanner class
    By coccoster in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 25th, 2009, 08:46 AM
  5. Replies: 1
    Last Post: May 13th, 2008, 08:08 AM