CrazyEngineers
  • Help! - Java Problem

    Updated: Oct 26, 2024
    Views: 1.1K
    Hey Guys!
    I started on Java Programming yesterday(on my own), and im having a bit of trouble..
    Here is the code i wrote(bear with me please, i only started yesterday):
    import java.util.*;
    public class calculator {
        
        public static void main(String[] args) {
            Scanner ip = new Scanner(System.in);
            double fnum , snum, ans;
            char resp;
            
            System.out.println("Enter the first number:");
            fnum = ip.nextDouble();
            System.out.println("Enter the second number");
            snum = ip.nextDouble();
            System.out.println("Enter the operation: [+,-,*,/]");
            resp = ip.nextLine().charAt(0);
            if(resp == '+'){
                ans = fnum + snum;
                System.out.println("Answer is "+ ans + ".");
            }
        }
    
    }
    
    Right, this program is a simple calculator. I have completed only a part of it(addition only 😛 ).
    If i run this program it shows up this error :
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at calculator.main(calculator.java:14)
    so what have i done wrong?
    the command i used is
    resp = ip.nextLine().charAt(0);
    is for taking the first character enterd isnt it?(im using this bcoz there is no '.nextChar();')
    I use the Eclipse IDE for Win-32
    -Thanks 😀
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Vengeance.Inc

    MemberFeb 2, 2011

    Read the javadoc comments for the nextLine method of the Scanner class. It reads :

    Advances this scanner past the current line and returns the input that was  skipped. This method returns the rest of the current line, excluding any line  separator at the end. The position is set to the beginning of the next line. 
    So this means the scanner is skipping the first line of input and looking for the second. when you've typically entered only 1 character in the first line. This returns an empty string, which is failing on charAt(0). use the next() method instead of nextLine() which returns a String.

    resp = ip.next().charAt(0);
    Are you sure? This action cannot be undone.
    Cancel
  • sam_from_hell

    MemberFeb 2, 2011

    Vengeance.Inc
    Read the javadoc comments for the nextLine method of the Scanner class. It reads :

    Advances this scanner past the current line and returns the input that was  skipped. This method returns the rest of the current line, excluding any line  separator at the end. The position is set to the beginning of the next line. 
    So this means the scanner is skipping the first line of input and looking for the second. when you've typically entered only 1 character in the first line. This returns an empty string, which is failing on charAt(0). use the next() method instead of nextLine() which returns a String.

    resp = ip.next().charAt(0);
    Excellent.Thank you 😀 😀 😁
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register