Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: My First program, I need help.

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    My Mood
    Sad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My First program, I need help.

    I'm trying to make it so you type in you username, then you type it in later
    and if the two match then it prints logged in succesfully, but it's basically saying the two don't match..?



    import java.util.Scanner;
    class Der{

    public static void main(String[]arguments)

    {

    Scanner key = new Scanner(System.in);
    System.out.println("Set your Username: ");
    String UserName = key.nextLine();
    Scanner key2 = new Scanner(System.in);
    System.out.println("Enter your UserName");
    String EnteredUserName = key2.nextLine();

    if (EnteredUserName == UserName)

    {

    System.out.print("Logged in Successfully");
    }

    }
    }


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: My First program, I need help.

    Hello Tyridge77!
    You should use the String's equals() method instead of the == operator when comparing Strings.
    And for your next posts be sure you use the code tags to get highlighting in your code.
    Last edited by andreas90; June 23rd, 2012 at 03:48 AM.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    My Mood
    Sad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My First program, I need help.

    Quote Originally Posted by andreas90 View Post
    Hello Tyridge77!
    You should use the String's equals() method instead of the == operator when comparing Strings.
    And for your next posts be sure you use the code tags to get highlighting in your code.

    I tried the equals mehod, and it still isn't working..



    ------

     
     
    import java.util.Scanner;
    class Der{
     
    	public static void main(String[]arguments)
     
    	{
     
    		Scanner key = new Scanner(System.in);
    		System.out.println("Set your Username: ");
    	String	UserName = key.nextLine();
     Scanner key2 = new Scanner(System.in);
     System.out.println("Enter your UserName");
     String EnteredUserName = key2.nextLine();
     
     
    String a = key.nextLine();
    String b = key2.nextLine();
     
     
    if(a.equals(b)){
     
    	 System.out.print("Logged in Successfully");
     }
     
     
    }
     
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My First program, I need help.

    Try debugging the code by adding a println statement that prints out the values of a and b so you can see what the computer sees:
    System.out.println("a="+a + "<\nb=" + b +"<");

    Add this just before the if statement.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: My First program, I need help.

    Quote Originally Posted by Tyridge77 View Post
    I tried the equals mehod, and it still isn't working..
    Your code is working for me as expected. You are probably entering different Strings for a and b (a space after one of the inputs might be the problem). Try printing them as Norm suggested and you 'll find out what the problem is.

  6. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: My First program, I need help.

    //You do not need to create key and key2. One will work.

    import java.util.*;

    class Application{

    public static void main(String[]arguments)

    {

    Scanner key = new Scanner(System.in);
    System.out.print("Set your Username: ");
    String UserName = key.nextLine();
    System.out.print("Enter your UserName");
    String EnteredUserName = key.nextLine();




    if(UserName.equals(EnteredUserName)){

    System.out.print("Logged in Successfully");
    }
    else
    System.out.println("Unsucessful"); just to check whether it works or not you can delete it later

    }
    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My First program, I need help.

    @kindk12
    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Also use code that follows the java programming standards.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Program to launch and mirror another program
    By hayate in forum Java Theory & Questions
    Replies: 13
    Last Post: March 9th, 2012, 12:47 AM
  2. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM