473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Java Calculator

22
I have just started using java and I'm trying to make a simple calculator, but I'm confused as to why I keep getting a warning message.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class Calculator {
  4.  
  5.  
  6.  
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner sc=new Scanner(System.in);
  10.         System.out.println("Enter first number: ");
  11.         double d1=sc.nextDouble();
  12.         System.out.println("Enter second number: ");
  13.         double d2=sc.nextDouble();
  14.  
  15.  
  16.     }
  17.  
  18. }

it tells me that my variables d1 and d2 are never read...I have no idea what this means...can someone please help me or refer me to a good book or SOMETHING...I'm totally lost.
Thanks!
Sep 18 '07 #1
11 3563
nickyeng
254 100+
I have just started using java and I'm trying to make a simple calculator, but I'm confused as to why I keep getting a warning message.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class Calculator {
  4.  
  5.  
  6.  
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner sc=new Scanner(System.in);
  10.         System.out.println("Enter first number: ");
  11.         double d1=sc.nextDouble();
  12.         System.out.println("Enter second number: ");
  13.         double d2=sc.nextDouble();
  14.  
  15.  
  16.     }
  17.  
  18. }

it tells me that my variables d1 and d2 are never read...I have no idea what this means...can someone please help me or refer me to a good book or SOMETHING...I'm totally lost.
Thanks!
it doesn't matter if you get warning on the code.
You declare&initialise d1 and d2 but not yet use them in your code...so there are warning show up.
Sep 18 '07 #2
LolaT
22
it doesn't matter if you get warning on the code.
You declare&initialise d1 and d2 but not yet use them in your code...so there are warning show up.

Thanks for the quick reply...I think I figured that part out...now I'm having a problem adding if statements...I'm trying to say if my operator is equal to '+' then the 2 numbers should be added. Like, I said, I'm very new at this, so I apologize if I make stupid mistakes...this is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. System.out.println("Enter an operator (+ - * / %): ");
  2.         String x= sc.next();
  3.         if (x== +)
  4.             System.out.println("The result: " + (d1+d2));
My error message says:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error on token "+", Expression expected after this token

at Calculator.main(Calculator.java:21)
Sep 18 '07 #3
nickyeng
254 100+
Thanks for the quick reply...I think I figured that part out...now I'm having a problem adding if statements...I'm trying to say if my operator is equal to '+' then the 2 numbers should be added. Like, I said, I'm very new at this, so I apologize if I make stupid mistakes...this is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. System.out.println("Enter an operator (+ - * / %): ");
  2.         String x= sc.next();
  3.         if (x== +)
  4.             System.out.println("The result: " + (d1+d2));
My error message says:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error on token "+", Expression expected after this token

at Calculator.main(Calculator.java:21)
if your if statement, you should put like this:

Expand|Select|Wrap|Line Numbers
  1. if(x.equalsIgnoreCase("+"))
  2. {
  3.        System.out.println("The result is " + (d1+d2)) ;
  4. }
  5.  
Sep 18 '07 #4
Nepomuk
3,112 Expert 2GB
Thanks for the quick reply...I think I figured that part out...now I'm having a problem adding if statements...I'm trying to say if my operator is equal to '+' then the 2 numbers should be added. Like, I said, I'm very new at this, so I apologize if I make stupid mistakes...this is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. System.out.println("Enter an operator (+ - * / %): ");
  2.         String x= sc.next();
  3.         if (x== +)
  4.             System.out.println("The result: " + (d1+d2));
My error message says:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error on token "+", Expression expected after this token

at Calculator.main(Calculator.java:21)
First of all, you want "+" to be a String, as that's what you want to compare it to.

Next, you can't use "==" to compare Strings. The Reason is, that every String is an Object in Java and you create two Objects: Your input and the "+", which is in your code. As those two Objects aren't the same, that comparison must result in false.

To compare Strings, use "+".equals(x) or similar.

Greetings,
Nepomuk
Sep 18 '07 #5
LolaT
22
thanks to everyone for their help, i think i'm slowly getting the hang of it.

I've configured my calculator so that it works as it is supposed to (for the most part), but now I'm trying to add a statement that will continue the process of evaluating the numbers entered should the user choose to continue.

