I am a beginner.
I am experimenting with java while loop. I have knowledge of both java while loop and if statements but have never nested them before. I will first post the script and then tell you guys the problem.
here is the script:
import java.util.Scanner;
class whileloop{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("This is a basic calculator.");
System.out.println("To exit, input exit when asked to input the method.");
System.out.println("----------------------------------------------");
while (true){
System.out.println("to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*' and to exit, input 'exit.'");
System.out.println("----------------------------------------------------");
System.out.print("Please enter a method here:");
String method = scan.nextLine();
if (method == "exit"){
System.out.println("You chose to exit.");
break;
}
else if (method == "+"){
System.out.println("You chose to add.");
System.out.print("Please enter first number here:");
double fnum = scan.nextInt();
System.out.print("Please enter second number here:");
double snum = scan.nextInt();
double ans = fnum + snum;
System.out.print("The answer is");
System.out.println(ans);
}
}
}
}
First of all, i have no knowledge about this type of nesting i am just experimenting right now so maybe that's why my method may seem a bit unusual to you people.
now, the problem is that whenever i execute this code, the infinite while loop works perfectly but it always skips the if statement no matter what i input and starts to loop the instructions again.
please help me with this.
use
method.equalsIgnoreCase("exit")
rathe than method == "exit"
equalsIgnoreCase
compares contents but ==
compares references and in you case references would not be same as you are using scan.nextLine();
which creates a new String Object.
When comparing strings, you need to use the .equals()
method, so, in your case, you need to change method == "exit"
to method.equals("exit")
. The same applies for the other if
statement.
You can take a look at this Oracle tutorial for more information on String comparison.
If you use ==
to compare strings this will compare the reference of the object, in which case it will always return false in your case. Since you don't have an else and only an else if you don't actually see this happening and it carries on to the next iteration.
Change it to .equals()
method to compare object value rather than reference.
Side Note
Be careful with reference comparison as you may have seen the odd example where reference equality is used which may throw you (it certainly did for me the first time as this example below shows).
String a = "Hello";
String b = "Hello";
If you compare a == b
in this case it will actually return true, this is because Java caches strings and so they do in this case have the same reference. Be careful when you see examples like this as they are the exception.
Use method.equals("exit")
for String comparison.
See this post.
== Its test reference equality for the objects.
equals(String) This is a method in String
class to comparing the content of two string.
In java, ==
means checking reference equality
, except numbers.
(see comments below)
Object obj1 = new Object();//toString: java.lang.Object@459189e1 <--|
//|- different addresses
Object obj2 = new Object();//toString: java.lang.Object@55f33675 <--|
obj1 == obj2; //false, because they are difference objects, difference references.
obj1 = obj2;
obj1 == obj2; //true, they are now pointing to the same object
So, you need use .equals
to check String equality.
This is a problem with all beginners. I had done it when I started. The problem here is
the equality operator ==
should not be used to compare two strings here. Because it is used to check any two objects are no different but of a same instance. ie
Object O1 = new Object()
Object o2 = o1
Here
o1 == o2
will evaluate to true. Because they check for the referential equality.
In the case of string, when you are comparing the content of the strings, you should always compare themeither with
equals
method or
equalsIgnoreCase
if you are not bothered about the case. God bless. Cheers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.