-2

Java is saying there is an error on the code below and I cant find it. I have indicated the 2 problem lines with a comments right above the lines reading "Problem line" and I have indicated the method closely related to this problem lines with a "Closely related" comment right above it. The error reads that "it cant resolve the symbol processed" and I dont why the heck not since I have in fact defined the variable (where I have marked problem line 1) . It really stomped me when I noticed that it gives a warning that the "variable processed is never used" in the spot where I defines it, even as it tells me that it dont know where it came from at the place where I try to use it. p.s., I do have a Scanner imported, i just that stackoverflow cut that part when they were uploading the code.

public class sub {
    String NameNum;
    int FirstNum, SecondNum, ThirdNum;

    public sub(){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter List Name");
        NameNum = input.next();
        System.out.println("Enter number");
        FirstNum = input.nextInt();
        System.out.println("Enter number");
        SecondNum = input.nextInt();
        System.out.println("Enter number");
        ThirdNum = input.nextInt();

       // Problem Line 1 of 2 below
       int[] processed = process(FirstNum, SecondNum, ThirdNum);

    }
    public void print(){

        System.out.println(NameNum + ":  " + FirstNum + "  " + SecondNum + "  " 
                           + ThirdNum);

    }

        //Closely related to Problem lines (method below, 3 lines in all) 
        int[] process(int FirstNum, int SecondNum, int ThirdNum) {

        int []  processed  = {FirstNum, SecondNum * 2, ThirdNum * 3};

        return processed;

    }
    public void printProcessed(){

         //Problem line 2 of 2 below
         System.out.println(NameNum + " Processed: " + processed[0] +                          
                            processed[1] + processed[2]);
    }


}
Dan Oswalt
  • 2,201
  • 2
  • 14
  • 24
jay
  • 15
  • 3
  • if this is a java issue, tag the question `java`, not the totally unrelated and superior `javascript` – Jaromanda X Mar 21 '18 at 02:08
  • 1
    it would be more helpful to see the error you actually get when compiling – avigil Mar 21 '18 at 02:10
  • Error:(35, 55) java: cannot find symbol symbol: variable processed location: class com.learn.java.sub – jay Mar 21 '18 at 02:19
  • Error:(35, 70) java: cannot find symbol symbol: variable processed location: class com.learn.java.sub – jay Mar 21 '18 at 02:19
  • Error:(35, 85) java: cannot find symbol symbol: variable processed location: class com.learn.java.sub – jay Mar 21 '18 at 02:20
  • Three line of error in all. – jay Mar 21 '18 at 02:21
  • You get 3 lines of errors because you reference a non existant processed variable within your public void printProcessed method. As mentioned in the other posts, declare the processed variable as a class member variable and it'll get rid of your errors. – CAMD_3441 Mar 21 '18 at 02:25

2 Answers2

1

When you declare a variable inside a method, it is called a local variable. Local variables can only be accessed from the method they were declared in.

To make your code compile, move the declaration of processed outside of your constructor (but keep it inside your class). We then refer to it as a field.

O.O.Balance
  • 2,930
  • 5
  • 23
  • 35
  • It is outside the method; it is initiated and assigned in the constructor, right below where I have commented "Problem line 1 of 2" – jay Mar 21 '18 at 02:17
  • @jay It is _declared_ in the constructor, meaning it is only visible in the constructor. You can keep the assignment in the constructor, just move the declaration. – O.O.Balance Mar 21 '18 at 02:19
  • Solved! By O.O. Balance. Thanks. – jay Mar 21 '18 at 02:30
1

First, for readability please keep the code on the same indentation. I'm referencing your method int[] process(int FirstNum, int SecondNum, int ThirdNum) which has an extra indentation which at first glance it makes it look like it belongs to the method above it.

And as O.O. Balance mentioned above your printProcessed() method is referencing a variable that doesn't exist as a member of the class.

Within your constructor you have a variable int []processed which I assume you want as a class member variable. So declare that variable outside the constructor and it should work.

CAMD_3441
  • 2,514
  • 2
  • 23
  • 38