This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Iam making a basic calculator with 3 functions. Iam using a Scanner-utility to help me interact with the calculator. You enter the first number, then it saves it to a variable, You enter a second number and that is also saved to a variable. Then after that it asks you what you want to do with the numbers. You can choose between Multiply, Add, Subtract. But none of them seem to work, and i dont know why, but iam guessing that the String that is inserted after the question isnt saved or maybe iam using wrong method.
import java.util.Scanner;
public class MainClass {
private static final String Multiply = null;
private static final String Add = null;
private static final String Subtract = null;
public static void main(String[] args)
{
MainClass mainObject=new MainClass();
mainObject.Calculator();
}
public void Calculator()
{
double firstnumber;
double secondnumber;
double result;
String word1="Multiply";
String word2="Add";
String word3="Subtract";
Scanner scan=new Scanner(System.in);
System.out.println("Enter first number");
firstnumber=scan.nextInt();
System.out.println("Enter second number");
secondnumber=scan.nextInt();
System.out.println("What do you want to do?");
String operator=scan.next();
if(operator==word1)
{
result=firstnumber*secondnumber;
System.out.println(result);
}
else if(operator==word2)
{
result=firstnumber+secondnumber;
System.out.println(result);
}
else if(operator==word3)
{
result=firstnumber-secondnumber;
System.out.println(result);
}
}
}
The comparison operation between the strings is not == but the equals() method:
if(operator.equals("add"))
...
else if (operator.equals("subtract"))
...
else if (operator.equals("multiply"))
...
else
wrong input
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 1 year ago.
so I try to use a string variable in the if statement but it seems it doesn't work like int and double and other primitive data types, why? Pls help
my code:
import java.util.Scanner;
public class Something{
static void Some(){
Scanner input = new Scanner(System.in);
String answer;
System.out.println("Hello Sir, what can I do for you?");
answer = input.nextLine();
String i = "2";
if(answer == i){
System.out.println("Sure Sir ");
}
else{
System.out.println("Sir, anything?");
}
}
public static void main(String [] args){
Some();
}
}
input:2
output: Sir, anything?
The == operator compares references in case of strings. To compare values you can use equals() method.
if(a.equals(b)){}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
I'm trying to make a program that can read whatever the user inputs and checks their input using if..else statement.
import java.util.Scanner;
class Answers{
public void FalseAnim() {
System.out.println("Game Over your answer is wrong. try again!");
}
public void CorrectAnim() {
System.out.println("your answer is correct");
}
}
public class quizgame {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
Answers ans = new Answers();
String strans1;
System.out.println("welcome to the quiz game!");
System.out.println("what is 1+1");
strans1 = input.nextLine();
if (strans1=="two"||strans1=="2") {
ans.CorrectAnim();
}
else {
ans.FalseAnim();
}
}
}
every time I run the program and input anything it goes straight into the else statement, even when I input either a "2" or a "two"
if ("two".equals(strans1)||"2".equals(strans1))
will work.
In your code, you are comparing references and not the value. Hence it is returning false either way.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
public class LabWork {
private String yourStatus;
private int yourIncome;
private double tax;
public int calculateTax () {
if (yourStatus = "Married" && yourIncome <= 2000) {
tax = yourIncome/10;
}
else if (yourStatus = "Married" && yourIncome > 2000) {
tax = 3*yourIncome/20;
}
else if (yourStatus = "Single" && yourIncome <=2000) {
tax = 17*yourIncome/100;
}
else
tax = 22*yourIncome/100;
}
public double getTax () {
return tax;
}
}
this is my first code and I have a tester class to use this like:
import java.util.Scanner;
public class UseLab3Work {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("What is your status? ");
String yourStatus = keyboard.next();
System.out.println("What is your incomew? ");
int yourIncome = keyboard.nextInt();
}
}
However, in the first program, I'm getting an error like "String cannot be converted to boolean" at line 7,10 and 14. Then how should I use if with String? For example I have input in tester and when I write there Married, the program should calculate tax related to my String input.
I first would have thought to mark this a duplicate, since you are comparing your Objects wrong, but it's a step further.
if (yourStatus = "Married" )
Here, you don't compare the values of the String, you actually just assign it.
if (yourStatus == "Married")
This would be better, but will produce false results. After all, the == operator is used for referential comparison, while you want to compare values.
if (yourStatus.equals("Married")) (or if (yourStatus.equalsIgnoreCase("Married")) )
would be better, since this is the correct way to compare the values of Strings.
An even better way would be:
if ( "Married".equals(yourStatus))
In the other order, if yourStatus is null, it will throw a NullPointerException, which you avoid by calling equals (or equalsIgnoreCase) on the String literal.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I want user to input text while it is not equal to "start".When it is equal to "start" I want to show "Bravo".In my code when I enter "start" it just continue to ask to input a text.What is missing in my code to process the operation i described.
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String komanda = "a";
do {
System.out.println("Unesi komandu ");
komanda = input.nextLine();
}
while(komanda != "start");
System.out.println("Bravo");
}
}
You have to use the equals method to compare strings in java:
while (!komanda.equals("start"));
or even better
while (!"start".equals(komanda));
this does not crash if komanda is null
See How do I compare strings in Java? for more information.
do it this way
Scanner input = new Scanner(System.in);
String komanda = "a";
do {
System.out.println("Unesi komandu ");
komanda = input.nextLine();
}
while(!"start".equals(komanda));
System.out.println("Bravo");
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I was wondering how I can fix this:
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What is your name?");
String name = input.next();
if (name == Donald)
System.out.println("Welcome back Admin");
else
System.out.println("Go Away");
}
}
I want to make it so that if the user inputs a specific name, then it will say something specific, anything else and it says go away.
I am a new student of Java and was messing around to see if this is possible
If I understand correctly strings are immutable and are frequently reused, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence. That means you can't just ask if string1 == string2 because they might be separate instances. So you want to check with string.equals(string2) to 'see if the content is the same'.
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What is your name?");
String name = input.next();
if (name.equals("Donald"))
System.out.println("Welcome back Admin");
else
System.out.println("Go Away");
}
}