读取数据来创建一个类对象在java中是行不通的



好的,那么数据文件看起来很简单:1用户通过名称bday1是行数,现在只有一行。有了这段代码,当我试图打印出用户的日志时它会打印出login@33acf5f7

我如何修改它来打印它是log[0],我如何编写这段代码,以便我最终可以打印log[0]这样的东西。getName或者我写对了吗?

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;
    }
}

你们的login课很奇怪。为什么用char[]代替String ?这很像c。您的System.out.println()导致奇怪输出的原因是因为它打印的是数组的内存位置,而不是您试图表示的字符串。

String代替。

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;
    }
}

还有,关于你的台词:

private String getName;

我不确定你是否试图像在C中一样向前声明你的方法,但你不需要在Java中这样做,所以我删除了这一行。

你的代码有几个问题。

你有login类,它应该被命名为Login

在登录类中,你有一个名为getName的字符串属性,如果你已经有一个名为name的字符串属性,那么就不需要它了,而且getName通常用于getter方法(你已经在使用)。

仍然在login类中创建了一个构造函数:

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

不需要int i属性,因为没有什么可以在你的类中使用它。如果你打算以后再添加一些内容,可以。

你的Login类应该是:

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;
    }
}

现在进入代码的读取部分。如果您计划在文件中使用它作为开始的行数,则必须稍微更改代码以获得所需的一切。所以你的文件看起来像:

1 user pass name bday

然后使用以下代码读取它,只更改try catch块:

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();
}

我已经将for (int i = 0; i < 1; i++) {的第一个更改为while (sc.hasNextLine()) {, Scanner将通过文件的每一行。你从文件中读取数据的方式sc.next();已经在空格之前获得了数据所以当你试图从文件中读取for (int j = 0; i < data; j++)时就会遇到问题如果你已经知道在你的文件中你有第一行的行数并且每个数据都用空格分隔那么就不需要for

所以项目类的readData方法代码应该是:
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();
    }
}

希望能有所帮助。

最新更新