So far, here is my code:

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class Calculator {
  4.  
  5.  
  6.  
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner sc=new Scanner(System.in);
  10.         System.out.println("Enter first number: ");
  11.         double d1=sc.nextDouble();
  12.         System.out.println("Enter second number: ");
  13.         double d2=sc.nextDouble();
  14.         System.out.println("Enter an operator (+ - * / %): ");
  15.         String x= sc.next();
  16.         if (x.equalsIgnoreCase("+"))
  17.             System.out.println("The result: " + (d1+d2));
  18.  
  19.         else if (x.equalsIgnoreCase("-"))
  20.             System.out.println("The result: " + (d1-d2));
  21.  
  22.         else if (x.equalsIgnoreCase("*"))
  23.             System.out.println("The result: " + (d1*d2));
  24.  
  25.         else if (x.equalsIgnoreCase("/"))
  26.             System.out.println("The result: " + (d1/d2));
  27.  
  28.         else if (x.equalsIgnoreCase("%"))
  29.             System.out.println("The result: " + (d1/d2));
  30.  
  31.         String option=sc.next();
  32.         System.out.println("Again? (y/n): ");
  33.  
I'm pretty sure the way I did it is strenuous and there is an easier way to do it, but since I am new at this, I'm not really comfortable with loops and such.

If anyone can help me out with how to loop the calculator that would be great...and is there any way to simplify how to get the result?

I haven't gotten much sleep since trying to figure this out and my brain isn't functioning to it's full potential, so any help would be great!
Sep 19 '07 #6
Nepomuk
3,112 Expert 2GB
thanks to everyone for their help, i think i'm slowly getting the hang of it.
Brilliant! That's what we want! :-)
I've configured my calculator so that it works as it is supposed to (for the most part), but now I'm trying to add a statement that will continue the process of evaluating the numbers entered should the user choose to continue.
So what you want is something like this:
Expand|Select|Wrap|Line Numbers
  1. Please enter the first number: 6
  2. Please enter the second number: 9
  3. Please enter the operator: +
  4. 6 * 0 = 54
  5. Do you want to continue (y/n)? y
  6. Your first number is 54.
  7. Please enter your second number: 4
  8. Please enter your operator: -
  9. 54 - 4 = 50
  10. Do you want to continue (y/n)?
  11.  
etc, is that right?
I'm pretty sure the way I did it is strenuous and there is an easier way to do it, but since I am new at this, I'm not really comfortable with loops and such.
Loops are the right idea. This article will give you an idea of what loops can do and how they are used. I would suggest to use a while-loop or a do-while-loop and a boolean, which tells the loop if it should continue or not.

Also, you might want to create a calculate method which does the actual calculating - that would be a bit nicer that what you have now.

Greetings,
Nepomuk
Sep 19 '07 #7
LolaT
22
Brilliant! That's what we want! :-)
So what you want is something like this:
Expand|Select|Wrap|Line Numbers
  1. Please enter the first number: 6
  2. Please enter the second number: 9
  3. Please enter the operator: +
  4. 6 * 0 = 54
  5. Do you want to continue (y/n)? y
  6. Your first number is 54.
  7. Please enter your second number: 4
  8. Please enter your operator: -
  9. 54 - 4 = 50
  10. Do you want to continue (y/n)?
  11.  
etc, is that right?
Loops are the right idea. This article will give you an idea of what loops can do and how they are used. I would suggest to use a while-loop or a do-while-loop and a boolean, which tells the loop if it should continue or not.

Also, you might want to create a calculate method which does the actual calculating - that would be a bit nicer that what you have now.

Greetings,
Nepomuk



Thanks for the article link!
Actually, for my program, if the user chooses to run it again, the input should be 2 brand new numbers, so for example:

Please enter the first number: 6
Please enter the second number: 9
Please enter the operator: +
6 + 9 = 15
Do you want to continue (y/n)? y
Please enter the first number: 4
Please enter the second number: 4
Please enter the operator: -
4 - 4 = 0
Do you want to continue (y/n)?


Also, is there a way to make a statement that has 4 arguments?
Perhaps if I was to write an error message had the user not chosen a correct operator, is there any way to say if x is not equal to ____ or ____ or ____ or ____, then print an error message?

Sorry if that doesn't make much sense! I didn't know how to word it!
Sep 20 '07 #8
Nepomuk
3,112 Expert 2GB
Thanks for the article link!
You're welcome of course! :-)
Actually, for my program, if the user chooses to run it again, the input should be 2 brand new numbers
You can do that with loops too (it's even a bit easier than the example I gave).
Also, is there a way to make a statement that has 4 arguments?
If you want to have more than two arguments, you'll either have to change your code to take exactly four arguments, or you'll need some kind of mechanism for checking, how many arguments the user wants to have. If you want a variable amount, you'll certainly need some sort of collection (e.g. array, vector, list, ...). Also, you need to think about how you want to calculate - should
Expand|Select|Wrap|Line Numbers
  1. 1 + 2 * 3 + 4
  2.  
be resolved with
Expand|Select|Wrap|Line Numbers
  1. 1 + 2 * 3 + 4 = 1 + 6 + 4 = 7 + 4 = 11
  2.  
or with
Expand|Select|Wrap|Line Numbers
  1. 1 + 2 * 3 + 4 = 3 * 3 + 4 = 9 + 4 = 13
  2.  
or even with
Expand|Select|Wrap|Line Numbers
  1. 1 + 2 * 3 + 4 = 3 * 3 + 4 = 3 * 7 = 21
  2.  
or any other way? Maybe you even want to support brackets? This is important, as your program must know, what to do first.

Basically, you'll have to first collect the whole information and then calculate. (You can do it differently, but it makes things so much more complicated.)
Perhaps if I was to write an error message had the user not chosen a correct operator, is there any way to say if x is not equal to ____ or ____ or ____ or ____, then print an error message?
Sure you can Check, if x is one of your possibilities - you can check it like this:
Expand|Select|Wrap|Line Numbers
  1. if(!(x.equals("+")) && !(x.equals("-")) && !(x.equals("*")) && !(x.equals("/")) && !(x.equald("%")))
  2.     // print your message
  3.  
However, you already are checking, if it's one of the known signs with
Expand|Select|Wrap|Line Numbers
  1. if (x.equalsIgnoreCase("+"))
  2.     System.out.println("The result: " + (d1+d2));
  3.  
  4. else if (x.equalsIgnoreCase("-"))
  5.     System.out.println("The result: " + (d1-d2));
  6.  
  7. else if (x.equalsIgnoreCase("*"))
  8.     System.out.println("The result: " + (d1*d2));
  9.  
  10. else if (x.equalsIgnoreCase("/"))
  11.     System.out.println("The result: " + (d1/d2));
  12.  
  13. else if (x.equalsIgnoreCase("%"))
  14.     System.out.println("The result: " + (d1/d2));
  15.  
so if you just make another case
Expand|Select|Wrap|Line Numbers
  1. else
  2.     // print your error
  3.  
you will have it done. If you are using a second method for the calculation and are giving back the result of that, you can do with Exceptions.

I hope, that helps.

Greetings,
Nepomuk
Sep 20 '07 #9
LolaT
22
You're welcome of course! :-)
You can do that with loops too (it's even a bit easier than the example I gave).
If you want to have more than two arguments, you'll either have to change your code to take exactly four arguments, or you'll need some kind of mechanism for checking, how many arguments the user wants to have. If you want a variable amount, you'll certainly need some sort of collection (e.g. array, vector, list, ...). Also, you need to think about how you want to calculate - should
Expand|Select|Wrap|Line Numbers
  1. 1 + 2 * 3 + 4
  2.  
be resolved with
Expand|Select|Wrap|Line Numbers
  1. 1 + 2 * 3 + 4 = 1 + 6 + 4 = 7 + 4 = 11
  2.  
or with
Expand|Select|Wrap|Line Numbers
  1. 1 + 2 * 3 + 4 = 3 * 3 + 4 = 9 + 4 = 13
  2.  
or even with
Expand|Select|Wrap|Line Numbers
  1. 1 + 2 * 3 + 4 = 3 * 3 + 4 = 3 * 7 = 21
  2.  
or any other way? Maybe you even want to support brackets? This is important, as your program must know, what to do first.

Basically, you'll have to first collect the whole information and then calculate. (You can do it differently, but it makes things so much more complicated.)

Sure you can Check, if x is one of your possibilities - you can check it like this:
Expand|Select|Wrap|Line Numbers
  1. if(!(x.equals("+")) && !(x.equals("-")) && !(x.equals("*")) && !(x.equals("/")) && !(x.equald("%")))
  2.     // print your message
  3.  
However, you already are checking, if it's one of the known signs with
Expand|Select|Wrap|Line Numbers
  1. if (x.equalsIgnoreCase("+"))
  2.     System.out.println("The result: " + (d1+d2));
  3.  
  4. else if (x.equalsIgnoreCase("-"))
  5.     System.out.println("The result: " + (d1-d2));
  6.  
  7. else if (x.equalsIgnoreCase("*"))
  8.     System.out.println("The result: " + (d1*d2));
  9.  
  10. else if (x.equalsIgnoreCase("/"))
  11.     System.out.println("The result: " + (d1/d2));
  12.  
  13. else if (x.equalsIgnoreCase("%"))
  14.     System.out.println("The result: " + (d1/d2));
  15.  
so if you just make another case
Expand|Select|Wrap|Line Numbers
  1. else
  2.     // print your error
  3.  
you will have it done. If you are using a second method for the calculation and are giving back the result of that, you can do with Exceptions.

I hope, that helps.

Greetings,
Nepomuk

Thanks a lot for all the advice! I officially got my loop (and my calculator) working! I've stumbled upon a new problem though....how do I assign the result of my calculator to a new variable? For example:

First number: 5
Second number: 9
Operation (+ - / * %): +
The result: 14
Again? (y/n): y
Fist number: _
Second number: 1
Operation (+ - / * %): -
The result: 13


So, when the user puts the "_" symbol, the previous result (meaning 14) is used as the first number.

Thanks again!
Sep 21 '07 #10
Nepomuk
3,112 Expert 2GB
Thanks a lot for all the advice! I officially got my loop (and my calculator) working! I've stumbled upon a new problem though....how do I assign the result of my calculator to a new variable? For example:

First number: 5
Second number: 9
Operation (+ - / * %): +
The result: 14
Again? (y/n): y
Fist number: _
Second number: 1
Operation (+ - / * %): -
The result: 13


So, when the user puts the "_" symbol, the previous result (meaning 14) is used as the first number.

Thanks again!
You're very welcome of course.

For the next task, you'll have to create a variable, which contains the previous result. At the beginning, this could contain 0.

Then you'll have to find out, if the user has entered a Number or not. React to that by using either the input or the result saved before.

That should be enough to get you started. ^^

Greetings,
Nepomuk
Sep 24 '07 #11
forse
7
Hi,

Can see that you have been progressing in your calculator app so far.

If you want a bit more of a reference for a full fledged yet simple calculator application then you may find this link useful.

A Free Calculator application written in JAVA

Good luck
Sep 24 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Dr. Mercury | last post by:
Greetings to all - Someone suggested that Java might be the answer for a little conversion program I need. It's pretty simple, really. Just take a column of numbers: 139 259 433 637
3
by: PieMan2004 | last post by:
Hi, ive been looking for a solid java community to help me when im tearing out my hair :) Basically ive constructed a GUI that has to represent the same look and functions of the typical windows...
2
by: erekose666 | last post by:
I need some help here if anyone is willing. I need to take this program and modify it to do something a little different. First things first, though. If you run this code like it is right now, it...
2
by: phjones | last post by:
Need help programming mortagage calculator for 3 different loans 7 year, 15 year and 30 year. using java array I am a beginner with Java, This is what I have so far. Need to know if I am off the...
5
by: erekose666 | last post by:
I have this program right now, but I need to modify it to output to the JTextPane. What I mean is, when someone clicks on the 1 button, it needs to output to the TextArea a 1. None of the add,...
15
by: colinNeedsJavaHelp | last post by:
I am attempting to program a very basic calculator. The program simply needs to prompt the use to input the computation i.e. 2 * 5 The program needs to compute this and display the result as "The...
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
2
by: jtanz0 | last post by:
Im fairly new to java programming although i have some experience in python. I have to create a calculator program (command line) which takes as input a mathematical expression consisting of...
1
by: Techno3000 | last post by:
import java.io.*; import java.awt.*; import hsa.Console; public class Calculator { static Console c; public static void main (String args) throws IOException
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.