Advertisement
Guest User

Untitled

a guest
Feb 17th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2. import java.io.*;
  3. import java.util.*;
  4. public class user
  5. {
  6.     public static ArrayList<ArrayList<String>>  users;
  7.     public static void main(String[] args) throws IOException
  8.     {
  9.         String[] details = usernameValidation();
  10.         if(details[0]!= "") //Empty string is assigned to the first index if the username
  11.         {                   //and password are invalid in usernameValidation method
  12.             String username = details[0];
  13.             int userScore = Integer.valueOf(details[1]); //Gave the details simple variable names for ease of use
  14.         }
  15.     }
  16.     public static String[] usernameValidation() throws IOException
  17.     {
  18.         boolean validInput = false;
  19.         int chances = 3;
  20.         String username = new String();
  21.         String password = new String();
  22.         String userScore = new String();
  23.  
  24.         File inputFile = new File("users.txt");
  25.         users = new ArrayList<ArrayList<String>>();
  26.         users.add(new ArrayList<String>());
  27.         users.add(new ArrayList<String>());
  28.         users.add(new ArrayList<String>()); //set up my array list for the user details
  29.        
  30.         String fileElements[];
  31.        
  32.         Scanner in = new Scanner(inputFile); //makes connection between scanner object and the file
  33.        
  34.         while(in.hasNext()) //for each row in the list, loop
  35.         {
  36.             fileElements = (in.nextLine()).split(","); //puts the details into an array by using a comma to split the details
  37.             users.get(0).add(fileElements[0]);        //the users.txt file is in the format 'username','password',score
  38.             users.get(1).add(fileElements[1]);
  39.             users.get(2).add((fileElements[2]));    //adds each seperate users details into the array list for ease of use
  40.         }
  41.        
  42.         in.close(); //ends association between scanner and file
  43.    
  44.         while(!validInput && chances!=0)
  45.         {
  46.             username = JOptionPane.showInputDialog(null,"Enter your username you have only " + chances + " chance(s) remaining");
  47.             password = JOptionPane.showInputDialog(null,"Enter your password"); //Allows user to enter name and password
  48.             for(int j = 0;j<users.get(0).size();j++)                            //and stores them in variables
  49.             {      
  50.                 //goes through each element in the list
  51.                 if(username.equals(users.get(0).get(j)) && password.equals(users.get(1).get(j))) //compares details entered to each element
  52.                 {                                                                               //in the array lists
  53.                     validInput = true;
  54.                     JOptionPane.showMessageDialog(null,"Login details are valid");
  55.                     userScore = users.get(2).get(j); //Since we have username, we must access score when details are at correct index
  56.                 }
  57.             }
  58.             if(!validInput)
  59.             {
  60.                 chances--; //decrement chances if the details entered are incorrect
  61.                 JOptionPane.showMessageDialog(null,"Login details entered were invalid");
  62.             }
  63.         }
  64.         if(validInput)
  65.         {
  66.             String[] details =  {username,userScore};
  67.             return details; //returns the details required
  68.         }
  69.         else
  70.         {
  71.             String[] details = {"",""};
  72.             return details; //If the input is incorrect, return these values
  73.         }
  74.     }  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement