0

I have a program where I need to store users information, and add users. To make it persistent, the program reads all the users data and initializes an array of users upon launch, then saves the information before it closes. Here's my user class:

class User {
    String name;
    int val = -1;
    int oldVal = -1;

    public User(String n){
        try{
             BufferedReader dataReader = new BufferedReader(new FileReader("/Users/" + n));
             name = dataReader.readLine();
             val = Integer.parseInt(dataReader.readLine());
             oldVal = Integer.parseInt(dataReader.readLine());
        } catch (Exception e){}
    }

This class reads from files in /users, following the format name.txt

 John
 90
 100

My core class looks like this:

import java.io.BufferedReader;
import java.io.FileReader;

class Core{
    public static void main (String[] args){
        int numUsers = -1;
        BufferedReader nameReader = null;
        User[] users = null;

        try {
            nameReader = new BufferedReader(new FileReader("Users/users.txt"));
            numUsers = Integer.parseInt(nameReader.readLine());
            users = new User[numUsers];

            for (int i = 0; i < numUsers; i++){
                users[i] = new User(nameReader.readLine());
            }
        } catch (Exception e) {
            System.out.println("Something went wrong. Aborting!");
            System.exit(1);
        }
        for (int i = 0; i < numUsers; i++){
            System.out.println("User " + users[i].getName() + "\n Val:" + users[i].getVal() + "\n oldVal: " + users[i].getOldVal());
        }
    }
}

But running core returns:

User null
val: -1
oldVal: -1

for every user. What is the problem? Is the system I've made viable, or do I need to change the foundation of my program entirely?

(EDIT to change tags)

5
  • I guess an exception occurs in your constructor, but due to this line catch (Exception e){}, you'll ignore that. Write e.printStackTrace() into that catch block to see the exception. And if you get one, please edit your question and post the stacktrace here. Commented Oct 4, 2014 at 14:06
  • @tom I get a compile time error: "Cannot find symbolm, symbol: printStacktrace() location: variable e of type exception" Odd.EDIT: capitalization error, oops. Commented Oct 4, 2014 at 14:09
  • because I had a typo in it. The correct spelling is printStackTrace() (uppercase T). I edited the first comment. Commented Oct 4, 2014 at 14:11
  • @tom I was specifying the files as a path. Adding '+ ".txt"' should fix it, checking now. EDIT: I also needed to get rid of the slash before "Users". Commented Oct 4, 2014 at 14:12
  • Don't forget to read chiastic-securitys answer. His remark is correct. Commented Oct 4, 2014 at 14:14

2 Answers 2

1

Your core is looking for a file in the relative path Users/. Your other class is looking in the absolute path /Users/.

Your textual description says you want /users/ (lower case). On some systems, this will be different again.

I suspect this is causing the problem, or at least part of it.

Looks like what you really want is a relative path, and a .txt on the end:

BufferedReader dataReader = new BufferedReader(new FileReader("Users/" + n + ".txt"));
Sign up to request clarification or add additional context in comments.

1 Comment

The additional slash before should not be there, as the path is supposed to be relative. The .txt extension is also missing. I'm unable to mark my own answer, so if you could edit yours to include this I'd be grateful! (and thanks for answering!)
0
 BufferedReader dataReader = new BufferedReader(new FileReader("/Users/" + n));

Should be

 BufferedReader dataReader = new BufferedReader(new FileReader("Users/" + n + ".txt"));

Notice the removal of the extra forward slash and the addition of the .txt extension.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.