Help! - Java Problem

sam_from_hell
@sam-from-hell-8gE2gC โ€ข Oct 26, 2024

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 ๐Ÿ˜€

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Vengeance.Inc

    @vengeanceinc-BKdklo • Feb 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);
  • sam_from_hell

    @sam-from-hell-8gE2gC • Feb 2, 2011

    Vengeance.IncRead 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 ๐Ÿ˜€ ๐Ÿ˜€ ๐Ÿ˜