Question

I am creating a program that will allow users to sign in with a username and...

I am creating a program that will allow users to sign in with a username and password. Their information is saved in a text file. The information in the text file is saved as such:

Username

Password

I have created a method that will take the text file and convert into an array list. Once the username and password is found, it will be removed from the arraylist and will give the user an opportunity to sign in with a different username and password. Also, the user has 3 tries to correctly sign in, if they are unable the program will terminate.

The problem with my program is that my program will not compile and I am getting issues such as: error: illegal start of type and error: <identifier> expected. These errors are coming from my method that converts the text file into an arraylist. Also, there maybe more bugs in there that I am unware of. Any help would be appreciated. Below is my code:

import java.io.*;
import java.util.*;
public class LoginPagev2
{
   public static void main(String[] args) throws IOException
   {
       String answ;
       String user;
       String pass ;
       int counter = 0;
       Scanner keyboard = new Scanner(System.in);
       System.out.println("Welcome! This program will allow you to sign in with a username and password you created.");
       System.out.println("If you fail to login correctly 3 times, this program will terminate. You can only sign in once per username.");
       System.out.println("If you wish to sign in into the same account twice, you must restart the program.");
       while(true)
       {   System.out.print("Would you like to sign in? (Y/N): ");
           answ = keyboard.nextLine();
           if(answ.equalsIgnoreCase("N"))
           {
               System.out.print("Goodbye!");
               System.exit(0);
           }
           System.out.print("What is your username?: " );
           user = keyboard.nextLine();
           System.out.print("What is your password?: ");
           pass = keyboard.nextLine();
           for (int i = 0; i<list().size(); i++)
           while((!list().contains(user) || !list().contains(pass)) || counter < 3)
           {
               ++ counter;
               if(list().contains(user) || list().contains(pass))
               {
                       list().remove(user, pass);
                       System.out.print("Congrats! You successfully signed in!");
                       counter = 0;
                       break;
               }
               if(counter==3)
               {
                   System.out.println("You have attempted you max amount of tries. This program will now end.");
                   System.exit(0);
               }
               else
               {
                   System.out.println("This is not valid. Please try again");
                   System.out.print("What is your username?: " );
                   user = keyboard.nextLine();
                   System.out.print("What is your password?: ");
                   pass = keyboard.nextLine();
                   //for (int i = 0; i<list().size(); i++)
               }
           }
           System.out.print("Would you like to sign in with another account? (Y/N): ");
           String rep = keyboard.nextLine;
           if(rep.equalsIgnoreCase("N"))
           {
               System.out.print("Goodbye!");
               System.exit(0);
           }
           else
               continue;
       }
      
   }          
  
   public static ArrayList<String> list()
   {
       BufferedReader reader = new BufferedReader(new FileReader("loginInfo.txt"));
       String line;
       ArrayList<String> arr = new ArrayList<String> ();
       while ((line = reader.readLine()) != null)
       {
           arr.add(line);
       }
       reader.close();
   }
   return arr;
}
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You are getting an illegal start of type error because, in list() method, you are not returning the arr inside the method. The code to return the array list arr is outside the method. It should be inside the method.

