mardi 30 juin 2015

Java: Login System Won't Work [duplicate]

This question already has an answer here:

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!

Aucun commentaire:

Enregistrer un commentaire