-4

In the 15th line ch = s1.charAt(0);, why ch is not getting the 0th word of s1, i.e., the operator .

I've tried without using the try-catch method but then the error was regarding the exception

and now no exception, no errors but the program don't ask for operator and directly after inputting the 1st and 2nd value , it shows the exception"can't do that"

please post your kind replies, thanks

import java.util.Scanner;

class apples {
    public static void calcu() {
        try{
            int a, b;
            String s1;
            char ch;
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter the 1st value : ");
            a = sc.nextInt();
            System.out.print("Enter the 2nd value : ");
            b = sc.nextInt();
            System.out.print("Enter the operator : ");
            s1 = sc.nextLine();
            ch = s1.charAt(0);
            System.out.println("yo");

                switch(ch){
                case '+' : System.out.print("sum is " + (a+b));
                case '-' : System.out.print("Substraction is : " +(a-b));
                case '*' : System.out.print("Multiplication is : " + (a*b));
                case '/' : System.out.print("Multiplication is : " + (a/b));
                default  : System.out.print("wtf yo");
                }
        }
        catch(Exception e) {
            System.out.println("cant do that ");
        }

    }

    public static void main(String args[]) {
        apples obj = new apples();
        obj.calcu();
    }
}
Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
  • We don't know what your input is, or your expected output. The fact that you're not bothering to log what the exception *is* doesn't help, either... please include the exception details in your logging (e.g. `System.out.println("Error: " + e);`) – Jon Skeet Dec 23 '14 at 11:56
  • Im just making normal calculator sir, and was learning about how to input the character variable – Takat Singh Dec 23 '14 at 11:59
  • That doesn't help us diagnose the problem though - it doesn't tell us what you entered, or anything about the exception. – Jon Skeet Dec 23 '14 at 12:04
  • possible duplicate of [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx) – Tom Dec 23 '14 at 12:16
  • hmm... thanks sir can u tell me please , that the parseInt method fall under which category and where could i learn it ? – Takat Singh Dec 23 '14 at 13:53

1 Answers1

3

You should replace nextInt with nextLine :

        System.out.print("Enter the 1st value : ");
        a = Integer.parseInt(sc.nextLine());
        System.out.print("Enter the 2nd value : ");
        b = Integer.parseInt(sc.nextLine());
        System.out.print("Enter the operator : ");
        s1 = sc.nextLine();
        ch = s1.charAt(0);

When s1 = sc.nextLine(); follows b = sc.nextInt(), it returns an empty String, since it returns the end of the line that contained that int. When you try to get the first character of an empty String, you get an IndexOutOfBoundsException.

Eran
  • 387,369
  • 54
  • 702
  • 768