Hey everyone,

I'm currently writing a simple text based MUD in Java for learning purposes.

Currently I'm in the process of coding a Login class that prompts the user to enter a user name and password in order to access the account.

Right now the problem lies in opening a .data file that contains user names and passwords. The syntax seems correct, and the actual .data file is in the same source folder as my main and login classes.

Everything runs fine until the opening of the file.

Here is my code for each of the 3 files.

Engine.java

//import java.util.*;

class Engine {
    //public static String cname;


    public static void main(String[] args) {

        Login.start();
       
    }
}

Everything is fine here.

Login.java

import java.util.*;
import java.io.*;
/**
 *
 * @author Silva
 */
public class Login extends Engine {
    private static String User, Password, checkUser, checkPW;
    public static boolean userFound = false, pwMatch = false, userMatch = false;
    
    public static void start() {
        Scanner sc = new Scanner(System.in);
                
        System.out.print("Welcome to Fyron! Enter user name: ");
        User = sc.nextLine();
        System.out.print("Enter password: ");
        Password = sc.nextLine();

        checkUserPW();
    }

    public static void checkUserPW() {
        try{     //THIS IS WHERE I USE SCANNER TO FIND AND OPEN USERLIST.DATA
            Scanner file = new Scanner(new File("UserList.data"));
            file.useDelimiter(System.getProperty("line.separator"));

            while(file.hasNext()) {
                parseLine(file.next()); // Call parseLine() with current line
            }
            file.close();
        }catch (FileNotFoundException e) {
         e.printStackTrace();
       }
        
        if(userFound){                         //Checks to see if user was found
            System.out.println("Welcome "+User);
        }
        else if(userMatch==true&&pwMatch==false) {     //Checks for incorrect PW
            System.out.println("Password incorrect...");            
        }                
        else if(userMatch==false) {                //Checks if new user
            System.out.println("New User");
        }            
    }

    public static void parseLine(String line) {
        Scanner lScanner = new Scanner(line);
        lScanner.useDelimiter(",");
        checkUser = lScanner.next();

        if(checkUser.equals(User)) { // Compares current name with entered User name
            userMatch=true;
            checkPW = lScanner.next();

            if(checkPW.equals(Password)) // Compares current Password to entered password
                userFound=true;
        }

    }
}

Line 24 is where I use Scanner to find and open UserList.data which contains the user names and passwords.

UserList.data

MyName,MyPassword
Name,Password

Program runs fine at start, user is prompt to enter name and password. After the password is entered I get this error, specifically on line 4. "System cannot find the file specified"

Output:

run:
Welcome to Fyron! Enter user name: Name
Enter password: Password
java.io.FileNotFoundException: UserList.data (The system cannot find the file specified)
New User
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at java.util.Scanner.<init>(Scanner.java:636)
        at Login.checkUserPW(Login.java:24)
        at Login.start(Login.java:19)
        at Engine.main(Engine.java:9)
BUILD SUCCESSFUL (total time: 16 seconds)

Any advice is great, thank you.

Anthony~

>Issue opening data file with Scanner

Look at the error message : java.io.FileNotFoundException: UserList.data (The system cannot find the file specified).

You have to specify the path of data file.

...
            File datafile=new File("x:\\dir1\\dir2\\UserList.data");
            if(!datafile.exists()) {
                System.out.println("File not found");
                return;
            }
            Scanner file = new Scanner(datafile);
...

>Issue opening data file with Scanner

Look at the error message : java.io.FileNotFoundException: UserList.data (The system cannot find the file specified).

You have to specify the path of data file.

...
            File datafile=new File("x:\\dir1\\dir2\\UserList.data");
            if(!datafile.exists()) {
                System.out.println("File not found");
                return;
            }
            Scanner file = new Scanner(datafile);
...

I see, thanks a bunch!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.