0

I am learning Java and just been working on creating a little calculator. I am writing one what will allow me to put any number of digits in until I press an equals sign at which point I want the calculator to display the total.

I think that there is a problem with the scanner as while debugging I only get as far as Scanner input = new Scanner (System.in): In the debugger it say Source not found. This is really strange especially as I am using two Scanners the other class within the same project with no problems at all. As you can tell I used to have 2 scanners in this piece but I read that this should not work so I am now using one. Here is the code...

package Calculator;

import java.util.Scanner;

public class Calculator3 {

    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
    //  Scanner opin = new Scanner (System.in);     
        String operative = input.next();        
        double numb = input.nextDouble();
        int answer = 0;
        int calc = 1;

        System.out.print("#######################################" + "\n");             
        while (operative.equalsIgnoreCase("="))
            {
            System.out.print("Interger " + calc + " :");
            System.out.print("Type your Operative :");
            if (operative.equals("+")) 
                answer += numb;
            {
                if (operative.equals("-")) 
                    answer -= numb;
                    {
                        if (operative.equals("/")) 
                            answer /= numb;
                            {
                                if (operative.equals("*")) 
                                    answer *= numb;
                                {
                                }
                            }
                    }
            }
        calc += 1;  
            }
        System.out.print("#######################################" + "\n");     
        System.out.println("Your answer is: " + answer + ".");
}
}
Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
Explicitsoul
  • 149
  • 2
  • 10
  • You probably are trying to step-in, so it's complaining because it's not able to find the source for `Scanner` or `System`), while debugging you should step-over. – Bhesh Gurung Nov 06 '12 at 23:53
  • Take a moment to read about switch/case statements. It'll help you tighten up that code. – Marvo Nov 06 '12 at 23:56

1 Answers1

0

The issue with your code is that the operative is not updated, and probably not = after the first read. Because of this your loop will not exit and the print statement will not be reached. Following your logic, you need to add read statements in your loop. Also I suggest using a switch, will make your more readable and easy to maintain.

Also note that while debugging you will get "Source not found" for classes for which your debugger is not able to detect the source. Probably your JDK configuration is missing the source entry. See this answer for details on what you need to check to fix your JDK config, if you are using Eclipse.

Community
  • 1
  • 1
dan
  • 13,132
  • 3
  • 38
  • 49