- import java.util.*;
-
-
class variables {
-
-
-
public static void main(String[] args) {
-
Scanner input = new Scanner(System.in);
-
String op;
-
int numberOne, numberTwo, result = 0;
-
-
System.out.print("Please input the first number\n");
-
numberOne = input.nextInt();
-
System.out.print("Please input the second number\n");
-
numberTwo = input.nextInt();
-
System.out.print("Please enter the operator you would like to use\n");
-
op = input.next();
-
if (op == "*") {
-
result = numberOne * numberTwo;
-
}
-
if (op == "+") {
-
result = numberOne + numberTwo;
-
}
-
else {
-
System.out.print("The answer is: " + result + " .\n");
-
}
-
}
-
}
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: - import java.util.*;
-
-
class variables {
-
-
-
public static void main(String[] args) {
-
Scanner input = new Scanner(System.in);
-
int numberOne, numberTwo, result = 0, result2 = 0;
-
-
System.out.print("Please input the first number\n");
-
numberOne = input.nextInt();
-
System.out.print("Please input the second number\n");
-
numberTwo = input.nextInt();
-
System.out.print("Please enter the operator you would like to use\n");
-
-
if (input.next() == "+"); {
-
result = numberOne + numberTwo;
-
System.out.print("The answer is: "
-
+ result + " .\n");
-
}
-
-
if (input.next() == "*"); {
-
result2 = numberOne * numberTwo;
-
System.out.print("The answer is: "
-
+ result2 + " .\n");
-
-
}
-
-
-
-
}
-
}
3 7703
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
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: - import java.util.*;
-
-
class variables {
-
-
-
public static void main(String[] args) {
-
Scanner input = new Scanner(System.in);
-
String op = null;
-
int numberOne, numberTwo, result = 0;
-
-
System.out.print("Please input the first number\n");
-
numberOne = input.nextInt();
-
System.out.print("Please input the second number\n");
-
numberTwo = input.nextInt();
-
System.out.print("Please enter the operator you would like to use\n");
-
input.next().equals(op);
-
if (op.equals("*")) {
-
result = numberOne * numberTwo;
-
System.out.print("The answer is: " + result);
-
}
-
else {
-
if (op.equals("+")) {
-
result = numberOne + numberTwo;
-
System.out.print("The answer is: " + result);
-
-
}
-
-
}
-
}
-
}
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: - the variable op can only be null at this location
at the
part of the code, but I have no idea how to fix it, please help.
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: -
op= input.next();
-
if (op.equals("*")) ...
-
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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:
/////////////////////////...
|
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 ->...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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....
|
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
|
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...
| |