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

Thread: Need help with exception handling for a string

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with exception handling for a string

    I am writing a program that you have to enter either "R", "T", or "C" for some code to execute. I have everything running but cannot figure out how to handle if a different letter is entered or a number.....I want a reenter message to print and do again. I know how to use the numberFormat exception, but don't know how a string exception would be coded. Do I have to make my own? I tried using if statements, but that doesn't work. Heres the if's I had:

    //get input from the user
    System.out.println("Enter customer type (r/c/t): ");
    System.out.println();
    customerType = sc.next();

    //check for non r/t/c entry

    if(!customerType.equalsIgnoreCase("R")){
    if(!customerType.equalsIgnoreCase("T"))
    if(!customerType.equalsIgnoreCase("C"))

    System.out.println("Re-enter");

    }else{

    System.out.println("Enter subtotal: ");
    System.out.println();
    double subtotal = sc.nextDouble();
    this only works for R entry.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with exception handling for a string

    See cross post at : Need help with exception handling for a string

Similar Threads

  1. [SOLVED] Embed a Jar into a website/Exception handling
    By Hallowed in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 07:34 AM
  2. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  3. Output String Handling in Java
    By merry in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 21st, 2010, 09:59 PM
  4. Exception handling for TextFields
    By FretDancer69 in forum AWT / Java Swing
    Replies: 1
    Last Post: June 16th, 2009, 07:48 AM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM