Results 1 to 5 of 5
  1. #1
    JavaGame is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Strings, scanners and IFs

    Hey everyone, this is my first post. Ive been learning some basic java in eclipse (linux ubuntu if you need to know, and im using Sun's java instead of OpenJDK). Anyways, im trying to make a very basic password accepting kind of system, just for some practice.
    If i use integers for passwords the program runs fine, here is the code:

    import java.util.Scanner;

    public class arrays {

    public static void main(String[] args) {

    System.out.println("Enter password");
    Scanner scan = new Scanner(System.in);
    int y = scan.nextInt();

    if (y == 6) {
    System.out.println("correct");
    }
    else {
    System.out.println("wrong");
    }


    }

    }



    So basically the password is 6 and the program just displays correct or wrong. However it doesn't quite work the same with string, here is the code im trying to use:

    import java.util.Scanner;

    public class arrays {

    public static void main(String[] args) {

    System.out.println("Enter password");
    Scanner scan = new Scanner(System.in);
    String y = scan.nextLine();

    if (y == "password") {
    System.out.println("correct");
    }
    else {
    System.out.println("wrong");
    }


    }

    }

    I always get wrong :/ anybody help me out here? Cheers!

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

    Default Re: Strings, scanners and IFs

    1) Don't compare Strings with == as this checks if one String variable references the same object as another String variable. Instead use String's equals(...) or equalsIgnoreCase(...) method.

  3. #3
    JavaGame is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: Strings, scanners and IFs

    Thanks for the reply, but just to clarify it'd be

    if ( String y equals("password ") {



    Thanks again

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

    Default Re: Strings, scanners and IFs

    No, you don't declare a type in an if boolean condition. It would be

    Java Code:
    if y.equals("foo") {
      //...
    }

  5. #5
    JavaGame is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Replies: 6
    Last Post: 08-22-2011, 05:32 AM
  2. Need help with Strings
    By </3java in forum New To Java
    Replies: 24
    Last Post: 02-17-2011, 05:00 PM
  3. Buffered Reader and scanners
    By nevermiind in forum New To Java
    Replies: 8
    Last Post: 05-04-2010, 06:22 PM
  4. It is possible in Strings..?
    By mlibot in forum New To Java
    Replies: 1
    Last Post: 03-12-2010, 05:30 AM
  5. Scanners / Printers
    By cytech in forum New To Java
    Replies: 0
    Last Post: 03-17-2008, 05:54 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
  •