Results 1 to 10 of 10
  1. #1
    twcast is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default Passing an array to a method.

    Once again, I find myself needing assistance. Please keep in mind that I am also an Over the Road Trucker, who is learning basic Java. :)

    Basic VB was pretty easy, but this is a little tougher challenge.
    I am getting an error on line 17.

    Arrays.java:17: cannot find symbol
    symbol : class Prices
    location: class Arrays
    Prices price = new Prices();
    ^
    Arrays.java:17: cannot find symbol
    symbol : class Prices
    location: class Arrays
    Prices price = new Prices();
    ^



    Here is my code:

    import javax.swing.JOptionPane;

    public class Arrays
    {
    public static void main(String[] args)
    {
    final double NUMBER_OF_PRICES = 5;
    double[] pricesArray;
    double x;
    double y;
    double average = 0;

    for (int i = 0; i < pricesArray.length; i++)
    {
    pricesArray[i] = Double.parseDouble(JOptionPane.showInputDialog(("E nter Price Number " + (i+1))));
    }
    Prices price = new Prices();
    x = price.sumArray(pricesArray);
    y = price.aveArray(pricesArray);

    JOptionPane.showMessageDialog(null, "The total of your prices is: $" + x);
    JOptionPane.showMessageDialog(null, "The average of your prices is: $" + y);
    JOptionPane.showMessageDialog(null, "Prices above the average consist of the following:");
    highPrices(pricesArray, y);
    }

    public double sumArray(double[] array)
    {
    double sum = 0.0;

    for (int x = 0; x < array.length; x++)
    sum += array[x];
    return sum;
    }

    public double aveArray(double[] array)
    {
    double average = 0.0;
    double sum = sumArray(array);
    return average = (sum / array.length);
    }

    public static void highPrices(double[] prices, double average)
    {
    for (int i = 0; i < prices.length; i++) {
    if (prices[i] > average)
    {
    JOptionPane.showMessageDialog(null, " " + prices[i]);
    }
    }
    }
    }

  2. #2
    steelshark is offline Member
    Join Date
    Aug 2009
    Posts
    25
    Rep Power
    0

    Default

    the problem is, you declare your array, but you do not initialize it.
    You have to initialize your array with a predefined length.
    Like this: priceArray= new double[100];
    hope this helps ;-)

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by steelshark View Post
    the problem is, you declare your array, but you do not initialize it.
    You have to initialize your array with a predefined length.
    Like this: priceArray= new double[100];
    hope this helps ;-)
    That's not his problem (yet); he has a compilation problem: the compiler can't find the .class (nor the source file) for the Prices class. Add a "-cp ." (minus cp space dot) flag to the compiler's arguments list so the compiler looks in the current directory for .class files. First compile the Prices.java file so a corresponding .class file exists in the current directory. Alternatively compile all the .java source files at once: "javac -cp . *.java"

    kind regards,

    Jos

  4. #4
    twcast is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    Thank you, but where would I place the priceArray= new double[100];?
    When I add it in, I get another error.

  5. #5
    steelshark is offline Member
    Join Date
    Aug 2009
    Posts
    25
    Rep Power
    0

    Default

    usualy you start initializing your variabeles after you declared all of them.So that would be behind the line
    double average = 0;
    And as JosAH pointed out you should first do what he says.

  6. #6
    twcast is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    Doggone it am still getting the same two errors.
    I did as both of you suggested.
    I found the problem with the compiliation. I had it saved as a different file, and it could not find the class. Now, it is finind the class. I think...

    PricesAndArrays.java:21: cannot find symbol
    symbol : class Prices
    location: class PricesAndArrays
    Prices price = new Prices();
    ^
    PricesAndArrays.java:21: cannot find symbol
    symbol : class Prices
    location: class PricesAndArrays
    Prices price = new Prices();
    ^
    2 errors

    import javax.swing.JOptionPane;

    public class PricesAndArrays

    {

    public static void main(String[] args)
    {
    final double NUMBER_OF_PRICES = 5;
    double[] pricesArray;
    double x;
    double y;
    double average = 0;
    pricesArray= new double[100];

    for (int i = 0; i < pricesArray.length; i++) {
    pricesArray[i] = Double.parseDouble(JOptionPane.showInputDialog(("E nter Price Number " + (i+1))));
    }

    // Make a new object and call its methods.
    Prices price = new Prices();
    x = price.sumArray(pricesArray);
    y = price.aveArray(pricesArray);

    JOptionPane.showMessageDialog(null, "The total is: $" + x);
    JOptionPane.showMessageDialog(null, "The average is: $" + y);
    JOptionPane.showMessageDialog(null, "Prices above the average are:");
    highPrices(pricesArray, average);

    }

    public double sumArray(double[] array) {
    double sum = 0.0;

    for (int x = 0; x < array.length; x++)
    sum += array[x];

    return sum;
    }

    public double aveArray(double[] array) {
    double average = 0.0;
    double sum = sumArray(array);

    return average = (sum / array.length);
    }

    public static void highPrices(double[] prices, double average) {
    for (int i = 0; i < prices.length; i++) {
    if (prices[i] > average) {
    // This code above seems to be the problem?

    JOptionPane.showMessageDialog(null, " " + prices[i]);
    }
    }
    }
    }

    I am missing something here, just cant find it. Sorry for the hassle.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by twcast View Post
    ..
    I did as both of you suggested...
    Did you use the -cp flag to tell the compiler where to find the Prices class?

  8. #8
    steelshark is offline Member
    Join Date
    Aug 2009
    Posts
    25
    Rep Power
    0

    Default

    My guess when i look at the error is, that you still don't compile the class Prices right.
    it gives an error when you try to create an object from that class so i figure the error must be somthing to do with that class...Have you tried creating a new class in the same dir with the name Prices ?
    see how that goes.
    Also, this is not really somthing bad but normally you would give your class a singular name:Price instead of prices...
    But thats just a convention, not really a mistake.
    Greetz, steelshark

  9. #9
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    Solution:

    import javax.swing.JOptionPane;

    public class PricesandArrays{

    public static void main(String[] args)
    {
    final double NUMBER_OF_PRICES = 5;
    double[] pricesArray;
    double x;
    double y;
    double average = 0;
    pricesArray= new double[5]; // i used only 5 items instead of 100

    for (int i = 0; i < pricesArray.length; i++) {
    pricesArray[i] = Double.parseDouble(JOptionPane.showInputDialog(("E nter Price Number " + (i+1))));
    }

    // Make a new object and call its methods.
    //PricesAndArrays price = new PricesAndArrays();
    x = sumArray(pricesArray);
    y = aveArray(pricesArray);

    JOptionPane.showMessageDialog(null, "The total is: $" + x);
    JOptionPane.showMessageDialog(null, "The average is: $" + y);
    JOptionPane.showMessageDialog(null, "Prices above the average are:");
    highPrices(pricesArray, average);

    }

    public static double sumArray(double[] array) {
    double sum = 0.0;

    for (int x = 0; x < array.length; x++)
    sum += array[x];

    return sum;
    }

    public static double aveArray(double[] array) {
    double average = 0.0;
    double sum = sumArray(array);

    return average = (sum / array.length);
    }

    public static void highPrices(double[] prices, double average) {
    for (int i = 0; i < prices.length; i++) {
    if (prices[i] > average) {
    // This code above seems to be the problem?

    JOptionPane.showMessageDialog(null, " " + prices[i]);
    }
    }
    }
    }

  10. #10
    thomasdevos's Avatar
    thomasdevos is offline Member
    Join Date
    Feb 2010
    Posts
    19
    Rep Power
    0

    Default

    hi twcast, i guess you want some program to accpet 5 number, then caculatee the sum and average, then to find out those number which is bigger than the average. is that right?
    below is the code can run modified from your code. you can make a comapration and learn something from. good luck.

    Java Code:
    import javax.swing.JOptionPane;
    
    public class Arrays {
    	public static void main(String[] args) {
    		final int NUMBER_OF_PRICES = 5;
    		double[] pricesArray = new double[NUMBER_OF_PRICES];
    		double x;
    		double y;
    		double average = 0;
    		
    		for (int i = 0; i < pricesArray.length; i++) {
    			pricesArray[i] = Double.parseDouble(JOptionPane
    					.showInputDialog(("E nter Price Number " + (i + 1))));
    		}
    		Arrays price = new Arrays();
    		x = price.sumArray(pricesArray);
    		y = price.aveArray(pricesArray);
    
    		JOptionPane.showMessageDialog(null, "The total of your prices is: $"
    				+ x);
    		JOptionPane.showMessageDialog(null, "The average of your prices is: $"
    				+ y);
    		JOptionPane.showMessageDialog(null,
    				"Prices above the average consist of the following:");
    		highPrices(pricesArray, y);
    	}
    
    	public double sumArray(double[] array) {
    		double sum = 0.0;
    
    		for (int x = 0; x < array.length; x++)
    			sum += array[x];
    		return sum;
    	}
    
    	public double aveArray(double[] array) {
    		double average = 0.0;
    		double sum = sumArray(array);
    		return average = (sum / array.length);
    	}
    
    	public static void highPrices(double[] prices, double average) {
    		for (int i = 0; i < prices.length; i++) {
    			if (prices[i] > average) {
    				JOptionPane.showMessageDialog(null, " " + prices[i]);
    			}
    		}
    	}
    }

Similar Threads

  1. Passing array objects
    By drymsza1234 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 03:40 PM
  2. Passing a 2D array
    By toymachiner62 in forum New To Java
    Replies: 6
    Last Post: 10-23-2009, 05:05 PM
  3. Passing Class Reference to method
    By nekt in forum Advanced Java
    Replies: 5
    Last Post: 03-26-2009, 06:08 AM
  4. array passing dynamically
    By jazz2k8 in forum Advanced Java
    Replies: 2
    Last Post: 10-16-2008, 11:29 PM
  5. Replies: 5
    Last Post: 10-04-2008, 12:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •