I am trying to make an program to determine averages for school. I am going to have all the files saved to the computer so its easy to access them for multiple uses. I stated to create multiple methods and discovered a problem. I have the user input for a subject in the startup method but in the main method, sub (the subject string) is used and its says "sub cannot be resolved to a variable" I understand why it say this but I am unsure how to fix. Here is my code:
public class Calculator {
static int x;
static int b;
public static void startup() {
System.out.println("**Receiving User**");
String user = System.getProperty("user.home");
System.out.println("**Checking Directories**");
boolean dir = new File(user + "/Library/Application Support/Average_Calculator/Settings/").mkdirs();
if (dir) {
System.out.println("**Directory Created at:" + user + "/Library/Application Support/Average_Calculator/**");
} else {
System.out.println("**Directory Has Already Been Created at:" + user
+ "/Library/Application Support/Average_Calculator/**");
}
System.out.println("Welcome to the Average Calculator");
System.out.println("Please input the subject average you want to calculate(no caps)");
Scanner scan = new Scanner(System.in);
String sub = scan.nextLine();
// System.out.println(sub);
try {
// System.out.println("It Does!");
FileOutputStream saveFile = new FileOutputStream(
user + "/Library/Application Support/Average_Calculator/" + sub + ".sav");
ObjectOutputStream save = new ObjectOutputStream(saveFile);
FileOutputStream SetsaveFile = new FileOutputStream(
user + "/Library/Application Support/Average_Calculator/Settings/" + "Setting" + sub + ".sav");
ObjectOutputStream setsave = new ObjectOutputStream(SetsaveFile);
// Create an ObjectOutputStream to put objects into save file.
// Close the file.
save.close();
setsave.close();// This also closes saveFile.
} catch (Exception exc) {
exc.printStackTrace(); // If there was an error, print the info.
}
}
public static void main(String[] args) {
startup();
System.out.println(sub);
try {
// Open file to read from, named SavedObj.sav.
FileInputStream saveFile = new FileInputStream(sub + ".sav");
// Create an ObjectInputStream to get objects from save file.
ObjectInputStream save = new ObjectInputStream(saveFile);
x = (Integer) save.readObject();
b = (Integer) save.readObject();
// Close the file.
save.close(); // This also closes saveFile.
} catch (Exception exc) {
// exc.printStackTrace(); // If there was an error, print the info.
}
// Print the values, to see that they've been recovered.
System.out.println(x);
System.out.println(b);
// All done.
}
}
Thanks for the help! PS I am new to methods and classes, an explanation would be greatly appreciated!