Help! - Java Problem
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):
If i run this program it shows up this error :
the command i used is
I use the Eclipse IDE for Win-32
-Thanks 😀
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: 0so what have i done wrong?
at java.lang.String.charAt(Unknown Source)
at calculator.main(calculator.java:14)
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