1

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!

JT attack
  • 87
  • 1
  • 1
  • 8

3 Answers3

2

sub is currently a local variable of startup(), so main() does not have access to it.

One solution is to have startup() return the value of sub, and to have main() use that returned value.

A second solution would be to declare sub (and any other shared variables) as a static variable of the Calculator class, which would place it within the scope of both static methods. In this case, you must no longer declare sub locally within startup(), since that would cause the method to ignore the static variable with the same name.

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • At the end of startup i put return sub; but it still say "sub can't be resolved to a variable" – JT attack Aug 28 '15 at 22:31
  • 1
    `main()` now has to use the returned value! Try changing the statement that calls `startup()` to `String sub = startup();`. I have updated my answer a bit. – cybersam Aug 28 '15 at 22:32
  • Thank you it works now one last question can I have startup return multiple Strings because I need to do the same thing as I did to sub as to user – JT attack Aug 28 '15 at 22:41
  • The issue with that is I need the user input in startup and by doing what you said I can't do that anymore. Thanks for helping me – JT attack Aug 28 '15 at 23:04
  • No, with the second solution, `startup()` can still *use* `sub` -- it just must not *declare* it (since that would cause the method to ignore the `static` variable with the same name). In other words, `String sub = scan.nextLine();` would simply need to be changed to `sub = scan.nextLine();`. – cybersam Aug 28 '15 at 23:20
0

You are declaring sub in your startup method but trying to access it in your main method. The scope of variables declared in methods is limited to that method; that means that once you leave the method, the variable is no longer accessible. If you want to access a variable both methods one option is to make it a property of the class like you did for x and b. Another option is to return the value of sub at the end of your startup method then simply print your call to the method in main.

Here is a good explanation about classes. Here is a good explanation about methods (you can ignore the part about functions; the explanation for methods is still good).

Community
  • 1
  • 1
Zarwan
  • 5,537
  • 4
  • 30
  • 48
0

Your startup function is returning a void. Change this line:

public static void startup() {

to this:

public static String startup() {

and this line:

startup();

to this:

String sub = startup();
Daniel Burgner
  • 224
  • 2
  • 6
  • 16