cant find symbol error

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 929 Views - Last Post: 02 June 2011 - 01:49 PM Rate Topic: -----

#1 flap  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-May 11

cant find symbol error

Posted 01 June 2011 - 12:16 PM

I hate to keep bother you guys, but I just cant see what im doing wrong, ive researched what this error is, but since my noob programming eyes I just cant tell what im doing wrong(it all looks correct to me lol). Can anyone help me go in the right direction, Thanks much appreciated. BTW the Cant find symbol error is in lines 9-15.

import java.io.*;
import java.util.Scanner;

public class ArrayProcessing
{
   public static void main(String[] args) throws IOException
   {
       inputData();
       reverseArray(array);
       printArray(array);
       sum(array);
       average(array);
       max(array);
       min(array);
       outputData(array);
   }
   public static int[] inputData() throws IOException
   {

       File data = new File("data.in");
       Scanner inputData = new Scanner(data);

       if (!inputData.data())
       {
           System.out.println("ERROR: file data.in does not exist.");
           System.exit(0);
       }
       inputData.close();
   }
   public static void reverseArray(int[] array)
   {
   int left  = 0;
   int right = array.length-1;

   while (left < right)
   {
      int temp = array[left];
      array[left]  = array[right];
      array[right] = temp;

      left++;
      right--;
   }
   }
   public static void printArray(int[] array)
   {
        for(int i=0; i<=array.length; i++)
          {
            System.out.printf("%2d", array[i]);
            if(i % 10 == 0)
            {
                System.out.println();
            }
            else
            {
                System.out.printf("average = %.2f\n", average(array));
                System.out.printf("max = %d\n", max(array));
                System.out.printf("min = %d\n", min(array));
            }
          }
   }
   public static int sum(int[] array)
   {
       int total = 0;
       for(int i=0; i<array.length; i++)
          total = total + array[i];

       return total;
   }
   public static double average(int[] array)
   {
       return sum(array)/array.length;
   }
   public static int max(int[] array)
   {
      int max = array[0];
      for (int i=1; i<array.length; i++)
      {
          if (array[i]>max)
          {
              max = array[i];
           }
       }
      return max;
   }
   public static int min(int[] array)
   {

      int min = array[0];
      for (int i=1; i<array.length; i++)
      {
         if (array[i]<min)
         {
            min = array[i];
         }
       }
      return min;
   }
   public static void outputData(int[] array)
   {
       for(int i=0; i<array.length; i++)
          if(array[i]%2 == 0)
          {
              PrintWriter array = new PrintWriter("even.out");
          }
          else
          {
              PrintWriter array = new PrintWriter("odd.out");
          }
   }
}



Cheers.

Is This A Good Question/Topic? 0
  • +

Replies To: cant find symbol error

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9143
  • View blog
  • Posts: 33,935
  • Joined: 27-December 08

Re: cant find symbol error

Posted 01 June 2011 - 12:21 PM

You don't define a variable "array" in your main() method. The variable array is local to each of your methods, as it is the parameter. You have to define an int[] to pass to them, though. Also, your input() method doesn't use or return an array at all. So at this point in your program, no array is in existence.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5578
  • View blog
  • Posts: 9,010
  • Joined: 19-March 11

Re: cant find symbol error

Posted 01 June 2011 - 12:22 PM

"array" doesn't exist. It's not declared in main, or anywhere that I can see.

Furthermore, you aren't going to get anything from those methods unless you assign the return values into some locally accessible variable.
a = addOneToA(a);  // a now equals a+1
addOneToA(a);      // a == a;

private int addOneToA(int a)
{
  return a++;
}



You also need to return arrays from those methods, or the compiler will complain.

This post has been edited by jon.kiparsky: 01 June 2011 - 12:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 flap  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-May 11

Re: cant find symbol error

Posted 01 June 2011 - 12:41 PM

Im completely confused, I tried to do what you guys said, but i just get more errors. Arrays dont like me. And I have to use the methods like that, thats what the teacher told us.
//so do this is what im doing
int [] array = sum(array);
sum (array);
// but Im getting more errors b/c of this


Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5578
  • View blog
  • Posts: 9,010
  • Joined: 19-March 11

Re: cant find symbol error

Posted 01 June 2011 - 12:46 PM

int [] array = sum(array);


This is a declaration. If you have another line after it like
int [] array = reverse(array);


you'll get an error because you're declaring two arrays with the same name. However, this will work:
int [] array = sum(array);
array = reverse(array);


It won't do anything visible, because you're assigning a value and then assigning over it without displaying it. To display it, you need to use your outputData method.

sum (array);


This still does nothing, unless you rewrite the method to actually print out the sum of the array. That's bad practice: methods should return a value and let you do what you want to with them, but if that's what your teacher wants, you should give them that.
Was This Post Helpful? 0
  • +
  • -

#6 flap  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-May 11

Re: cant find symbol error

Posted 01 June 2011 - 12:52 PM

Well this is what she wants us to do, I guess I wrote the whole program wrong, hope not.

