473,320 Members | 2,117 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,320 software developers and data experts.

Trying to make a command line calculator

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. class variables {
  4.  
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner input = new Scanner(System.in);
  8.         String op;
  9.         int numberOne, numberTwo, result = 0;
  10.  
  11.         System.out.print("Please input the first number\n");
  12.         numberOne = input.nextInt();
  13.         System.out.print("Please input the second number\n");
  14.         numberTwo = input.nextInt();
  15.         System.out.print("Please enter the operator you would like to use\n");
  16.         op = input.next();
  17.         if (op == "*") {
  18.             result = numberOne * numberTwo;
  19.         }
  20.         if (op == "+") {
  21.             result = numberOne + numberTwo;
  22.         }
  23.         else {
  24.         System.out.print("The answer is: " + result + " .\n");
  25.         }
  26.     }
  27. }
It's supposed to take two inputs and set the numberOne and numberTwo ints to them, then it's supposed to get an input, if it equals "+" then it should add numberOne and numberTwo, and show the result to the user. Then it takes the two numbers, and the operator, but it always displays "0" to the user, and I have no idea why, I've only started learning Java a couple of days ago, so please don't be hard on me if it's something simple, thank you. :)

Edit;
I edited the code, now it'll add the two inputs together, but it wont multiply them,
If I put numberOne = 2, numberTwo = 3, and the operation = * it'll still add them together, heres the new code:
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. class variables {
  4.  
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner input = new Scanner(System.in);
  8.         int numberOne, numberTwo, result = 0, result2 = 0;
  9.  
  10.         System.out.print("Please input the first number\n");
  11.         numberOne = input.nextInt();
  12.         System.out.print("Please input the second number\n");
  13.         numberTwo = input.nextInt();
  14.         System.out.print("Please enter the operator you would like to use\n");
  15.  
  16.         if (input.next() == "+"); {
  17.             result = numberOne + numberTwo;
  18.             System.out.print("The answer is: "
  19.                     + result + " .\n");
  20.         }
  21.  
  22.         if (input.next() == "*"); {
  23.             result2 = numberOne * numberTwo;
  24.             System.out.print("The answer is: "
  25.                     + result2 + " .\n");
  26.  
  27.         }
  28.  
  29.  
  30.  
  31.     }
  32. }
Oct 12 '08 #1
3 7703
JosAH
11,448 Expert 8TB
You can't compare two Strings for equality by using the == operator. You have to
use the equals() method for that. Read the API documentation for the String class.

kind regards,

Jos
Oct 12 '08 #2
Thanks a ton for replying, JosAH I've been trying to figure out what my problem is, but I just can't seem to find it, heres my current code:
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. class variables {
  4.  
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner input = new Scanner(System.in);
  8.         String op = null;
  9.         int numberOne, numberTwo, result = 0;
  10.  
  11.         System.out.print("Please input the first number\n");
  12.         numberOne = input.nextInt();
  13.         System.out.print("Please input the second number\n");
  14.         numberTwo = input.nextInt();
  15.         System.out.print("Please enter the operator you would like to use\n");
  16.         input.next().equals(op);
  17.         if (op.equals("*")) {
  18.             result = numberOne * numberTwo;
  19.             System.out.print("The answer is: " + result);
  20.         }
  21.         else {
  22.         if (op.equals("+")) {
  23.             result = numberOne + numberTwo;
  24.             System.out.print("The answer is: " + result);
  25.  
  26.         }
  27.  
  28.         }
  29.     }
  30. }
and heres the problem:


as you can see, the program didn't output the answer for the "+" equation.
It outputs the answer for the "*" equation, though.
Here is what I think the problem is;
I'm using Eclipse and I get the warning:
Expand|Select|Wrap|Line Numbers
  1. the variable op can only be null at this location
at the
Expand|Select|Wrap|Line Numbers
  1. if (op.equals("*")) {
part of the code, but I have no idea how to fix it, please help.
Oct 12 '08 #3
JosAH
11,448 Expert 8TB
There's no magic: first assign the user input to the String op variable; next check
the value of the op variable; in code the two steps go like this:

Expand|Select|Wrap|Line Numbers
  1. op= input.next();
  2. if (op.equals("*")) ...
  3.  
kind regards,

Jos
Oct 12 '08 #4

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

Similar topics

7
by: Steve M | last post by:
I'm trying to invoke a Java command-line program from my Python program on Windows XP. I cannot get the paths in one of the arguments to work right. The instructions for the program describe the...
10
by: Steve | last post by:
Is there anyone with experience using the Winzip command line add-in? I can't get it to create the zip file with a space in the file name. Using: RetValue = Shell("C:\winzip\wzzip.exe...
5
by: randyelliott | last post by:
Good Day, I have a MS Access (Access 2000 now upgraded to 2003) database that tracks customer information. One function of this database is to create an encrypted license file for our software,...
2
by: David Richards | last post by:
Hi, I wonder if anyone can help me. I am trying to learn to program in VB.Net and am having a little difficulty compiling a program using the command line. I am trying to create a small mdi...
1
by: Rune Jacobsen | last post by:
Hi, I've been trying to figure this one out, but my experience just doesn't have what it takes... :| I am writing an application that reads an XML file and displays the contents in various...
1
by: Andrew McCall | last post by:
Hi Folks, I am building an application under multiple OS's, and I wanted to give my application For example, the test application I am working on is a calculator and I would like to have a...
3
by: andrew | last post by:
Hello, I have spend the past day trying to figure out how to do this. PROBLEM: My instructor wants us to use a shapes library that horstmann wrote. An example would be: /////////////////////////...
3
by: jlw16 | last post by:
Hello, I’m trying to use my vbs script to get a command line argument for a file which will need to be opened through QuickTestPro. Below are the commands I’m using: Dim qt_file 'As String ->...
7
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.