4

Is there a way to access the object "rec" after the for loop when I try to print out rec.report()?

(Report() is a method inside of the class BmiRecord that returns new calculated results).

for(int i=0; i<limit; i++)
{
     int height = scanner.nextInt();
     int weight = scanner.nextInt();
     String name = scanner.nextLine();

     BmiRecord rec = new BmiRecord(name, height, weight);
} 

    System.out.println(rec.report());
4
  • 2
    Because of scope. google.com/… The workaround is to define the object BmiRecord rec = null outside for for loop, then only assign it inside the loop. Then you can use it after the loop terminates Commented Apr 2, 2015 at 15:40
  • Rec is defined inside the loop. So it no long exists outside. Commented Apr 2, 2015 at 15:41
  • Each thing has a scope and it is not visible outside that. In this case the for loop has a scope delimited by curly braces Commented Apr 2, 2015 at 15:41
  • Either do what @Kon wrote, or create a collection like 'List<BmiRecord> records = new ArrayList<>();' and then add the records to it: 'records.add(rec);'. Later you can iterate over that list (examples can be found on Google or Stackoverflow) to print each entry. Commented Apr 2, 2015 at 15:49

3 Answers 3

2

You cannot access the object rec outside the for loop because the scope of the object is only valid in the for loop. As you have created that object inside the for loop.

You can relate this with another question. Why can't you access the local variable defined inside a function in another function?

Refer the following code:

BmiRecord rec[]=new BmiRecord[limit];

for(int i=0; i<limit; i++)
{
 int height = scanner.nextInt();
 int weight = scanner.nextInt();
 String name = scanner.nextLine();

 rec[i] = new BmiRecord(name, height, weight);
} 
for(BmiRecord re:rec){
     System.out.println(re.report);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Is there any way to access the object "rec"?
An array is not wrong, but using a Collection instead is almost always the better decision. Just a note, no need to update your answer :).
You are right. However, OP seems to be new to the language that's why I skipped the Collection part.
1

Because rec is a private variable defined within the for loop. To access outside of it's scope you need to define it before the for loop. Here is your new code:

BmiRecord rec;

for(int i=0; i<limit; i++)
{
 int height = scanner.nextInt();
 int weight = scanner.nextInt();
 String name = scanner.nextLine();

 rec = new BmiRecord(name, height, weight);
} 

System.out.println(rec.report());

Comments

0

You are accessing object outside of loop which is out of scope, try something like this

    BmiRecord rec = null;
    for (int i = 0; i < limit; i++) {
        int height = scanner.nextInt();
        int weight = scanner.nextInt();
        String name = scanner.nextLine();

        rec = new BmiRecord(name, height, weight);
    }

    System.out.println(rec.report());

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.