1.11M Members

Username & Password Verification

 
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));

}

}

 
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.

 
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)
 
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 six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article