public static ArrayList<String> list() { try { BufferedReader reader = new BufferedReader (new FileReader(loginInfo.txt));

Also, there should be the try-catch block to handle any exception that might occur on file processing. And it should return in case of any exception.

I found other errors in your code, which are as follows:

  • When you are checking for username and password in the ArrayList, the logical operator between the two conditions should be AND(&&) not OR(||).
  • You cannot remove both username and password from the list in a single call. You need to call it two times to remove username and password.

for (int i = 0; i < list().size(); i++) { while ((!list().contains (user) || !list().contains (pass)) || counter <3) { ++coun

  • You forgot to put brackets in the nextLine() method:

System.out.print(Would you like to sign in with another account? (Y/N): ); String rep = keyboard.nextLine(); if (rep.equals

Below is the sample output after fixing your code:

este si me pas com run: Welcome! This program will allow you to sign in with a username and password you created. If you fail

Below is the content of the input file:

myuser
myPassword

This completes the requirement. Let me know if you have any questions.

Thanks!

Add a comment
Know the answer?
Add Answer to:
I am creating a program that will allow users to sign in with a username and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone fix the program below so it does what the picture says it won't work...

    Can someone fix the program below so it does what the picture says it won't work for me. import java.util.Scanner; public class Userpass { static String arr[]; static int i = 0; public static void main(String[] args) { String username, password; int tries = 0, result; do { System.out.print("Enter the username: "); username = readUserInput(); System.out.print("Enter the password: "); password = readUserInput(); result = verifyCredentials(username, password); tries++; if (result == -1) System.out.println("The username is incorrect!\n"); else if (result == -2)...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • I need help ASAP on this, this is due at midnight PST. This is the current...

    I need help ASAP on this, this is due at midnight PST. This is the current code I have. How can I allow the user to quit. My counting while loop works fine, but I would like it to not keep outputting username if a file was successfully opened. This is what is required. Prompt You have assumed the role of managing the technology infrastructure at a zoo. You will develop a working program (either an authentication system or a...

  • in python 5.23 Login Create a program that prompts the user for a username and password....

    in python 5.23 Login Create a program that prompts the user for a username and password. The correct username should be python and the correct password should be csci 135. See below for example output. (Different messages depending on if user name or password are correct.) Only allow the user to make 3 attempts. Welcome to my secret program! Please enter your username: Trish Please enter your password: Duce Invalid username and password Please enter your username: Trish Please enter...

  • 1. Create a program to verify a user name and password given by a user 2....

    1. Create a program to verify a user name and password given by a user 2. Create a text file named "correctData.txt" (You will NOT upload this file during submission). Place a username on line 1 in the txt file and a password on line 2 in the file. This will be used for testing. 3. Create the following functions and call them starting from main: • void login () - This function is responsible for displaying the prompts asking...

  • User Profiles Write a program that reads in a series of customer information -- including name,...

    User Profiles Write a program that reads in a series of customer information -- including name, gender, phone, email and password -- from a file and stores the information in an ArrayList of User objects. Once the information has been stored, the program should welcome a user and prompt him or her for an email and password The program should then use a linearSearch method to determine whether the user whose name and password entered match those of any of...

  • Written in Java I have an error in the accountFormCheck block can i get help to...

    Written in Java I have an error in the accountFormCheck block can i get help to fix it please. Java code is below. I keep getting an exception error when debugging it as well Collector.java package userCreation; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import java.util.regex.Matcher; import java.util.regex.Pattern; import javafx.event.ActionEvent; import javafx.fxml.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.text.Text; import javafx.stage.*; public class Controller implements Initializable{ @FXML private PasswordField Password_text; @FXML private PasswordField Confirm_text; @FXML private TextField FirstName_text; @FXML private TextField LastName_text;...

  • I have a table with username and passwords that I want to input in Hotmail's username...

    I have a table with username and passwords that I want to input in Hotmail's username and password box. Is there a way to actually program this? Are you allowed to manipulate text boxes of someone else's webpage? Not for illegal purposes, just to make my job easier and to not have to input values manually one by one. Hotmail is just an example.

  • I am creating a program where a user will enter in a price amount. And then...

    I am creating a program where a user will enter in a price amount. And then the program will determine how many 20, 10, 5, 1, etc.. they get back. I keep getting "error: unexpected type" in my findValue method. Not too sure why I am getting this. There also might be more bugs in there that I am not aware of because it wont compile. Any help is appreciated. Here is my code: import java.util.Scanner; public class prac1 {...

  • Look for some finshing touches java help with this program. I just two more things added...

    Look for some finshing touches java help with this program. I just two more things added to this code. A loop at the end that will ask the user if they want to quit if they do want to quit the program stops if they don't it loads a new number sequence. import java.util.Random; import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.Collections; public class main { public static void main(String[] args) { List < Sequence > list =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT