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

Thread: User selection problem

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Cool User selection problem

    Hey,

    So this is my code for my getChoice ( ) method.
    private int getChoice ( int lower, int upper ) {
     
    		System.out.println ( "--------------------------------------------------------------" );
    		System.out.print   ( "Please enter your choice: ");
    		String stringSelection = Keyboard.readInput ( ); 
    	        System.out.println ( "--------------------------------------------------------------" );
    	            if ( stringSelection.length ( ) == 0 ) {
    	    	       return INVALID;
    	             } else {
    	    		    int selection = Integer.parseInt ( stringSelection );
    	    		    if ( selection >= lower && selection <= upper ) { 
    	    			return selection;
    	    		    } else {	 
    	    			return INVALID;
    	    		}
    	    } 
    	}

    It's meant to take an input, and then if it's not between or equal to lower and upper (in this case the method is being used for lower int = 1, and upper = 4)
    then it will return my constant INVALID variable. My Keyboard.readInput() method returns a String as you can see.

    At the moment I have it set up so that if the input is empty (now that I think of it I could have just said null instead of .length() ) then it will return invalid.
    And if the INTEGER is not between lower and upper, then it will return invalid. But say if I was to type "a22asdf" or something else random, then it will give me an error.
    So my question, is how can I make it return INVALID if anything entered as a choice is not between or equal to lower and upper.
    Probably a very silly thing that I will understand as soon as someone helps me out. One of those things

    (I was thinking of an array and a for loop that cycled through int i)

    Thank you very much and regards,
    Max
    Last edited by zipstacrack; May 3rd, 2012 at 10:58 PM.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: User selection problem

    Quote Originally Posted by zipstacrack View Post
    At the moment I have it set up so that if the input is empty (now that I think of it I could have just said null instead of .length() ) then it will return invalid.
    And if the INTEGER is not between lower and upper, then it will return invalid. But say if I was to type "a22asdf" or something else random, then it will give me an error.
    So my question, is how can I make it return INVALID if anything entered as a choice is not between or equal to lower and upper.
    Probably a very silly thing that I will understand as soon as someone helps me out. One of those things

    (I was thinking of an array and a for loop that cycled through int i)

    Thank you very much and regards,
    Max
    Hello Max!
    If I'm getting it right you want the method to return INVALID when it throws an exception. I think you can do that by surrounding the part of code that may through the exception (in your code the parsing statement and inner if...else's block) with a try...catch block and when you "catch" the exception you can return INVALID.
    Hope it helps.

  3. The Following User Says Thank You to andreas90 For This Useful Post:

    zipstacrack (May 4th, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: User selection problem

    Quote Originally Posted by andreas90 View Post
    Hello Max!
    If I'm getting it right you want the method to return INVALID when it throws an exception. I think you can do that by surrounding the part of code that may through the exception (in your code the parsing statement and inner if...else's block) with a try...catch block and when you "catch" the exception you can return INVALID.
    Hope it helps.
    Thank you very much for your reply,

    I am not familiar with try...catch.
    Could you possibly give me a small example of how it would work with this/or something similar to this?

    Regards,
    Max

  5. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: User selection problem

    Quote Originally Posted by zipstacrack View Post
    Thank you very much for your reply,

    I am not familiar with try...catch.
    Could you possibly give me a small example of how it would work with this/or something similar to this?

    Regards,
    Max
    try {
            //code that may throw the exception, the parsing method
          } catch (ExceptionThatMayBeThrown e) {
                    e.getStackTrace();             
                    return INVALID;
          }
    To understand which return statement is executed (since INVALID will be returned in multiple cases) I would also suggest you print a message before you return anything .

  6. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: User selection problem

    Quote Originally Posted by andreas90 View Post
    try {
            //code that may throw the exception, the parsing method
          } catch (ExceptionThatMayBeThrown e) {
                    e.getStackTrace();             
                    return INVALID;
          }
    To understand which return statement is executed (since INVALID will be returned in multiple cases) I would also suggest you print a message before you return anything .
    Getting there I think. This is my code now (I'm confused about the "exceptionthatmaybethrown")
    private int getChoice ( int lower, int upper ) {
     
    		System.out.println ( "--------------------------------------------------------------" );
    		System.out.print   ( "Please enter your choice: ");
    		String stringSelection = Keyboard.readInput ( ); 
    	    System.out.println ( "--------------------------------------------------------------" );
    	    if ( stringSelection.length ( ) == 0 ) {
    	    	return INVALID;
    	    } else {
    	    		int selection = Integer.parseInt ( stringSelection );
    	    		try {
    	    			if ( selection >= lower && selection <= upper ) { 
    		    			return selection;
    		    		}
    	    		} catch ( java.util.InputMismatchException e ) {
    	    			e.getStackTrace ( );
    	    			return INVALID;
    	    		}
    	    }

    Now I am getting an error that this needs to return something, which it does, so somehow the returns are not being reached.

    Sorry, this is the first time I have attempted error handling in Java. (other than a few simple if statements in previous works.)

    Regards,
    Max

    EDIT: Updated code (I think I am getting closer) (but eclipse is still saying I need a return statement)
    private int getChoice ( int lower, int upper ) {
     
    		System.out.println ( "--------------------------------------------------------------" );
    		System.out.print   ( "Please enter your choice: ");
    		String stringSelection = Keyboard.readInput ( ); 
    	    System.out.println ( "--------------------------------------------------------------" );
     
    	    try {
    	    	int selection = Integer.parseInt ( stringSelection );
     
    	    	if ( selection >= lower && selection <= upper ) {
    	    		return selection;
    	    	}
    	    } catch ( java.util.InputMismatchException e ) {
    	    	return INVALID;
    	    }
    	}
    Last edited by zipstacrack; May 4th, 2012 at 05:44 AM.

  7. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: User selection problem

    Quote Originally Posted by zipstacrack View Post
    Getting there I think. This is my code now (I'm confused about the "exceptionthatmaybethrown")
    "exceptionthatmaybethrown" was just fordemonstration. You should catch the exception that actually may be thrown which in your case is the NumberFormatException.


    The try block should contain the statement that may throw the exception. In your code the exception will may be thrown from this statement
    int selection = Integer.parseInt ( stringSelection );
    For example if the user enters a letter the method won't parse it to an int since it doesn't have the appropriate format (see the details in the above link). The inner if...else block won't throw an exception so it doesn't need to be indide the try block.
    You should read the java tutorials for exceptions handling.

  8. The Following User Says Thank You to andreas90 For This Useful Post:

    zipstacrack (May 4th, 2012)

  9. #7
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: User selection problem

    Quote Originally Posted by zipstacrack View Post
    EDIT: Updated code (I think I am getting closer) (but eclipse is still saying I need a return statement)
    It "complains" becaused you took away the else block and therefore there is case that the method will not return anything. For example if the selection is out of the entered bounds.

  10. The Following User Says Thank You to andreas90 For This Useful Post:

    zipstacrack (May 4th, 2012)

  11. #8
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: User selection problem

    Quote Originally Posted by andreas90 View Post
    "exceptionthatmaybethrown" was just fordemonstration. You should catch the exception that actually may be thrown which in your case is the NumberFormatException.


    The try block should contain the statement that may throw the exception. In your code the exception will may be thrown from this statement
    int selection = Integer.parseInt ( stringSelection );
    For example if the user enters a letter the method won't parse it to an int since it doesn't have the appropriate format (see the details in the above link). The inner if...else block won't throw an exception so it doesn't need to be indide the try block.
    You should read the java tutorials for exceptions handling.
    If I was to not include the if statement in the try statement how would I check if the integers were between the lower and upper local variables?
    It still requires a return type apparently.

    I really appreciate your help, thanks,
    Max

    EDIT: I GOT IT! I always feel so good once a problem that I've been working on for a long time is solved.

    Here is my updated code if anyone finds it useful:
    	private int getChoice ( int lower, int upper ) {
     
    		System.out.println ( "--------------------------------------------------------------" );
    		System.out.print   ( "Please enter your choice: ");
    		String stringSelection = Keyboard.readInput ( ); 
    	    System.out.println ( "--------------------------------------------------------------" );
    	    try {
    	    	int selection = Integer.parseInt ( stringSelection );
    		    if ( selection >= lower && selection <= upper ) {
    	    		return selection;
    	    	} else { 
    	    		return INVALID;
    	    	}
    	    } catch ( NumberFormatException e ) {
    	    	e.getStackTrace();
    	    	return INVALID;
    	    }	    	
     
    	}

    Thank you very much for your help

    Regards and out,

    Max
    Last edited by zipstacrack; May 4th, 2012 at 06:05 AM.

  12. #9
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: User selection problem

    Glad you got it work.
    Just for the record, you can take the input evaluation (if...else block) outside the try...catch block since in the latter you are just checking for the exception.

  13. The Following User Says Thank You to andreas90 For This Useful Post:

    zipstacrack (May 4th, 2012)

  14. #10
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: User selection problem

    Done. Thank you very much.

Similar Threads

  1. Calendar from user input PROBLEM!
    By Exiled in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 5th, 2013, 10:18 AM
  2. user input mismatch problem
    By JavaN00b101 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 9th, 2012, 07:02 AM
  3. [SOLVED] problem in simple program, new user
    By musterdplug12 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 04:11 PM
  4. Dynamically Creating User Selection through ResultSet
    By anmaston in forum Java Theory & Questions
    Replies: 3
    Last Post: April 10th, 2011, 11:08 AM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM