Results 1 to 3 of 3
  1. #1
    eyal3400 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Lightbulb 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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:
    Java Code:
    if (stringA.equals(stringB)) {
      //  do something
    }
    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.

    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.

  3. #3
    eyal3400 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    thx alot for the useful info and the helpful advice mate, appreciate it

Similar Threads

  1. Problem with code
    By koryvandell in forum New To Java
    Replies: 4
    Last Post: 04-25-2011, 04:28 AM
  2. GUI code problem
    By baf06 in forum New To Java
    Replies: 17
    Last Post: 04-07-2011, 03:46 PM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Problem with code
    By and0rsk in forum New To Java
    Replies: 1
    Last Post: 10-10-2010, 09:52 AM
  5. Replies: 9
    Last Post: 09-21-2010, 04:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •