Results 1 to 9 of 9
  1. #1
    LabcoatMC is offline Member
    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

    Default Java Calculator Problem

    Hi guys, I'm a learner right now, and I need some help with this calculator program which is throwing an error in eclipse as soon as I run it.
    Can someone tell me what I am doing wrong? Thank you.

    Java Code:
    package calcpackage;
    
    import java.util.Scanner;
    
    public class TwoNumberCalculator1Ln {
    	
    	public static void main(String args[]) {
    		
    		Scanner input = new Scanner(System.in);
    		double answer = 1;
    		System.out.println("Welcome to this calculator program");
    		
    		while (true) {
    			
    			System.out.println("Please enter a calculation in this format: <number1> <valid operator> <number2>");
    			
    			double num1 = input.nextDouble();
    			
    			String operator = input.next();
    			
    			double num2 = input.nextDouble();
    			
    			switch (operator) {
    			
    			case "x":
    				answer = num1 * num2;
    				System.out.println("The answer is: " + answer);
    				break;
    				
    			case "+":
    				answer = num1 + num2;
    				System.out.println("The answer is: " + answer);
    				break;
    				
    			case "-":
    				answer = num1 - num2;
    				System.out.println("The answer is: " + answer);
    				break;
    				
    			case "/":
    				answer = num1 / num2;
    				System.out.println("The answer is: " + answer);
    				break;
    				
    			default:
    				System.out.println("You did not enter a valid operator to use. The four supported are *, +, -, /");
    				break;
    			}
    		}
    		
    	}
    
    }
    The error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at com.avaje.ebeaninternal.server.lib.sql.Prefix.main (Prefix.java:40)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,936
    Rep Power
    4

    Default Re: Java Calculator Problem

    Well, by all appearances, this is not the program you ran. What steps are you taking to run it? Normally, I just right click in the window of the app with the main entry point and choose something like "run as java application."

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    LabcoatMC is offline Member
    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

    Default Re: Java Calculator Problem

    Quote Originally Posted by jim829 View Post
    Well, by all appearances, this is not the program you ran. What steps are you taking to run it? Normally, I just right click in the window of the app with the main entry point and choose something like "run as java application."

    Regards,
    Jim
    I just usually run it with the button at the top that when you hover over it says run. I don't see what's the difference though and why it would be any different to "Run as Java Application" which btw is the only option upon right click.
    But yes it did work that time so thanks!

  4. #4
    LabcoatMC is offline Member
    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

    Default Re: Java Calculator Problem

    Also, how would you check that the two numbers entered are valid? I don't think you can use a switch statement for so many possible numbers!
    Thanks.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,936
    Rep Power
    4

    Default Re: Java Calculator Problem

    Check the API for Scanner. Incorrect input will throw an exception.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    ShadowWalker is offline Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    96
    Rep Power
    0

    Default Re: Java Calculator Problem

    Guys .. this program run very vell to say the truth in my computer... i just remove the first line.. "package calcpackage;" check pls and tell me..
    it's working even if you give only one input for example when the program output this "Please enter a calculation in this format: <number1> <valid operator> <number2>"" even if i put 2 + 3 or :
    2
    +
    3
    the result is correct ;)
    but something really strange to me is when i try to do 3 / 0 or -3/0 this should give an error because if i have right there is no if statement to prevent that action.. but when i try it into the program all was correct and the output was "the result is Infinity " or in case two "The result is -infinity" .. hmm really strange..




    I Just find out what is going on.. and that's a very nice tip for those who don't know this .. (Like me)
    if you do this action in hava 3 / 0 this will cause this error :
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at TwoNumberCalculator1Ln.main(TwoNumberCalculator1Ln .java:5)
    this happening when the number (or variables ar integers) but when the values are double the result will be Infinity.. amazing.. i didn't know that ;)
    Last edited by ShadowWalker; 07-07-2013 at 12:50 PM.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,007
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Calculator Problem

    There is nothing strange about it: if you divide two ints (or longs etc) where the denominator equals zero, an Exception is thrown; if you do the same with floating point numbers, no Exception is thrown but the result is Infinity with the correct sign; there's also the number NaN (Not a Number); see a description of it all in the API for the Double class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    ShadowWalker is offline Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    96
    Rep Power
    0

    Default Re: Java Calculator Problem

    Very useful! i have see about the Nan Number but i didn't know about the division of two doubles.. but that's the good about java.. learning new things everyday ;)

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,007
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Calculator Problem

    Be careful with that NaN; it is not a number by any means; e.g. NaN != NaN although the bit patterns for those NaNs are the same. Java even simplified the meaning of NaN; in reality there are many, many NaNs; Java uses only one.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Problem with calculator
    By SlickJava in forum New To Java
    Replies: 4
    Last Post: 12-19-2012, 02:19 AM
  2. Basic Java Calculator Code Problem
    By SerbianSergeant in forum New To Java
    Replies: 7
    Last Post: 07-22-2011, 01:50 PM
  3. Java calculator problem ???
    By danielmessick in forum Advanced Java
    Replies: 2
    Last Post: 03-13-2010, 06:53 PM
  4. problem Creating Calculator with java and jsp
    By mindblaster in forum New To Java
    Replies: 0
    Last Post: 12-21-2009, 11:43 PM
  5. Time Calculator Problem
    By ktisallred in forum New To Java
    Replies: 3
    Last Post: 09-22-2009, 07:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •