1,105,395 Community Members

Username & Password Verification

Member Avatar
thejunkie
Newbie Poster
5 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm new at Java and I'm tryin to create a Login screen that prompts the user to enter a username and a password and then allows them into the program.

Is it true that I cant use the "switch" on a string and only if else statements?
If it is a string I can't use the !== or == to check if its equivalent.

Is this how it should be done?
Or must i create a boolean?


import java.io.*;

public class test
{
public void pass() throws Exception
{
// declaration
String username;
int id;

BufferedReader userInput=new BufferedReader (new InputStreamReader(System.in));

System.out.println("Enter Username");
username=userInput.readLine();
if (!.equals("lene");
System.out.println("Denied");

else (username.equals("lene");

}

do
{

System.out.print("Enter ID: ");
id=Integer.parseInt(userInput.readLine());

if (id!=123)

System.out.println("Denied");
}else((id==123));

}

}

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

Yes, to test for string equality you need to use equals() or equalsIgnoreCase(). There are more fundamental problems with what you have written there though and it will not compile. Re-examine the basic structure of 'if' statement blocks.

Member Avatar
thejunkie
Newbie Poster
5 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi

I've created the code for the username & password. Now I need to know where do I place it in the main file.

Attachments Password.java (0.59KB) Record.java (2.81KB)
Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
0
 

There are some fundamental problems with your program structure. You seem to be doing password validation in 'Password.java' itself and that too in the main method which isn't such a good idea. Plus your 'Record.java' file is never used.

A simple way would be to create a new file 'Main.java' whose main method would create instances of the 'Password' and 'Record' type as per required by your program logic. Just make sure to keep nothing in main method except instance creation and method invocation i.e. maximum abstraction.

I would recommend you to read some good book and tutorials which teach Java as well as OO programming in general.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article