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

Thread: my do while isn't behaving (beginner)

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default my do while isn't behaving (beginner)

    I get a " java.lang.StringIndexOutOfBoundsException: String index out of range: 0". This seems like it should be too easy to mess up but here I am. Any help would be awsome, Thanks
    import java.util.*;
    public class classPractice2_10
    {
      public static void main(String[]args)
      {
        Scanner kb = new Scanner(System.in);
     
        double length, width, area, perimeter;
        char repeat;
        String input;
     
       do
       {
        System.out.println("Please enter the length of the room. ");
        length = kb.nextDouble();
     
        System.out.println("Please enter the width of the room. ");
        width = kb.nextDouble();
     
        area = length * width;
        perimeter = (length + length) + (width + width);
     
        System.out.printf("The area of your room is %.3f and the perimeter of your room is %.3f \n",area,perimeter);
     
        System.out.println("Do you wish to calculate another room? ");
        System.out.print("Enter Y for yes and N for no ");
        input = kb.nextLine();
        repeat = input.charAt(0);
       }while (repeat == 'y' || repeat == 'Y');
      }
    }


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

    Default Re: my do while isn't behaving (beginner)

    Please copy the full text of the error message and paste it here. It has important info about the error.

    If the index of 0 is out of bounds, then the String must be empty.
    The Scanner's nextLine() method can read an empty String if used following another next type of method that didn't read the endline character.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: my do while isn't behaving (beginner)

    Thanks for being so quick.

    java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at classPractice2_10.main(classPractice2_10.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)
    >

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: my do while isn't behaving (beginner)

    See the end of post#2 re nextLine()
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: my do while isn't behaving (beginner)

    Ok, so Ive been staring at post #2 and I dont get what your telling me. I don't want an empty string, I was hoping for another message to prompt my user for an answer to the continue. lol sorry for being hard headed

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: my do while isn't behaving (beginner)

    The Scanner class's method nextDouble() leaves a endline char in the Scanner's buffer that the nextLine() method reads and returns as an empty String. To get the user's input, another call to nextLine() must be made.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: my do while isn't behaving (beginner)

    Always remember that the user might actually enter an empty line. Better check for this scenario in your code too.

  8. #8
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: my do while isn't behaving (beginner)

    Ok, thanks norm that does work but it seems weird that i have to do that is that normal? good point Cornix, my solution to your empty line doesnt work. Any ideas on how to correct that?
    import java.util.*;
    public class classPractice2_10
    {
      public static void main(String[]args)
      {
        Scanner kb = new Scanner(System.in);
     
        double length, width, area, perimeter;
        char repeat;
        String input = "";
     
       do
       {
        System.out.println("Please enter the length of the room. ");
        length = kb.nextDouble();
     
        System.out.println("Please enter the width of the room. ");
        width = kb.nextDouble();
     
        area = length * width;
        perimeter = (length + length) + (width + width);
     
        System.out.printf("The area of your room is %.3f and the perimeter of your room is %.3f \n",area,perimeter);
     
        System.out.println("Do you wish to calculate another room? ");
        System.out.print("Enter Y for yes and N for no ");
        input = kb.nextLine();
        input = kb.nextLine();
        repeat = input.charAt(0);
         if (input == " ");
        {
          System.out.println("Please Enter Y or N");
        }
       }while (repeat == 'y' || repeat == 'Y');
      }
    }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: my do while isn't behaving (beginner)

    The String class has methods for testing if a String is empty, use one of them.
     if (input == " ");
    Several problems with that line of code:
    Use the equals() method when comparing the contents of two Strings, not the == operator.
    I don't think the normal Scanner methods can return a space: " "
    The ; after the ) ends the if statement. The code in the {} following the if is not part of the if.
    If you don't understand my answer, don't ignore it, ask a question.

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

    meangreen2003 (June 3rd, 2014)

Similar Threads

  1. Excel RATE() function in Java behaving a bit differently
    By capricorn86 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 23rd, 2014, 06:18 AM
  2. Replies: 0
    Last Post: March 6th, 2014, 01:17 PM
  3. Counter behaving oddly, skipping large blocks of code& error message
    By Hagstrom505 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: January 28th, 2013, 08:43 PM
  4. [SOLVED] NoSuchElementException(), simple program not behaving right with Scanner class.
    By mwebb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2011, 06:41 PM
  5. [SOLVED] If, else if, else statement not behaving as expected
    By techwiz24 in forum Loops & Control Statements
    Replies: 5
    Last Post: April 15th, 2011, 12:02 PM