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)
catch (Exception e){}, you'll ignore that. Writee.printStackTrace()into that catch block to see the exception. And if you get one, please edit your question and post the stacktrace here.printStackTrace()(uppercaseT). I edited the first comment.chiastic-securitys answer. His remark is correct.