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.59 KB)
import java.io.*;

public class Password {

public void main(String[] args) throws IOException{


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

String user;
int password;


System.out.println ("Enter your username");

user=userInput.readLine();



System.out.println ("Enter your password");

password=Integer.parseInt(userInput.readLine());



if (!"lene".equals (user) && 123!=(password)) {

System.out.println ("Sorry, username unknown or password wrong");



System.exit (1);

}

System.out.println ("Welcome!");


}

}
Record.java (2.81 KB)
import java.io.*;
import java.util.*;
import java.text.*;

public class Record{

	private String name, dimensions, details, date;
	private double price;
	private int qty;
	private int ID;


	public Record(String name, String dimensions, double price, String details, String date, int qty, int ID)

		{
		this.name=name;
		this.dimensions=dimensions;
		this.price=price;
		this.details=details;
		this.date=date;
		this.qty=qty;
		this.ID=ID;


		}

	public Record(){}

	public void read(RandomAccessFile file)throws IOException{

		char namearray[]=new char [40];
				for(int x=0;x<40;x++)
					namearray[x]=file.readChar();
		name=new String(namearray);

		char dimensionsarray[]=new char[40];
					for(int x=0; x<40; x++)
					dimensionsarray[x]=file.readChar();
		dimensions=new String(dimensionsarray);


		char detailsarray[]=new char[40];
					for(int x=0; x<40; x++)
					detailsarray[x]=file.readChar();
		details=new String(detailsarray);


		char datearray[]=new char[10];
					for(int x=0; x<10; x++)
					datearray[x]=file.readChar();
		date=new String(datearray);

		price=file.readDouble();
		ID=file.readInt();
		qty=file.readInt();

	}

	public void write(RandomAccessFile file) throws IOException{

		StringBuffer buf;

				if (name!=null)
				buf=new StringBuffer(name);
			else
				buf=new StringBuffer(40);
				buf.setLength(40);
				file.writeChars(buf.toString());

				if (dimensions!=null)
				buf=new StringBuffer(dimensions);
			else
				buf=new StringBuffer(40);
				buf.setLength(40);
				file.writeChars(buf.toString());

				if (details!=null)
				buf=new StringBuffer(details);
			else
				buf=new StringBuffer(40);
				buf.setLength(40);
				file.writeChars(buf.toString());

				if (date!=null)
				buf=new StringBuffer(date);
			else
				buf=new StringBuffer(10);
				buf.setLength(10);
				file.writeChars(buf.toString());


			file.writeInt(qty);
			file.writeInt(ID);
			file.writeDouble(price);


	}


		public void setID(int ID){
		this.ID=ID;
	}
		public int getID(){
		return ID;
	}

		public void setName(String str){
		this.name=str;
	}
		public String getName(){
		return name;
	}


		public void setDimensions(String str){
		this.dimensions=str;
	}
		public String getDimensions(){
		return dimensions;
	}


		public void setDetails(String str){
		this.details=str;
	}
		public String getDetails(){
		return details;
	}

		public void setDate(String str){
		this.date=str;
	}
		public String getDate(){
		return date;



	}

		public void setQty(int qty){
		this.qty=qty;
	}
		public int getQty(){
		return qty;
	}

		public void setPrice(double price){
		this.price=price;
	}
		public double getPrice(){
		return price;


}

	public static long size(){return 50;}

}
 
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