Click here to Skip to main content
14,331,316 members
Rate this:
Please Sign up or sign in to vote.
See more:
import java.util.Scanner;



public class systemsimulate {
	
public static void main(String[] args) {
	
	String y,ynext;
	
	Scanner scm=new Scanner(System.in);
	System.out.println("enter the initial  state");
	y=scm.next();

	
	System.out.println("enter the disturbance");
    String d=scm.next();
	
if(y=="A" && d=="1"){
		System.out.println("A");
	}
}
}


when i run this program it doesnt print A ispite og giving valid input parameters.why the condition is not being evaluated.can anyone tell me the error

What I have tried:

i tried using char instead of string format but it is still not working.i also displayed indivsual y and d .it got displayed but it is not performing code in if loop
Posted
Updated 7-Mar-18 6:01am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

The == operator tests for reference equality. That means it checks if it is the same object. But you want to check for value equality (if two objects hold the same value). See also Comparing Objects - Wikibooks, open books for an open world[^].

For strings use the equals or compareTo methods (see Comparing Strings and Portions of Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)[^]).
   
Comments
CPallini 7-Mar-18 7:28am
   
5.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Start by using the debugger - put a breakpoint on the if line and run your code. When the debugger hits the line, it will stop and you can look at exactly what is in y and d before the condition is checked.
We can't do that for you: we can't run your code under exactly the conditions you do, we can see what you are typing!
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Run your code on debugger and inspect y and d contains.
Pay extra attention to the variables, if y="A ", it will print as you expect, but the text will fail.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100