0

Does anyone have any ideas how I can complete my code for when a user logs in using their username/password for the first time it notify s them that a new account has been created and then create a text file, with the user/pass in it. Whilest having it proceed to let users with accounts log in as usual.

Here is my code so far. It will read a text file for the username but it will receive a run time error on pass.

import java.util.Scanner;

public class Login{
  public static void main (String[] args) {
    Scanner scan = new Scanner("Libraries/Documents/userPass.txt");
    Scanner keyboard = new Scanner(System.in);
    String user = scan.nextLine();
    String pass = scan.nextLine();

    String inpUser = keyboard.nextLine();
    String inpPass = keyboard.nextLine();

    if(inpUser.equals(user) && inpPass.equals(pass)){
      System.out.print("Welcome");
    } 
    else{
      System.out.print("Password or Username is incorrect"); 
    }
  }
}
3
  • Mind sharing the details about the error you are getting and stack trace that accompanies it? Commented Aug 26, 2014 at 1:42
  • You need to check to see if user or pass is null...in fact, you should be checking to see if the file contains a value for user (scan.hasNextLine) - if this is false, then you need to enter "new user mode" Commented Aug 26, 2014 at 1:49
  • @MadProgrammer hope you are fine. I think you will love my post here lol Commented Aug 26, 2014 at 2:47

3 Answers 3

1

You would better use a XML file to store the usernames and passwords. It's easy to modify and read. And also you can trigger an error messages like User Already Exist if that username contains in XML file. Format will looks like(You can change this format as your choice),

<users>
    <user id="1">
        <username>User 1</username>
        <password>User 1 password</password>
    </user>

    <user id="2">
        <username>User 2</username>
        <password>User 2 password</password>
    </user>
</users>

and try to encrypt the password for more security. You can check with the username, if signing in user is already exist or not. If not exist, you can write it into the XML and notify a message like A new account has been created.

Here is the tutorial of XML reading and writing.

Read and Write XML in Java

Here is the modification of your code.

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

public class Login {
    public static void main(String[] args) {
        Scanner scan = null;
        File file = new File("Libraries/Documents/userPass.txt");

        try {

             scan = new Scanner(file);
             FileWriter fw = new FileWriter(file, true); //the true will append the new data
             Scanner keyboard = new Scanner(System.in);
             String user = "";
             String pass = "";
             while (scan.hasNext()) {
                 user = scan.nextLine();
                 pass = scan.nextLine();
             }
             String inpUser = keyboard.nextLine();
             String inpPass = keyboard.nextLine();

            if (inpUser.equals(user) && inpPass.equals(pass)) {
                 System.out.print("Welcome");
            } else {
                 System.out.println("Password or Username is incorrect");
                 fw.write("\n" + inpUser + " " + inpPass);//appends the string to the file
                 fw.close();

                 System.out.println("New Account has been created!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
     }
}

Hope this will help.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks MadProgrammer. I've added the code modification.
0

Code:

   int i = 0;
    String fileName = "C:\\Users\\J Urguby\\Documents"
            + "\\NetBeansProjects\\UserPassPageScanner\\src\\userpasspagescanner\\userPass.txt";
    File file = new File(fileName);
    while (i == 0) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter your user name plz:");
        String user = keyboard.next();
        System.out.println("Enter your password plz");
        String pass = keyboard.next();

        try (Scanner input = new Scanner(file)) {
            while (input.hasNextLine()) {
                String[] line = input.nextLine().split(" ");
                if (line[0].equals(user) && line[1].equals(pass)) {
                    System.out.print("Welcome");
                    i = 1;
                } 
           }
             if(i==0) System.out.println("Password or Username is incorrect");
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output:

enter image description here

File Content:

kick 123456
hello 234568

Note: if you need any clarification please let me know

8 Comments

How is this checking to see if the user is a new user or not?
basically I have to give the whole answer? this is just an idea. op needs to put some effort to get to point op wants.
It's just unobvious as (not to mention cryptic on the part of the OP). May be highlight where a new user account would need to be created as opposed to where validation fails...
@MadProgrammer you are right cuz the op never asked for that
May be I missed something "Does anyone have any ideas how I can complete my code for when a user logs in using their username/password for the first time it notify s them that a new account has been created" but seems to that the OP is asking, when a user first runs the program they are greeted with a "create an account" style message/process...The problem with this is it assumes that an invalid user AND password is a new user...
|
0

Let's start with, Scanner scan = new Scanner("Libraries/Documents/userPass.txt");, which isn't doing what you think it is. Instead of reading the file userPass.txt, it is using the String value as the contents for the Scanner...

Instead you should be using Scanner(File) instead...

In this case, all you really need to do is check for the existence of the File to determine if it's a new user or not, for example...

File passes = new File("userPass.txt");
try {
    String user = null;
    String pass = null;
    if (passes.exists()) {
        try (Scanner scan = new Scanner(passes)) {
            user = scan.nextLine();
            pass = scan.nextLine();
        }
    }

    if (user == null) {

        System.out.println("Welcome new user, please enter the user name and password for your new account...");

    } else {

        System.out.println("Welcome back user, please enter your user name and password...");

    }

    Scanner keyboard = new Scanner(System.in);

    String inpUser = keyboard.nextLine();
    String inpPass = keyboard.nextLine();

    if (user == null) {
        // Create user account...
    } else {
        if (inpUser.equals(user) && inpPass.equals(pass)) {

            System.out.print("Welcome");

        } else {

            System.out.print("Password or Username is incorrect");

        }
    }
} catch (IOException ex) {
    ex.printStackTrace();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.