Thursday, February 3, 2011

CrazyEngineers Forum - Computer Science & IT Engineering

CrazyEngineers Forum - Computer Science & IT Engineering


Help! - Java Problem

Posted: 02 Feb 2011 08:25 PM PST

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):
Code:

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 :P ).
If i run this program it shows up this error :
Quote:

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
Code:

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 :)

All about artificial intelligence

Posted: 02 Feb 2011 10:14 AM PST

Let us discuss about artificial intelligence here

Please share anything you know about this topic .It may include definition even

file management

Posted: 02 Feb 2011 03:40 AM PST

please tell me the meaning of supporting primitives for manipulating files and directories with respect to the operation of operating system in file management .......

No comments:

Post a Comment