Results 1 to 7 of 7
  1. #1
    FromDownUnder is offline Member
    Join Date
    May 2014
    Posts
    3
    Rep Power
    0

    Default Help with a very simple program? Thanks!

    Hi all,

    I'm having a bit of a problem with a simple program I cooked up for practice. The code is below:

    Java Code:
    import java.util.Scanner;
    
    
    public class CAPPLogin {
    
    	public static void main(String[] args) {
    		/* This program is designed to take two string inputs from the user simultaneously
    		 * and check them against two preset string values. If the inputs match, the user
    		 * will receive an "Access Granted" message. If one or both of the inputs do not
    		 * match, the user will receive one of three self-written error messages (not an exception) 
    		 * that will each be output depending on the scenario.
    		 */
    		
    	
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Thank you for activating CAPP Employee LogIn Launcher.");
    		System.out.print("Username: ");
    		String username = scan.nextLine();
    		System.out.print("Password: ");
    		String password = scan.nextLine();
    		
    		if (username == "Employee1" && password == "bli33ard")// If both the username and password match the preset strings
    		{
    				System.out.println("Processing...");
    				System.out.println("Account recognized. Welcome Michael.");
    		}
    		else if(username != "Employee1")// If the username does not match the preset string
    		{
    				System.out.println("Processing...");
    				System.out.println("Error: Username not recognized.");
    		}
    		else if(password != "bli33ard")// If the password does not match the preset string
    		{
    				System.out.println("Processing...");
    				System.out.println("Error: Password not recognized.");
    		
    		}
    		else if (username != "Employee1" && password != "bli33ard")// If both the username and password do not match the preset strings
    		{
    				System.out.println("Processing...");
    				System.out.println("The specified account does not exist.");
    		}
    		
    
    	}
    
    }
    (Just so you know, I use Eclipse for my programs.)
    Like I said, this program is just for practice and isn't intended for anything other than learning Java.
    I am having no trouble at all running the program itself, there are no critical errors. It's just whenever I do run it, and input the correct "log-in info," it gives me the error message "Error: Username not recognized." This is odd, as I have checked the program itself and my input.
    If it's a simple oversight, like it probably is, forgive me as I am quite new to Java.
    Also, if anyone has feedback on how I could improve the program itself, that would be very much appreciated

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Help with a very simple program? Thanks!

    You need to use the equals() method for comparing Strings.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    FromDownUnder is offline Member
    Join Date
    May 2014
    Posts
    3
    Rep Power
    0

    Default Re: Help with a very simple program? Thanks!

    Thanks for the reply! So, how would I go about converting the if statements to equals() methods?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Help with a very simple program? Thanks!

    Do you know how to call a method?
    Read the API doc for the String class's equals() method to see how to code it.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    FromDownUnder is offline Member
    Join Date
    May 2014
    Posts
    3
    Rep Power
    0

    Default Re: Help with a very simple program? Thanks!

    Oh, of course
    Thank you for the help! The program works now
    Last edited by FromDownUnder; 05-15-2014 at 01:34 AM. Reason: Implemented solution into program

  6. #6
    ravidamarla is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Re: Help with a very simple program? Thanks!

    yes by using equals() method this program will work sucessfully.If you want clear cut difference between equals and "==" and compareTo() method, visit following website
    TutorialsEra | ...........................for success
    Strings Example | TutorialsEra

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,767
    Blog Entries
    7
    Rep Power
    21

    Default Re: Help with a very simple program? Thanks!

    Quote Originally Posted by ravidamarla View Post
    yes by using equals() method this program will work sucessfully.If you want clear cut difference between equals and "==" and compareTo() method, visit following website
    TutorialsEra | ...........................for success
    Strings Example | TutorialsEra
    I checked (part of) that tutorial: it is so full of typos and mistakes, it isn't worth the bits it takes and I would've been ashamed to publish its url. Thread closed.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 07:40 AM
  2. My simple GUI program could use a bit of help.
    By SpicyHero in forum New To Java
    Replies: 2
    Last Post: 09-22-2013, 12:49 AM
  3. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 06:12 AM
  4. Please help with simple program.. Very simple.
    By jonytek in forum New To Java
    Replies: 7
    Last Post: 02-14-2011, 01:44 AM
  5. Simple Program
    By TheRocket in forum Advanced Java
    Replies: 15
    Last Post: 12-30-2008, 03:35 PM

Tags for this Thread

Posting Permissions

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