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: Help needed for string index out of bounds exception

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help needed for string index out of bounds exception

    I am fairly new at this and any help would be appreciated . thx

    private static int doFarenheitCelsius()
    {
    util.writeln("\nYou chose Farenheit or Celsius");

    Scanner reader = new Scanner(System.in);
    double temp = 0;

    double convertedF = 0;
    double convertedC = 0;
    DecimalFormat df = new DecimalFormat("#.##");
    util.writeln("Enter a temperature ");
    temp = reader.nextInt();

    String strInput = "0";

    util.writeln("Convert to Farenheit or Celsius");
    strInput = reader.nextLine();
    char letter = strInput.charAt(0);

    if (letter == 'F' && letter == 'f')
    {
    convertedF = temp * 9/5 + 32;
    util.writeln(temp + "is " + df.format(convertedF) + " Degrees Ferenheit");
    }
    else if (letter == 'C' && letter == 'c')
    {
    convertedC = (temp - 32) * 5 / 9;
    util.writeln(temp + "is " + df.format(convertedC) + " Degrees Celsius");
    }

    return 0;

    }
    and my error message is as below, seems like a simple method to execute


    You chose Farenheit or Celsius
    Enter a temperature
    45
    Convert to Farenheit or Celsius
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at exercises.SuperCalculatorAssignmentFive.doFarenhei tCelsius(SuperCalculatorAssignmentFive.java:132)
    at exercises.SuperCalculatorAssignmentFive.main(Super CalculatorAssignmentFive.java:73)


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help needed for string index out of bounds exception

    Please read the Announcement topic at the top of the sub-forum to learn how to post code in code or highlight tags and other useful info for newcomers.

    The statement:

    temp = reader.nextInt();

    takes the numerical part of the user's input and leaves the linefeed character in the input buffer. Then, the statement:

    strInput = reader.nextLine();

    sees there is still a linefeed in the input buffer and accepts it as the input. The result is that strInput is an empty string with no length, hence the error.

    To correct, flush the buffer after the first statement with a throwaway call to reader:

    reader.nextLine();

    Let me know if this is not clear.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Help needed for string index out of bounds exception

    reader.nextLine(); must be returning an empty String. Do you press enter or does this just happen immediate after the "Convert to Farenheit or Celsius" text is printed out? If you are pressing enter, there is your problem. If not, I'm not really sure why that would be happening. Perhaps something weird is going on in your util.writeln(String) method which cases the Scanner to think user input occurred.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help needed for string index out of bounds exception

    thanks GregBrannon, that reader.nextLine(); cleared the mismatch error but doesn't do my loop. I'll work on that. and I assume by the link you gave me, I posted incorrectly . Sorry about that thought I was in the right area.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help needed for string index out of bounds exception

    You posted in the right area. You didn't post your code using tags to make it readable.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 3
    Last Post: March 23rd, 2013, 07:20 PM
  2. [SOLVED] Array Index out of bounds Exception
    By Tohtekcop in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2012, 03:03 PM
  3. ArrayList initial capacity problem (Index out of bounds Exception)
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 11:24 AM
  4. String index out of bounds error 5???
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2010, 07:11 PM
  5. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 12:19 PM