Closed Thread
Results 1 to 8 of 8

Thread: Help with Array problem

  1. #1
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Help with Array problem

    Beginner programer, please bare with me. I have a program that I keep getting 1 error(so far, I've debugged several, it may lead to more once someone corrects my error?) that says:
    "Prices.java:8: illegal start of expression
    double aver = > aver;
    ^


    I thought that the = > were not to have a space between them, but I still get the same error? Thank you to whom ever can respond with some advise!!

    the program I am trying to write is:

    //Create an array that stores 20 prices, such as 2.34, 7.89, 1.34, and
    //so on. (1) Display the sum of all the prices. (2) Display all values less
    //than 5.00. (3) Calculate the average of the prices, and (4) display all
    //values higher than the calculated average value.


    My program so far is below:

    public class Prices
    {
    public static void main(String[] args)
    {
    int price[] = new int [20];
    int min = 0, max = 5.00;
    double aver = > aver;
    double sum = 0;
    int i;
    float pricesItems[] = {2.34f, 7.98f, 1.34f, 8.95f, 4.26f, 5.75f, 3.92f, 1.48f, 5.66f, 7.12f, 6.39f, 2.40f, 1.12f, 4.84f, 7.03f, 4.91f, 7.37f, 6.92f, 6.43f, 9.99f};
    for(int i; i > 0; i++)
    {
    System.out.println("The sum of all prices is $ " + pricesItems[i] + ":");
    price[i] = relInt();
    sum = sum + price[i];
    if(prices[i] < max)
    {
    System.out.print("The prices less than 5.00 " + pricesItems[i] + ":");
    }
    System.out.println("\n Average price is: $" + aver);
    System.out.println("Price higher than the average is: $" + min);
    }
    }
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Help with Array problem

    It's >= not =>. Please post your code in [code][/code] tags.
    Wow I changed my sig!

  4. #3
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Re: Help with Array problem

    Thank you for the advice. Pardon my asking, but what do you mean by: Please post your code in tags? Please define & I will try to adhere to the process.

    I changed it to >=, but now I get 2 errors for the same line again. Not sure why.

    Thank you.

    Prices.java:7: ';' expected
    double aver >= aver;
    ^
    Prices.java:7: not a statement
    double aver >= aver;

  5. #4
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Help with Array problem

    Like this:
    [code]public class Prices
    {
    public static void main(String[] args)
    {
    int price[] = new int [20];
    int min = 0, max = 5.00;
    double aver = > aver;
    double sum = 0;
    int i;
    float pricesItems[] = {2.34f, 7.98f, 1.34f, 8.95f, 4.26f, 5.75f, 3.92f, 1.48f, 5.66f, 7.12f, 6.39f, 2.40f, 1.12f, 4.84f, 7.03f, 4.91f, 7.37f, 6.92f, 6.43f, 9.99f};
    for(int i; i > 0; i++)
    {
    System.out.println("The sum of all prices is $ " + pricesItems[i] + ":");
    price[i] = relInt();
    sum = sum + price[i];
    if(prices[i] < max)
    {
    System.out.print("The prices less than 5.00 " + pricesItems[i] + ":");
    }
    System.out.println("\n Average price is: $" + aver);
    System.out.println("Price higher than the average is: $" + min);
    }
    }
    }[/code]

    That's how you do it.

    Anyway, the problem is is just that, it's not a statement. >= means "Less than or equal to", it's a comparison operator and aver is being declared there, not defined or assigned to. What exactly are you trying to do with that statement? It looks like it's supposed to hold the average price.

    Code:
    public class Prices
    {
        public static void main(String[] args)
        {
            int price[] = new int [20];
            int min = 0, max = 5.00;
            double aver = > aver;
            double sum = 0;
            int i;
            float pricesItems[] = {2.34f, 7.98f, 1.34f, 8.95f, 4.26f, 5.75f, 3.92f, 1.48f, 5.66f, 7.12f, 6.39f, 2.40f, 1.12f, 4.84f, 7.03f, 4.91f, 7.37f, 6.92f, 6.43f, 9.99f};
            for(int i; i > 0; i++)
            {
                System.out.println("The sum of all prices is $ " + pricesItems[i] + ":");
                price[i] = relInt();
                sum = sum + price[i];
                if(prices[i] < max)
                {
                    System.out.print("The prices less than 5.00 " + pricesItems[i] + ":");
                }
                System.out.println("\n Average price is: $" + aver);
                System.out.println("Price higher than the average is: $" + min);
            }
        }
    }
    Code:
            double aver = > aver;
    This code is meaningless and faulty. Just say "double aver = 0;".
    Code:
            float pricesItems[] = {2.34f, 7.98f, 1.34f, 8.95f, 4.26f, 5.75f, 3.92f, 1.48f, 5.66f, 7.12f, 6.39f, 2.40f, 1.12f, 4.84f, 7.03f, 4.91f, 7.37f, 6.92f, 6.43f, 9.99f};
    This will not work in Java, since all Arrays are objects, and all objects must be declared using the new operator. Say float pricesItems[] = new float[] {2.34f, 7.98f, 1.34f, 8.95f, 4.26f, 5.75f, 3.92f, 1.48f, 5.66f, 7.12f, 6.39f, 2.40f, 1.12f, 4.84f, 7.03f, 4.91f, 7.37f, 6.92f, 6.43f, 9.99f}; and it'll work.


    Finally, I wouldn't even use floats for this, since floating point numbers are not accurate enough to keep financial information. Instead, you'd want to use integers that represent individual pennies, so $100 would equal 10000 in your program, and simply format the printing in a manner that puts a "." before the second to last number.
    Wow I changed my sig!

  6. #5
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Re: Help with Array problem

    Thank you for your help, I do appreciate it! I made the changes you suggested, received several errors, corrected most of them, but now I am stuck on these last four. If you have time, please advise on these, I am stumped on how to correct them. Error msgs. below program with corrections in it.
    Thank you.

    Code:
    public class Prices
    {
      public static void main(String[] args)
      {
       int price[] = new int [20];
       int min = 0, int max = 5.00;
       double aver = 0;
       double sum = 0;
       int i;
       float pricesItems[] = new float[]{2.34f, 7.98f, 1.34f, 8.95f, 4.26f, 5.75f, 3.92f, 1.48f, 5.66f, 7.12f, 6.39f, 2.40f, 1.12f, 4.84f, 7.03f, 4.91f, 7.37f, 6.92f, 6.43f, 9.99f};
       for(int i; i > 0; i++)
        {
           System.out.println("The sum of all prices is $ " + pricesItems[i] + ":");
           price[i] = readInt();
           sum = sum + price[i];
           if(prices[i] < max)
           { 
             System.out.print("The prices less than 5.00 " + pricesItems[i] + ":");
           } 
              System.out.println("\n Average price is: $" + aver);
              System.out.println("Price higher than the average is: $" + min);  
        }  
      } 
    }
    Code:
    Prices.java:7: possible loss of precision
    found   : double
    required: int
    	int max = 5.00;
    	          ^
    Prices.java:12: i is already defined in main(java.lang.String[])
       for(int i; i > 0; i++)
               ^
    Prices.java:15: cannot find symbol
    symbol  : method readInt()
    location: class Prices
           price[i] = readInt();
                      ^
    Prices.java:17: cannot find symbol
    symbol  : variable prices
    location: class Prices
           if(prices[i] < max)
              ^
    4 errors

  7. #6
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Help with Array problem

    C'mon man, this is basic stuff.

    Problem 1:
    The first is an error because you cannot (and should not) assign a constant float value to an int, it only serves to lose precision. The Java compiler forces you to behave safely, and if you want that to convert, you need it to convert explicitly using Math.ceil() or Math.floor(). If you're taking my suggestion to just use ints, is this a school assignment and did they TELL you to use floats? If so, just use floats and get over it, but if this is a practical application that is only supposed to output certain values according to the assignment or job then use ints since they're better for financial calculation.

    First, I'd have a function that derives a String with the dollar value of whatever integer was placed into it, like so:
    Code:
      public static String toDollarValue(int intValue)
      {
        return "$" + (intValue / 100) + "." + (intValue &#37; 100);
      }
    As you can see, it is pretty simple, but very convenient. Now just convert EVERYTHING to ints, and eliminate the '.' in each number, thus making it hundreds:
    Code:
       final int min = 0, max = 500;
       int aver = 0, sum = 0;
       int pricesItems[] = new int[] { 234, 798, 134, 895, 426, 575, 392, 148, 566, 712, 639, 240, 112, 484, 703, 491, 737, 692, 643, 999 };
    This will maintain all the information you need, and it will simply display to the user properly when you use the above method. Easy, huh?

    Problem 2:
    This is because you have already defined i in main, just like it tells you!

    All you've got to do is eliminate the "int i;" declaration above the for loop, and you'll be fine.

    Problem 3:
    What is readInt() supposed to be? It's not in the class you have, so I can't really say. Is this supposed to get user input?

    Problem 4:
    This is because 'prices' doesn't exist, you only have an array called 'price', so it cannot find 'prices'.

    Finally...
    I don't think your for loop does what I think you think it's supposed to do. What is your assignment?
    Wow I changed my sig!

  8. #7
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Re: Help with Array problem

    I do appreciate all your help! this is my first programing course and I admit I am lost. Learning online, with a book that is terrible (may as well have been written in Latin) and an instructor that takes 2-3 days to respond to a single question and then responds in riddles isn't helpful, the suggestions you provide are helpful. Yes it is "basic stuff" and I appreciate your bearing with me. You are, to say, helping an old dog learn new tricks. I have posted an updated program below, made the changes you suggested but still get 2 errors. I also have one other question (more of an opinion) if you don't mind? I had to make a choice of taking either Java & C++... or... Visual Basic & Adv. Visual Basic. Are both sets of classes pretty much the same as in ease/difficulty of learning or would I have been better off starting with the two Visual classes instead & then learning Java & C++? Thank you again for all your help and advice in helping this Green Foot, get started in programming.

    The errors say '.class expected' which I take to mean that I have an...int i where I should just have an 'i' somewhere? but I'm not sure where. I tried several combonations but just ended up with more errors.

    The assgnment was:
    //Create an array that stores 20 prices, such as 2.34, 7.89, 1.34, and
    //so on. (1) Display the sum of all the prices. (2) Display all values less
    //than 5.00. (3) Calculate the average of the prices, and (4) display all
    //values higher than the calculated average value.


    I need to have all four elements in the program.

    Code:
    import java.util.*;
    public class Prices2
    {
      public static String toDollarValue(int intValue)
      {
        return "$" + (intValue / 100) + "." + (intValue &#37; 100);
      }
       final int min = 0, max = 500;
       int aver = 0, sum = 0;
       int pricesItems[] = new int[] { 234, 798, 134, 895, 426, 575, 392, 148, 566, 712, 639, 240, 112, 484, 703, 491, 737, 692, 643, 999 };
         {
           {
           System.out.println("The sum of all prices is $ " + pricesItems[] + ":");
          if(prices[i] < max)
           { 
             System.out.print("The prices less than 5.00 " + pricesItems[] + ":");
         } 
              System.out.println("\n Average price is: $" + aver);
              System.out.println("Price higher than the average is: $" + min);  
    	} 
      } 
    }
    Prices2.java:13: '.class' expected
    System.out.println("The sum of all prices is $ " + pricesItems[] + ":");
    ^
    Prices2.java:16: '.class' expected
    System.out.print("The prices less than 5.00 " + pricesItems[] + ":");
    ^
    2 errors

  9. #8
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Help with Array problem

    Alright, no problem, I'm going to write a very detailed post here, so come back in maybe 30 minutes.
    Wow I changed my sig!

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. c# array problem
    By baboulas in forum C# Programming
    Replies: 16
    Last Post: 05-17-2011, 08:41 PM
  2. in array problem?
    By Hamed in forum PHP Development
    Replies: 4
    Last Post: 12-04-2010, 02:55 AM
  3. Array problem
    By wiwbiz in forum C and C++
    Replies: 5
    Last Post: 01-29-2010, 03:05 PM
  4. Problem With Array
    By Divya in forum PHP Development
    Replies: 5
    Last Post: 03-30-2009, 09:30 PM
  5. C++ Array Problem
    By dksmarte in forum C and C++
    Replies: 4
    Last Post: 04-12-2007, 09:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

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