1

I've been making a login system prototype for my game, sad thing is it doesn't work! I'm not sure why but here's my code:

This is the code for my main class:`

package darkbyte.tests;

import java.util.Scanner;

public class Main {

    private static FileCreator creator = new FileCreator();
    private static FileReader reader = new FileReader();

    private static void login() {
        Scanner input = new Scanner(System.in);

        boolean passwordExists;

        System.out.println();
        System.out.println("Please enter your username: ");
        String username = input.nextLine();
        System.out.println("Please enter your password: ");
        String password = input.nextLine();

        reader.openFile(username + ".user");
        if(reader.doesStringExist(password)) {
            passwordExists = true;
        } else {
            passwordExists = false;
        }

        reader.closeFile();

        if(passwordExists) {
            System.out.println("Welcome back to Darkbyte " + username + "!");
        } else {
            System.out.println("The password you entered is incorrect!");
            login();
        }
    }

    private static void register() {
        Scanner input = new Scanner(System.in);

        System.out.println();
        System.out.println("Please enter the username you desire: ");
        String username = input.nextLine();
        System.out.println("Please enter the password you desire: ");
        String password = input.nextLine();

        creator.openFile(username + ".user");
        creator.writeString(username);
        creator.writeString(password);
        creator.closeFile();
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Welcome to Darkbyte!");
        System.out.println("Would you like to login or register an account?"); 
        System.out.println("Enter 0 to login and any other number to register: ");
        int choice = input.nextInt();

        if(choice == 0) {
            login();
        } else {
            register();
        }
    }
}

This is the code for my FileCreator class:

package darkbyte.tests;

import java.io.*;

import java.util.*;

public class FileCreator {

    private Formatter format;

    public void openFile(String file) {
        try {
            format = new Formatter(file);
        } catch(Exception e) {
            System.err.println("Darkbyte: There was an error in opening the file!");
        }
    }

    public void closeFile() {
       format.close();
    }

    public void writeInteger(int i) {
        format.format("%i%n", i);
    }

    public void writeFloat(float f) {
        format.format("%f%n", f);
    }

    public void writeString(String s) {
        format.format("%s%n", s);
    }

    public void appendInt(String file, int i) {
        try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)))) {
            out.println(i);
        }catch (IOException e) {
            System.err.println("Darkbyte: Couldn't find file!");
        }
    }

    public void appendFloat(String file, float f) {
        try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)))) {
            out.println(f);
        }catch (IOException e) {
            System.err.println("Darkbyte: Couldn't find file!");
        }
    }

    public void appendString(String file, String s) {
        try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)))) {
            out.println(s);
       }catch (IOException e) {
             System.err.println("Darkbyte: Couldn't find file!");
        }
    }
}

This is the code for my FileReader class:

package darkbyte.tests;

import java.io.*;

import java.util.*;

public class FileReader {

    private Scanner scanner;

    public void openFile(String file) {
        try {
            scanner = new Scanner(new File(file));
        } catch(Exception e) {
            System.err.println("Darkbyte: Couldn't find file!");
        }
    }

    public void closeFile() {
        scanner.close();
    }

    public void readInt(int i, int lookingFor) {
        while(i != lookingFor) {
            if(scanner.hasNext()) {
                i = Integer.parseInt(scanner.next());
            }
        }
    }

    public void readFloat(float f, float lookingFor) {
        while(f != lookingFor) {
            if(scanner.hasNext()) {
                f = Float.parseFloat(scanner.next());
            }
        }
    }

    public void readString(String s, String lookingFor) {
        while(s != lookingFor) {
            if(scanner.hasNext()) {
                s = scanner.next();
            }
        }
    }

    public boolean doesIntExist(int i) {
        boolean intIsFound = false;
        int tempInt;

        while(scanner.hasNext()) {
            if(!intIsFound) {
                tempInt = Integer.parseInt(scanner.next());
                if(tempInt == i) {
                    intIsFound = true;
                } else {
                    intIsFound = false;
                }
            }
        }

        return intIsFound;
    }

    public boolean doesFloatExist(float f) {
        boolean floatIsFound = false;
        float tempFloat;

        while(scanner.hasNext()) {
            if(!floatIsFound) {
                tempFloat = Float.parseFloat(scanner.next());
                if(tempFloat == f) {
                    floatIsFound = true;
                } else {
                    floatIsFound = false;
                }
            }
        }

        return floatIsFound;
    }

    public boolean doesStringExist(String s) {
        boolean stringIsFound = false;
        String tempString;

        while(scanner.hasNext()) {
            if(!stringIsFound) {
                tempString = scanner.next();
                if(tempString == s) {
                    stringIsFound = true;
                } else {
                    stringIsFound = false;
                }
            }
        }

        return stringIsFound;
    }
}

The issue is, when I try to login, even if my username and password are correct, it still says that my details are incorrect!

I'm not sure why but can you help me out please!

3 Answers3

1

I think the error is in

if(tempString == s) {
    stringIsFound = true;
} else {
    stringIsFound = false;
}

Change it to:

if(tempString.equals(s)) {
    stringIsFound = true;
} else {
    stringIsFound = false;
}

Always use equals when comparing Strings, otherwise you are comparing their reference and not their value.

Tom Jonckheere
  • 1,630
  • 3
  • 20
  • 37
0

You cannot compare strings like this:

if(tempString == s) {

You must use equals... So instead of == compare with equals() and fill your boolean like this:

stringIsFound = tempString.equals(s);
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
-1

Your code is quite unlogical. comparing a password you entered by command line to another one you enter through the command line?

As OldProgrammer already stated: you'll need to use the .equals method to compare the Strings on value equality. == will only test them for referential equality.

Also, a small remark. Code like this:

if(reader.doesStringExist(password)) {
            passwordExists = true;
        } else {
            passwordExists = false;
        }

can easily be refactored to a slightly more efficiënt and easier to read snippet:

passwordExists = reader.doesStringExist(password);
Stultuske
  • 9,296
  • 1
  • 25
  • 37