Jump to content

Help [Java]

PugoOfficial

How do I make the program confirm if the input of the user is correct or wrong(Username and Password) ? Appreciate it if someone actually showed me the solution .. The Aim of the program is to make the user to register(if they don't have an account yet) then log in then exit.

import java.io.*; public class Login { 	 public static void main (String [] args) throws IOException { 	 	 BufferedReader fc = new BufferedReader(new InputStreamReader(System.in)); 	 	  	 	  String RegUsers[] = new String[1]; 	 	  String RegPass[] = new String[1]; 	 	  	 	 boolean loop = true;			while(loop == true) {										boolean choose = true;						while (choose == true) { 	 	  	 	 System.out.println("[1]Register a new Account    [2]Login an existing Account      [3]Exit"); 	 	 int z = Integer.parseInt(fc.readLine()); 	 	  	 	 if (z == 1) { 	 	   	 	   int x = 0; while(RegUsers[x] !=null) { x++; }  	 	    	 	   System.out.println("Enter your username: "); 	 	    	 	    RegUsers[x] = fc.readLine(); 	 	     	 	     System.out.println("Enter your password: "); 	 	      	 	      RegPass[x] = fc.readLine(); 	 	       	 	       for(int i = 0; i<RegUsers.length; i++) {  	  			 System.out.println(RegUsers[i]); 	  			 } 	  			 }			 else if (z == 2) {				System.out.println("Enter your username: ");				String user = fc.readLine();								System.out.println("Enter your password: "); 	 	     	String pass = fc.readLine();				}							else {				System.out.println("Program Terminated!");				choose = false;				}							loop = false;								}			}		}	} 

CPU: Intel Core i7-4790K @ 4.0GHz | COOLING: Corsair H100i Liquid Cooler | MOTHERBOARD: ASUS Maximus VII Formula ATX | MEMORY: Corsair Vengeance Pro Series 16GB (2 x 8) DDR3-1866 | STORAGE: Intel 730 Series 480GB SSD + Seagate Barracuda 3TB HDD | PSU: Corsair AX860i 80+ Platinum | GPU: ASUS GeForce GTX 780Ti DirectCU II (2-Way SLI) | CASE: Phanteks Enthoo Luxe (Black) | DISPLAY: ASUS PB278Q 27.0" (2560 x 1440) | KEYBOARD: Razer BlackWidow Chroma | MOUSE: Razer Deathadder Chroma | SOUND: Logitech Z906 5.1 Speakers / Razer Kraken Chroma | OS: Microsoft Windows 8.1 (64-bit)

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not giving you solutions, lol. Just compare the strings with the equals() method.

 

Also, I'm not sure why you're using an String array of size 1. It's impractical.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not giving you solutions, lol. Just compare the strings with the equals() method.

 

Also, I'm not sure why you're using an String array of size 1. It's impractical.

 

Well I'm just using the size of 1 for testing I could change that anytime you know ;)

CPU: Intel Core i7-4790K @ 4.0GHz | COOLING: Corsair H100i Liquid Cooler | MOTHERBOARD: ASUS Maximus VII Formula ATX | MEMORY: Corsair Vengeance Pro Series 16GB (2 x 8) DDR3-1866 | STORAGE: Intel 730 Series 480GB SSD + Seagate Barracuda 3TB HDD | PSU: Corsair AX860i 80+ Platinum | GPU: ASUS GeForce GTX 780Ti DirectCU II (2-Way SLI) | CASE: Phanteks Enthoo Luxe (Black) | DISPLAY: ASUS PB278Q 27.0" (2560 x 1440) | KEYBOARD: Razer BlackWidow Chroma | MOUSE: Razer Deathadder Chroma | SOUND: Logitech Z906 5.1 Speakers / Razer Kraken Chroma | OS: Microsoft Windows 8.1 (64-bit)

Link to comment
Share on other sites

Link to post
Share on other sites

It also helps if you keep good formatting to help read code, it can be very confusing when it keeps indenting.

 

If you are not encrypting passwords, then you can just do as Midnight suggested, use .equals(), like so:

for (int i = 0; i < RegUsers.length; i++){    if (RegUsers[i].equals(user)) //Up to you if case sensitive matters, use equalsIgnoreCase()    {        if (RegPass[i].equals(pass))        {            // correct password        } else {            // wrong password        }        break;    }}

There are probably much better ways of doing this, but this is the quick and dirty solution.

Edited by KuroSetsuna29
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×