Results 1 to 4 of 4
  1. #1
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default creation of an algorithm for log in with verification

    i'm making a code for a log in system that allow me to verify if username and psw are correct (using a file txt as refer), and then i will add maybe the possibility to sign up.
    BUT at the momento i have some proble, if someone can help me with the code will be very great

    the fact is that i want this kind of formattation on the .txt
    username psw
    username psw
    username psw
    ...etc

    so i have to read the lines and split to the " " and compare the insert data and the read data.
    HERE IS my code, it star but give me this error when i insert any word
    XML Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    	at prova.main(prova.java:20)
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class prova {
    	public static void main(String[] args) throws FileNotFoundException {
    		
    		System.out.println("INSERT USERNAME AND PASSWORD (USERNAME PSW)");
    		
    		File data = new File("path to file... provone.txt");
    		Scanner reading_file = new Scanner(data);
    		
    		while(reading_file.hasNextLine()){
    			
    			Scanner name = new Scanner(System.in);
    			String name_entered = name.nextLine();
    			
    			String[] divide_data_entered = name_entered.split(" ");
    			String username_entered = divide_data_entered[0];
    			String psw_entered = divide_data_entered[1];
    			
    			String txt_line = reading_file.nextLine();
    			String[] divided = txt_line.split(" ");
    			String username = divided[0];
    			String psw = divided[1];
    			
    			
    			if(username_entered.equals(username)&&psw_entered.equals(psw)){
    				System.out.println("OK LOGIN");
    			}else{
    				System.out.println("DATA ERROR");
    			}
    		}	
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: creation of an algorithm for log in with verification

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at prova.main(prova.java:20)
    The code on line 20 used an index past the end of the array. Check the size of the array using the .length field to be sure it is long enough. The array must have 2 elements to use an index of 1.

    For debugging use the Arrays class's toString() method to print out an array's contents:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: creation of an algorithm for log in with verification

    And in addition to what Norm said, I recommend you move the Scanner(System.in) creation outside the while loop. No need to keep creating a Scanner instance. Then I recommend you print out the values for username and password for both the file and the user. You may also want to print out the input lines as well. Using print statements throughout your program can greatly facilitate debugging.

    Regards,
    Jim
    Last edited by jim829; 02-07-2015 at 02:39 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: creation of an algorithm for log in with verification

    First of all Norm i really wanna say a great thank you because you always try to help me and in general people, i'm new but i can see this... trust me.

    btw i resolved that error, in the txt i saved the example value in this way
    mario
    helloworld

    but they have to be so:
    mario helloworld
    if not now appear obiously to me that there will be an array length error.

    before i made a class (called from my main class) that controll if your name was in the list. i'm improving the system for control an username and psw, and this code is function. but i have some problem to modify this code because i have to have a return value to my main.

    HERE THE OLD CODE WITH THE RETURN VALUE THAT IT WORKED
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class UsersCheck {
    	public int controlloNomi(String name_used) throws FileNotFoundException{
    		
    		Scanner userList = new Scanner(new File("path to file... /usersList.txt"));
    		
    		int val = 0;		
    		while(userList.hasNextLine()){
    			String line = userList.nextLine();
    			
    			if(line.indexOf(name_used) != -1){
    				System.out.println("You are a registered member, WELCOME BACK!");
    				val = 1;
    				break;
    			}else{
    				val = 0;
    				continue;
    			}
    		}
    		if(val == 0){
    			System.out.println("You're not a registered member, RUN TO SIGN UP!");
    		}
    		return val;
    	}
    }

    AND THIS IS THE CODE TO MODIFY TO HAVE A VALUE THAT COME BACK AS IN THE CLASS UP HERE:
    NB: in this code is wrong that i'm asking the data to insert, this must do in my main and so this will must change i nedd to pass to this class the value inserted from the user... another big problem for me
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class prova {
    	public static void main(String[] args) throws FileNotFoundException {
    		
    		System.out.println("INSERT USERNAME AND PASSWORD (USERNAME PSW)");
    		
    		File data = new File("path to file... /provone.txt");
    		Scanner reading_file = new Scanner(data);
    		
    		while(reading_file.hasNextLine()){
    			
    			Scanner name = new Scanner(System.in);
    			String name_entered = name.nextLine();
    			
    			String[] divide_data_entered = name_entered.split(" ");
    			String username_entered = divide_data_entered[0];
    			String psw_entered = divide_data_entered[1];
    			
    			String txt_line = reading_file.nextLine();
    			String[] divided = txt_line.split(" ");
    			String username = divided[0];
    			String psw = divided[1];
    			
    			
    			if(username_entered.equals(username)&&psw_entered.equals(psw)){
    				System.out.println("OK LOGIN");
    			}else{
    				System.out.println("DATA ERROR");
    			}
    		}	
    	}
    }

    so in practice need to combine the functionality of the new class (that is looking for that username for the password) with the functionality of the old (that was able to move back to a value)
    I tried but I fail miserably here is the result (which obviously give errors of any kind)

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class UsersCheck {
    	public int controlloNomi(String name_used) throws FileNotFoundException{
    		
    		Scanner userList = new Scanner(new File("PATH TO FILE... /usersList.txt"));
    		
    		int val = 0;		
    		while(userList.hasNextLine()){
    			
    			String txt_line = userList.nextLine();
    			
    			String[] txt_divided = txt_line.split(" ");
    			String username = txt_divided[0];
    			String psw = txt_divided[1];
    			
    			String[] divide_data_entered = name_used.split(" ");
    			String username_entered = divide_data_entered[0];
    			String psw_entered = divide_data_entered[1];
    			
    			if(username_entered.equals(username)&&psw_entered.equals(psw)){
    				val = 1;
    				break;
    			}else{
    				val = 0;
    				continue;			
    			}
    		return val;
    	}
    }
    when i write the username and psw correct or incorrct the console says to me
    XML Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    	at UsersCheck.controlloNomi(UsersCheck.java:17)
    	at PrimaClasse.main(PrimaClasse.java:21)
    AND another problem is that will be more than 1 line in the txt file (ex. 2 username and 2 psw) the system atm can't handle the second... only 1 line, i think that i'm making a wrong code...
    Last edited by maki; 02-07-2015 at 02:15 PM.

Similar Threads

  1. Image verification
    By pratapsingh in forum Struts
    Replies: 1
    Last Post: 02-23-2013, 02:43 PM
  2. Replies: 0
    Last Post: 05-18-2012, 01:29 PM
  3. Fingerprint verification
    By venkateshcoolmoon in forum Advanced Java
    Replies: 4
    Last Post: 02-02-2010, 06:19 PM
  4. jarsigner verification
    By karan1989 in forum New To Java
    Replies: 1
    Last Post: 11-12-2009, 12:21 PM
  5. Image Verification
    By peiceonly in forum Java Servlet
    Replies: 2
    Last Post: 04-04-2009, 07:38 AM

Posting Permissions

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