Results 1 to 7 of 7
  1. #1
    ecandrews0 is offline Member
    Join Date
    Nov 2014
    Posts
    3
    Rep Power
    0

    Default Exception Handling for strings?

    For one of my last labs for the semester, my professor is having the class go back to our very first program and apply some of the exception handling that we just recently learned about. Here's my improved code so far:

    Java Code:
    import java.util.*;
    import java.lang.*;
    
    public class Lab2Part1 {
    
        public static void main (String [] args) {
    	Scanner input = new Scanner (System.in);
    
    	String name = "";
    	boolean continueInput = true;
    
    	do {
    	    System.out.print("Please enter your name: ");
    
    	    try {
    		name = input.nextLine();
    		continueInput = false;
    		
    	    }
    	    catch(InputMismatchException ex) {
    		System.out.println("Oops! Please enter your name.");
    		input.nextLine();
    	    }
    
    	} while(continueInput);
    
    	System.out.println("Hello " + name + "!");
    
    
    
        }
    
    }

    My code compiles fine, but even if I enter an integer or a double, it saves the number as a string, and prints that out as the name. Is there any way to get around this? Or do I need to use something besides a try-catch?

    Btw, I'm not asking for anyone to do my homework for me. I would just like a pointer in the right direction. :)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Exception Handling for strings?

    Can you post the program's output and add some comments showing what you want the output to look like?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Exception Handling for strings?

    You will get an exception when the input doesn't match the expected type. So ask yourself. What does nextLine return? What does name expect?
    And what does the second call to nextLine return?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    ecandrews0 is offline Member
    Join Date
    Nov 2014
    Posts
    3
    Rep Power
    0

    Default Re: Exception Handling for strings?

    Can you post the program's output and add some comments showing what you want the output to look like?
    Basically, right now if I enter a 7, this is the output I get:

    Java Code:
    Please enter your name: 7
    Hello 7!
    When instead, I would like to be able to enter a number, have it throw the exception (I think that's the right phrase to use?), and then prompt me to enter a string instead. I'm guessing the problem is occurring because it is storing the number as a string? And thus no exception is found? I'm a little stuck on this. :/

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Exception Handling for strings?

    Everything you read from a keyboard /console is character. It is very hard to enter int values with a keyboard. The Scanner class's methods convert the characters to different data types like String or int.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Exception Handling for strings?

    Quote Originally Posted by ecandrews0 View Post
    When instead, I would like to be able to enter a number, have it throw the exception (I think that's the right phrase to use?), and then prompt me to enter a string instead.
    That would be impossible since "7" is also a very valid string - and I'm sure in some corner of the world it is also a very valid name. There is no exception to catch, this is all functionally and technically correct.

    Your real requirement is that you do not want to allow the user to input numbers - that is application business logic, rules that you yourself impose. You can't expect Java to handle that for you, you'll have to build the logic to impose those rules by checking ("validating") what the user inputs. On that subject: so what other characters is the user not allowed to input for his/her name?


    PS: as soon as you start to filter on this, your program discriminates: Falsehoods Programmers Believe About Names | Kalzumeus Software

    This is all about learning programming of course and thus it is fine, just don't take bad practices to the real world ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    ecandrews0 is offline Member
    Join Date
    Nov 2014
    Posts
    3
    Rep Power
    0

    Default Re: Exception Handling for strings?

    That makes sense. Thank you so much! I really appreciate your help. :)

Similar Threads

  1. Exception handling
    By Moedig in forum New To Java
    Replies: 2
    Last Post: 11-10-2011, 10:43 PM
  2. Handling long strings - Advice needed
    By Codercode in forum Forum Lobby
    Replies: 0
    Last Post: 01-02-2011, 12:43 AM
  3. Handling Multi byte Character Strings
    By madhav1351 in forum Advanced Java
    Replies: 2
    Last Post: 09-22-2010, 12:55 PM
  4. [SOLVED] Handling strings including " character
    By flywheel in forum New To Java
    Replies: 5
    Last Post: 09-29-2008, 03:45 AM
  5. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 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
  •