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 ๐
Replies
-
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);
-
sam_from_hell
Excellent.Thank you ๐ ๐ ๐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);
You are reading an archived discussion.
Related Posts
Hello,
Can anybody tell me why Variable Compression ratio can be applied in 2 stroke engine but is facing difficulties in 4 stroke engine?
In short why Variable Compression ratio...
STED is going to organize a recruitment drive for GENPACT for its bpo services in association with Career Grew on feb 6th.
Job description:
Designation : Process Associate / Process...
๐ ๐
Happy Birthday Suyash! ๐
Suyash is one of the most talented CEans. Wishing you a successful life ahead!
๐
can we multicast in local area network?
hey guys, i want all of you to be my friend, please invite Me who ever read this one.
thank you and god bless!๐