-3

ok, so the data file is simple looks like this: 1 user pass name bday the 1 is the amount of rows which is only one right now. Well with this code when I am trying to print out the log of the user it prints this login@33acf5f7

how do I fix this to print that it is log[0] and how do I make this code so I can end up printing things like log[0].getName or do I have that part right itll end up working?

public class Project {

    public Scanner sc;
    login[] log;

    public int readData() {
        sc = new Scanner(System.in);
        try {
            sc = new Scanner(new File("src/input.txt"));
        } catch (Exception e) {
        }

        int rows = sc.nextInt();
        int data = 4;
        // Fill in data
        log = new login[rows];
        try {
            for (int i = 0; i < 1; i++) {
                for (int j = 0; i < data; j++) {

                    String user = sc.next();
                    String password = sc.next();
                    String name = sc.next();
                    String bday = sc.next();
                    log[j] = new login(rows, user, password, name, bday);
                    System.out.println(log[i].getName());
                }
            }
        } catch (Exception e) {
            System.out.println("ERROR HERE");
        }

        return 0;

    }

    public static void main(String[] args) {
        Project proj = new Project();
        proj.readData();

    }

}

class login {

    private char[] user, pass, name, bday;
    private String getName;

    public login(int i, String user, String pass, String name, String bday) {
        this.user = user.toCharArray();
        this.pass = pass.toCharArray();
        this.name = name.toCharArray();
        this.bday = bday.toCharArray();
    }

    public char[] getName() {
        return this.name;
    }
}
Jorge Campos
  • 20,662
  • 7
  • 51
  • 77
  • What is inside of your `src/input.txt` show us at least part of it! – Jorge Campos Oct 12 '15 at 00:35
  • Also take a look at this thread on how to read a file with scanner: http://stackoverflow.com/questions/13185727/reading-a-txt-file-using-scanner if it wont help add a comment – Jorge Campos Oct 12 '15 at 00:37
  • the reading in works... I am trying to ok, so the data file is simple looks like this: 1 user pass name bday the 1 is the amount of rows which is only one right now. Well with this code when I am trying to print out the log of the user it prints this login@33acf5f7 how do I fix this to print that it is log[0] and how do I make this code so I can end up printing things like log[0].getName or do I have that part right itll end up working? The input file looks like this: 1 user pass name bday – Evan coding Oct 12 '15 at 00:42

2 Answers2

0

Your login class is very strange. Why are you using char[] instead of String? That is very C-like. The reason that your System.out.println() is causing weird output is because it's printing the memory location of the array rather than the String that you are attempting to represent.

Use String instead.

class login {

    private String user, pass, name, bday;

    public login(int i, String user, String pass, String name, String bday) {
        this.user = user;
        this.pass = pass;
        this.name = name;
        this.bday = bday;
    }

    public String getName() {
        return this.name;
    }
}

Also, regarding your line:

private String getName;

I am not sure if you were attempting to forward-declare your method like in C, but you don't need to do so in Java, so I removed the line.

RogueBaneling
  • 3,931
  • 4
  • 20
  • 33
0

There are a few problems with your code.

First read about the Java Code Conventions You have the login class which should be named Login

In the Login class you have a String attribute called getName that is no need of it if you already have a String attribute named name also getName is conventionally used for a getter method (which you are already using).

Still on your login class you created a constructor that is:

public login(int i, String user, String pass, String name, String bday)

No need for that int i attribute since there is nothing to use it on your class. If you plan to add something about it later, ok.

Your Login class should be:

public class Login {
    private String user, pass, name, bday;

    public Login(String user, String pass, String name, String bday) {
        this.user = user;
        this.pass = pass;
        this.name = name;
        this.bday = bday;
    }

    public String getName() {
        return this.name;
    }
}

Now for the reading part of your code. If you plan to have the number of lines on your file to use it as a start you will have to change your code a bit to it get everything you need. So your file looks like:

1 user pass name bday

Then to read it you use the following code, changing jut the try catch block:

try {
    while (sc.hasNextLine()) {
        for (int i = 0; i < 1; i++) {
            String user = sc.next();
            String password = sc.next();
            String name = sc.next();
            String bday = sc.next();
            log[i] = new Login(rows, user, password, name, bday);
            System.out.println(log[i].getName());
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

I've change the first for for (int i = 0; i < 1; i++) { to while (sc.hasNextLine()) { To the Scanner would go through every line of your file. They way you are reading the data from your file sc.next(); Already gets the data before the spaces so you had a problem with it when you where trying to read from the file within this for for (int j = 0; i < data; j++) If you already know that in your file you have the first line with the number of lines and each data separeted with a space there is no need to that for

So the readData method code for your Project class should be:

public void readData() {
    sc = new Scanner(System.in);
    try {
        sc = new Scanner(new File("src/input.txt"));
    } catch (Exception e) {
        //As you are starting always use this command on an exception block
        //it will print what happened with your code, it will be easier to
        //help you if we know what happened.
        e.printStackTrace();
    }

    int rows = sc.nextInt();
    int data = 4;
    // Fill in data
    log = new Login[rows];
    /*
    * Since you know that your file has the number of lines first
    * you must read this value before the while so The Scanner class
    * would get it and go on in the same line.
    * If you want to add another line add it whitout the number of lines
    * like this:
    * >2 user pass name bday
    * >user2 pass2 name2 bday2
    * Otherwise you will need to change your code a bit.
    */
    try {
        while (sc.hasNextLine()) {
            for (int i = 0; i < 1; i++) {
                String user = sc.next();
                String password = sc.next();
                String name = sc.next();
                String bday = sc.next();
                log[i] = new Login(rows, user, password, name, bday);
                System.out.println(log[i].getName());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Hope it helps.

Jorge Campos
  • 20,662
  • 7
  • 51
  • 77