Results 1 to 3 of 3
- 07-19-2011, 12:07 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
have problem with a code help please...
i have started working with java a week ago, however i have a background from JavaScript. i have written a simple code that goes like this:
import java.util.Scanner;
public class hi5 {
public static void main (String args[]){
Scanner a = new Scanner (System.in);
System.out.println ("print a code to access message: ");
String code = a.nextLine();
System.out.println ("now enter your message: ");
String msg = a.nextLine();
System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n");
//if anyone knows another way to actually blank the screen i would be happy to know
System.out.println("-----------------------------------");
String code1 = a.nextLine();
if (code1 == code){
System.out.println("here is the secret massage: ");
System.out.println(msg);
System.out.println("press 0 to blank screen");
int r;
r = a.nextInt();
if (r == 0){
System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n");
}
}
if(code1 != code){
System.out.print("wrong");
}
}
}
the problem is when i run it it always considers code 1 to be different than code so it always posts "wrong" i have no idea why even though i tried everything
help will be greatly appreciaterd
-
You're trying to compare two Strings with the == operation, and this is failing because == checks if two variables refer to the same object, and you really don't care about this. Instead what you really want to know is are the character arrays held by the two String variables the same, and to do that, you'll need to use either the String equals(...) method or its equalsIgnoreCase(...) method. So to sum, instead of
Java Code:if (stringA == stringB) { // do something }
do:
Also, you'll want to use a more informative subject heading in your next question here. The thread topic should summarize your current problem in a logical way. Of course you're having a "have problem with a code" else you wouldn't be posting here, but this subject heading tells us nothing about your problem. Better would be something like "Problem comparing Strings" or something similar.Java Code:if (stringA.equals(stringB)) { // do something }
Next we'll work on code tags. ;)
Good luck and welcome to the forum.Last edited by Fubarable; 07-19-2011 at 12:30 AM.
- 07-19-2011, 02:20 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Problem with code
By koryvandell in forum New To JavaReplies: 4Last Post: 04-25-2011, 04:28 AM -
GUI code problem
By baf06 in forum New To JavaReplies: 17Last Post: 04-07-2011, 03:46 PM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
Problem with code
By and0rsk in forum New To JavaReplies: 1Last Post: 10-10-2010, 09:52 AM -
Can anyone see the problem with my code? problem understanding switch (newbish)
By keith in forum New To JavaReplies: 9Last Post: 09-21-2010, 04:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks