1

I have just taken java programming course in college. And we were asked to do a project. However, I am facing a dilemma here and some assistance will be highly appreciated.

        if (opt == 1) {
            System.out.println("Enter your name:");
            name1 = keyboard.next();
            name2 = keyboard.next();
            System.out.println("Enter your ID:");
            ID = keyboard.nextInt();
            System.out.println("Enter semesters taken:");
            semtaken = keyboard.nextInt();
            int[] semcode = new int[semtaken];
            int[] subjectTaken = new int[semtaken];
            double[] GPA = new double[semtaken];
            double finalGPA;
            for (i = 0; i < semtaken; i++) {
                System.out.println("Enter semester code for semester no " + (i + 1));
                semcode[i] = keyboard.nextInt();
                System.out.println("Enter subjects taken:");
                subjectTaken[i] = keyboard.nextInt();
                String[][] subject = new String[subjectTaken[i]][4];
                int subjectsTakenNO = subjectTaken[i];

                System.out.println("Enter your subject code, hours, letter");
                for (j = 0; j < subjectTaken.length; j++) {
                    for (int k = 0; k < 3; k++) {
                        subject[j][k] = keyboard.next();
                    }

                }

                GPA[i] = getGPA(subject, subjectsTakenNO);
                totalGPA += GPA[i];
                finalGPA = totalGPA / semtaken;
                System.out.println("Your GPA for this term is" + getGPA(subject, subjectsTakenNO));
            }
            opt1 = true;
        } /* Here is the PROBLEM. I want to get I want to get semtaken and gpa but it shows an
             error (Variable not found) */ 
        else if (opt == 2 && opt1) {
            summary(name1, name2, ID, GPA, totalGPA, semtaken, semcode);

So basically, I want to use totalGPA,semtaken,semcode and GPA which are inside the IF. How can I use them without showing me an error "Variable not found"? I cant initialize the array before the IF because I dont know its size. The user inputs the size so it has to be inside the IF.

1
  • I tried the solutions below, I appreciate the help BUT the values in the end it shows as zero. I want to retrieve the values that were assigned by the "IF" statement. Commented May 8, 2011 at 8:57

3 Answers 3

5

You can't allocate it but you can introduce it.

int[] list = null;
if (...) {
    list = new int[10];
}
Do what so ever with list.
Sign up to request clarification or add additional context in comments.

1 Comment

this will trigger "is not initialized" when used outside the if
2

You can define it outside the if:

GPA[] array = null; //or new GPA[0];

if (...) {
   array = new GPA[x];
}

2 Comments

Yeah but the values would be ZERO.
Yes.. but if you assign anything they won
0

Just declare (but don't initialize) the variables you need outside the if before the if. For example

int semtaken;
if (opt == 1) {
    ...
    semtaken = ...;
    ...
}
foo(semtaken);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.