Implement the following methods in the program:
• public static int[] inputData() – This method will open a file called data.in, create an array, and store the integers read from the file into the array. If data.in does not exist, give an appropriate error message and terminate the program.
• public static void reverseArray(int[] array) – This method will reverse the elements of the array so that the 1st element becomes the last, the 2nd element becomes the 2nd to the last, and so on.
• public static void printArray(int[] array) – This method will display the content of the array on screen. Print 10 integers per line and use printf method to align columns of numbers.
• public static int sum(int[] array) – This method should compute and return the sum of all elements in the array.
• public static double average(int[] array) – This method should compute and return the average of all elements in the array.
• public static int max(int[] array) – This method should find and return the largest value in the array.
• public static int min(int[] array) – This method should find and return the smallest value in the array.
• public static void outputData(int[] array) – This method will create two output files called even.out and odd.out. Scan through the entire array, if an element is even, print it to even.out. If it is odd, print the element to odd.out.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5578
  • View blog
  • Posts: 9,010
  • Joined: 19-March 11

Re: cant find symbol error

Posted 01 June 2011 - 01:02 PM

Okay, yeah. You've followed her instructions. Sorry, I misread your code the first time.

Do you see why you'd want to return a new array, rather than changing the old one?
What happens, for example, if you want to use your reverseArray method to check whether an array is palindromic? It's a lot easier if you just return a new reversed array, and do whatever you want with it when you get it back (including assigning it over the original, if that's what you want to do).

So for your original question, you want to declare an int array called array first thing, and then use that to catch the return value from inputData().
Was This Post Helpful? 1
  • +
  • -

#8 flap  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-May 11

Re: cant find symbol error

Posted 01 June 2011 - 01:28 PM

O ok i got you, thank you very much I have fixed all those small annoying java errors.

Now i guess my file reader code is not correct
Was This Post Helpful? 0
  • +
  • -

#9 flap  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-May 11

Re: cant find symbol error

Posted 01 June 2011 - 01:41 PM

ok so ive got this

import java.io.*;
import java.util.Scanner;

public class ArrayProcessing
{
   public static void main(String[] args) throws IOException
   {
       int[] array = array;
       inputData();
   }
   public static int[] inputData() throws IOException
   {

       File data = new File("data.in");
       if (!inputData.exist())
       {
           System.out.println("ERROR: file data.in does not exist.");
           System.exit(0);
       }

       Scanner inputData = new Scanner(data);
       while(inputData.hasNext())
       {
        double array = inputData.nextLine();
       }
       inputData.close();
   }


This post has been edited by flap: 01 June 2011 - 01:58 PM

Was This Post Helpful? 0
  • +
  • -

#10 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1005
  • View blog
  • Posts: 2,242
  • Joined: 05-April 11

Re: cant find symbol error

Posted 01 June 2011 - 02:03 PM

15 if (!inputData.exist())
21 Scanner inputData = new Scanner(data);

You cant use inputData before you declare it. Instead of inputData I think you mean if ( data.exists() )
Was This Post Helpful? 0
  • +
  • -

#11 flap  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-May 11

Re: cant find symbol error

Posted 01 June 2011 - 02:33 PM

ok i fixed that, lmao I really hate arrays now!
Was This Post Helpful? 0
  • +
  • -

#12 flap  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-May 11

Re: cant find symbol error

Posted 01 June 2011 - 03:09 PM

ok i dont get why im getting a ArrayProcessing.java:8: variable array might not have been initialized. i dont see why its giving me this

line 8 is
int[] array = array;

This post has been edited by flap: 01 June 2011 - 03:10 PM

Was This Post Helpful? 0
  • +
  • -

#13 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1005
  • View blog
  • Posts: 2,242
  • Joined: 05-April 11

Re: cant find symbol error

Posted 01 June 2011 - 04:24 PM

View Postflap, on 01 June 2011 - 03:09 PM, said:

ok i dont get why im getting a ArrayProcessing.java:8: variable array might not have been initialized. i dont see why its giving me this

line 8 is
int[] array = array;


Why would you write array = array ? You gain nothing from it
Was This Post Helpful? 0
  • +
  • -

#14 flap  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-May 11

Re: cant find symbol error

Posted 01 June 2011 - 04:55 PM

View Postjon.kiparsky, on 01 June 2011 - 01:02 PM, said:

So for your original question, you want to declare an int array called array first thing, and then use that to catch the return value from inputData().

isnt that what he told me to do?
Was This Post Helpful? 0
  • +
  • -

#15 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1005
  • View blog
  • Posts: 2,242
  • Joined: 05-April 11

Re: cant find symbol error

Posted 01 June 2011 - 05:09 PM

View Postflap, on 01 June 2011 - 04:55 PM, said:

View Postjon.kiparsky, on 01 June 2011 - 01:02 PM, said:

So for your original question, you want to declare an int array called array first thing, and then use that to catch the return value from inputData().

isnt that what he told me to do?


I just read his post and some more of yours for at better understanding :P

Your problem is that you name your int[] 'array' as your method parameter already is named array. Don't let them have the same name :)

Also some better names could be oldArray and reversedArray, so you don't mess it up later :P
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2