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: String index out of range?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String index out of range?

    I'm supposed to be making a calculator that will add, subtract, etc. two integers. It's supposed to prompt to enter in what the person would like to do endlessly, then terminate when "exit" is entered.
    And yes, it's all supposed to be in a while loop.

    There are two problems (maybe) with my code.
    First, whenever I add, subtract, etc. the ints, it will give me a result. After the result, however, I'm getting an error saying there's a StringIndexOutofBoundsException at line 23: if(low.charAt(zero) == 'a')
    I have no idea why.
    Second, when I divide, it's returning the value as an int. I tried casting, but I couldn't get it to come out right.

    Thanks in advance.



    import java.util.Scanner;
    public class calculator
    {
        public static void main(String args[]){
     
          Scanner in = new Scanner(System.in);
          String entered,low;
          int dur = 0;
          int first, second, zero= 0;
          int answer = 0;
          String ex = "exit";
          double divide;
     
          while(dur == 0){
            System.out.println("A) Add");
            System.out.println("B) Subtract");
            System.out.println("C) Multiply");
            System.out.println("D) Divide");
            System.out.println("Enter 'exit' to close the program");
            entered = in.nextLine();
            low = entered.toLowerCase();
     
            if(low.charAt(zero) == 'a')
            {System.out.println("Enter in the first integer: ");
                first = in.nextInt();
                System.out.println("Enter in the second integer: ");
                second = in.nextInt();
                answer = add(first,second);
                System.out.println(answer);}
     
            else if(low.charAt(zero) == 'b')
            {System.out.println("Enter in the first integer: ");
                first = in.nextInt();
                System.out.println("Enter in the second integer: ");
                second = in.nextInt();
                answer= sub(first,second);
                System.out.println(answer);}
     
            else if(low.charAt(zero) == 'c')
            {System.out.println("Enter in the first integer: ");
                first = in.nextInt();
                System.out.println("Enter in the second integer: ");
                second = in.nextInt();
                answer = mult(first,second);
                System.out.println(answer);}
     
            else if(low.charAt(zero) == 'd')
            {System.out.println("Enter in the first integer: ");
                first = in.nextInt();
                System.out.println("Enter in the second integer: ");
                second = in.nextInt();
                divide = div(first,second);
                System.out.println(divide);}
     
            else if(low.equals(ex))
            {System.out.println("Ending program");
            dur = 1;}
     
            else
            {System.out.println("Invalid input. Try again.");}
          }
        }
          public static int add(int input1,int input2)
        {int sum;
        sum = (input1+input2);
        return sum;}
     
        public static int sub(int input1,int input2)
        {int equals;
        equals = (input1-input2);
        return equals;}
     
        public static int mult(int input1,int input2)
        {int prod;
        prod = (input1*input2);
        return prod;}
     
        public static double div(int input1,int input2)
        {double equ;
        equ=(input1/input2);
         return equ;}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: String index out of range?

    What is the value of low when you're trying to call charAt() on it? What value are you passing in to that function? Make sure by using a debugger or adding a print statement.

    And int operations always result in an int answer, regardless of how you cast that answer. One trick is to cast the actual int values before using them in the operation, instead of trying to cast the answer after it has already been calculated.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Help with my Range Finder programme
    By kidza12 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 01:53 PM
  2. String Index out of range
    By petemyster in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2010, 03:59 PM
  3. 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
  4. [SOLVED] Help; algorithm to determine 'range'
    By b_jones10634 in forum Algorithms & Recursion
    Replies: 13
    Last Post: August 24th, 2010, 04:57 PM
  5. set the slider's models (range)
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 28th, 2009, 11:59 PM