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 5 of 5

Thread: getting input from the user and give him some chances

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default getting input from the user and give him some chances

    Hi,

    I try to write piece of code that get input from the user and in case of wrong input give the user anoter chance (total of 3 tries) to type input again.
    what is the problem with it?

    this is the piece of code:

    while (tries < 5)
    {
    	try{
    		System.out.println("Enter height: ");
    		inp = Double.parseDouble(b.readLine());
      	        crr=1;
    		if(crr>=1)
    			break;
    	}
    	catch (Exception e){			
    		System.out.println("wrong input, try again");
    		continue;
            }
    	finally {
    		tries ++;
    	}
    }
    if (tries  == 3)
    	throw new Exception();
     
    catch (Exception e)
         System.out.println("exiting program");


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: getting input from the user and give him some chances

    Quote Originally Posted by felixb View Post
    what is the problem with it?
    ...What is wrong with it?
    Does it compile? Does it run? Does it give incorrect output?

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: getting input from the user and give him some chances

    it run, but incorrect.
    if i give 5 incorrect input - it run good, give 5 tries and exiting program after the 5 th attemp.
    if i give 4 incorrect and the last is correct - it give 4 tries, i the 5 th try i enter correct input but it still exiting program...

  4. #4
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: getting input from the user and give him some chances

    The finally block will always run wether the try block passes or fails. What your program is doing is adding one to each tries variable.

    So when its at 4 at the end of the loop it adds 1 more. If I am not mistaken your tries starts at 0 so this is whats happening:

    tries = 0;
    while (0 < 5) (1st Iteration)
    ... Execute code
    ... tries++; (1)
    while (1 < 5) (2nd Iteration)
    ... Execute code
    ... tries++; (2)
    ...etc
    while (4 < 5) (5th Iteration)
    ... Execute code with correct input
    ... tries++; (5)

    By modifying your finally block you can see this happening
    	finally {
    		tries ++;
                              // Display tries
                              System.out.println(tries);
    	}

    You will have to find a way to only add to tries while input is incorrect since it seems somewhere else in your code it takes tries into account.

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: getting input from the user and give him some chances

    ok, i moved it to the catch section.
    now it sees to be good.
    thnk you

Similar Threads

  1. User Input help
    By dx2731 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 13th, 2013, 12:46 AM
  2. User Input help
    By lanmonster in forum What's Wrong With My Code?
    Replies: 20
    Last Post: January 20th, 2013, 10:44 AM
  3. Take user input
    By frabbi.cse in forum Java Theory & Questions
    Replies: 1
    Last Post: July 22nd, 2012, 12:48 PM
  4. Replies: 1
    Last Post: September 17th, 2011, 10:28 AM