0

Question:

  • create a class named Account that contains the string fields name, email and password.
  • Develop a program called LoginSim that simulates a login procedure.
  • The program reads a list of names, email addresses and passwords from a file pw.txt.
  • Store the information in an ArrayList of Account objects. *Note: for Netbeans users the file must be placed in a test folder and accessed with new File("test/pw.txt");

  • Your program will prompt the user for their email address.

    • If the email is not in the system, prompt the user to try again. Give them an option to quit.
    • If the email is found in the system, prompt the user to enter their password.
  • After 3 unsuccessful tries, inform the user that they are locked out and end the program.
  • If the password matches, welcome the user by name and ask if they would like to change their password.
  • If so, prompt for the new password and change it accordingly. If not, end the program by confirming that they have signed out. When the program ends, display the list of accounts.

Sample output:

Enter your email address (q to quit):
draco@hogwarts.com
Email not found, please try again (q to quit):
dmalfoy@hogwarts.com
Email not found, please try again (q to quit):
q
Goodbye!

pw.txt

Hagrid hagrid@hogwarts.com 111
Harry harry@hogwarts.com  killvoldy777
Ron ron@hogwarts.com  mypassword123
Hermione hermione@hogwarts.com  98fJG83h*4iwrej!

What should I do next, exception at line 16

import java.util.*;
import java.io.*;

public class LoginSim {
    private static int index;

    public static void main(String args[]) throws 
FileNotFoundException, ArrayIndexOutOfBoundsException {
        String em;
        String pw;
        Scanner f = new Scanner(new File("src/pw.txt"));
        Scanner kb = new Scanner(System.in);
        String[] email = new String[3];
        String[] password = new String[3];
        int i = 0;
        while (f.hasNext()) {
            email[i] = String.valueOf(f.hasNext());
            password[i] = String.valueOf(f.hasNext());
            i++;
        }

        System.out.println("Enter Email:");
        em = kb.next();

        System.out.println("Enter Password:");
        pw = kb.next();

        if (index != -1) {
            System.out.println("Enter pw:");
            pw = kb.next();
            int tries = 0;
            while (!pw.equals(tries < 2) && 
!pw.equals(password[index])) {
                System.out.println("Incorrect Password, Try Again");
                tries++;
                pw = kb.next();
            }

            if (pw.equals(password[index])) {
                System.out.println("Successful Login");
            }else {
                System.out.println("3 Strikes, Locked out");
            }
            System.out.println("Email not found");
         }
    }
}
  • 1
    Please, format your code properly, and add exception stack trace to the post. In what line exactly it throws outofbounds? – Sergei Sirik Jan 11 '19 at 22:48
  • Possible duplicate of [How to prevent ArrayIndexOutOfBoundsException in Java?](https://stackoverflow.com/questions/5554734/how-to-prevent-arrayindexoutofboundsexception-in-java) – Sergei Sirik Jan 11 '19 at 22:49
  • Also, it would be helpful to provide sample "pw.txt". Take a look at this [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Sergei Sirik Jan 11 '19 at 22:52
  • @sergeiSirik Sorry, Fixed it now. Can you check –  Jan 11 '19 at 22:56
  • See my answer below. – Sergei Sirik Jan 11 '19 at 23:05
  • Change the length of `String[] email = new String[3];` and `String[] password = new String[3];` to 4, you have 4 entries in you **pw.txt**. The exception you're getting is basically the compiler telling you that you're trying to add more items than the array can take, If you would like to improve your code, read about **Dictionary** which is the equivalent of **HashMap** in Java. – Bargros Jan 12 '19 at 00:25

1 Answers1

0

Ok, you are getting java.lang.ArrayIndexOutOfBoundsException: 3 in this line email[i] = String.valueOf(f.hasNext()); because the size of your array is 3 (String[] email = new String[3];), but you are trying to add more then 3 entries to it, at least 4 based on your sample "pw.txt". Also, you have the same issue with your password array.

Sergei Sirik
  • 1,249
  • 1
  • 13
  • 28