Results 1 to 12 of 12
  1. #1
    Radman23 is offline Member
    Join Date
    Jan 2009
    Location
    WI
    Posts
    10
    Rep Power
    0

    Default Class Expected error

    Looking for direction on how I can find a resolution to this error. :confused:

    Line 39 returns the following error.
    '.class' expected underTen = gallons[];


    import java.io.*;
    import java.util.Formatter;

    public class Gallons
    {
    public static void main(String[] args)
    {

    double[] gallons = new double[10];
    double sum = 0;
    double underTen;

    gallons[0] = 8.25;
    gallons[1] = 6.85;
    gallons[2] = 14.21;
    gallons[3] = 13.51;
    gallons[4] = 8.52;
    gallons[5] = 9.37;
    gallons[6] = 14.23;
    gallons[7] = 7.36;
    gallons[8] = 9.69;
    gallons[9] = 2.98;

    System.out.println("Average gallons:");

    for(int i=0;i < gallons;i++)
    {
    sum = sum + gallons[i];
    }

    System.out.print("Sum is:" + sum);

    for(int i=10;i < gallons;i++)

    if(gallons[i] < 10.00);

    {
    underTen = gallons[];
    }

    System.out.println("Gallons under ten" + underTen);


    }//close main()

    }//close class

    Working on basic programming to help understand arrays.
    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Here:
    Java Code:
    if(gallons[i] < 10.00);
    {
      underTen = gallons[];
    }
    are you missing an "i" array index? Should it be instead:

    Java Code:
    if(gallons[i] < 10.00);
    {
      underTen = gallons[i]; // "i" goes here!
    }
    Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:

    Java Code:
    &#91;code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    &#91;/code]
    Best of luck

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    in for loop , gallons.length should be used

    and
    Java Code:
    if(gallons[i] < 10.00);  // why add ';' here?
    {
      underTen = gallons[i];
    }
    and
    Java Code:
    for(int i=10;i < gallons.length; i++)  // i =10, then .... will it loop?
    and

    underTen is not initialized


    and

    Java Code:
    System.out.println("Gallons under ten" + underTen);
    you want to show only the last gallon under ten?

    and

    Java Code:
    System.out.print("Sum is:" + sum);
    should use println or print method?
    Last edited by mtyoung; 01-10-2009 at 05:36 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    what are your instructions here? what are you trying to calculate anyway?

  5. #5
    Radman23 is offline Member
    Join Date
    Jan 2009
    Location
    WI
    Posts
    10
    Rep Power
    0

    Default

    Thanks for the comment so far everyone.

    The code was pasted from TextPad, missed the format code part before sending.

    Learning to write code and was trying a "simple" task of taking set amount of fill ups at the gas pump (10 in this case) then returning the average gallons of the fill ups. Then show how many of those are over 10 gallons.

    As my knowledge of Java increases would hope to allow user to input the values and return those results based on the input - for another day.

    This may lead to creating a program to estimate the cost of fuel over a period of time.

    Will take what has been given and will keep working on this. New to Java, and so far this is really cool. Wish I would have picked up on this years ago.

  6. #6
    Radman23 is offline Member
    Join Date
    Jan 2009
    Location
    WI
    Posts
    10
    Rep Power
    0

    Default

    Well this code runs and is close, knowing that the outcome is basic at best. Now to keep reading and coding to get this close to a usable program.

    The gallons over 10 is just coded with [2] to pick the value, need to work on how to select the variables from the array that met the criteria requested.

    Java Code:
    import java.io.*;
    import java.util.Formatter;
    
    public class Gallons
    {
    	public static void main(String[] args)
    	{
    
    		double []gallons = {8.25, 6.85, 14.21, 8.51, 8.52, 9.37, 4.23, 7.36, 9.69, 2.98};
    		double sum = 0;
    		double overTen;
    
    			for(int i=0; i<10; i++)
    
    			{
    				sum += gallons[i];
    				}
    
    				System.out.println("Sum is:" + sum); //Total gallons used
    
    				for(int i=0;i < gallons.length;i++)
    
    				if(gallons[i] > 10.00);
    
    				{
    					 overTen = gallons[2];
    				 }
    
    					System.out.println("Gallons over ten" + overTen);
    
    
        }//close main()
    
    }//close class

  7. #7
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  8. #8
    Radman23 is offline Member
    Join Date
    Jan 2009
    Location
    WI
    Posts
    10
    Rep Power
    0

    Default

    Was attempting to gather any value that was over ten gallons.
    The example shows that I selected the second element, just a way to return at least one value until I can figure this out.

    Any direction or suggestions for a source to investigate?
    Reading some comments online and eagerly awaiting the arrival of my Java for dummies.

    Know there is a way to do this just not knowledgeable at this point.

    Another question, can a user be allowed to enter a choice for selecting an element in the array, say for example the second row?

    Thanks

  9. #9
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    as we do not know the number of gallons > 10, i suggest to use classes like Vector, Stock, LinkedHashSet etc to store the oversize gallons

    row?

  10. #10
    Radman23 is offline Member
    Join Date
    Jan 2009
    Location
    WI
    Posts
    10
    Rep Power
    0

    Default

    Got me on that one. Bit over my head with the Vector and the others. Sorry you lost me at this point.

    The elements in the array should be checked for a value greater than 10. Still trying to work on this part. Was thinking that a nested loop for statement might do the trick.

    No luck with this yet still working on the problem.
    Thanks for the advice, will Google these and see where this leads me.

  11. #11
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    you can initial another array(boolean or int as you like) with same length,
    initial them with some value, if the gallons[i] > 10, anotherArray[i] update to some other value, finally, you loop the anotherArray, to get which gallons is > 10?

    or you can use a int
    if both gallons[1] and gallons[2] > 10, int update to 110
    if both gallons[5] and gallons[2] > 10, 100100
    by using % 10 to you check which gallon is > 10

    above method can use on String too "110", "100100", using charAt to check position is 1 or 0 ...etc


    allow you to select ...
    you need to use InputSteamReader(example find in BufferReader class) or Scanner like class to get the input from users
    Last edited by mtyoung; 01-13-2009 at 08:47 AM.

  12. #12
    Radman23 is offline Member
    Join Date
    Jan 2009
    Location
    WI
    Posts
    10
    Rep Power
    0

    Default

    mtyoung
    Will give this a try. Thanks for the suggestion.
    Should have thought to use the array to find the element.

    Try to over engineer this at times and try to do more than required.

    Like the streamer idea, will note this for version 2 of the program.

Similar Threads

  1. Identifier expected error
    By vasu18 in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 05:49 PM
  2. Error: ')' expected
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:32 AM
  3. Error: '{' expected
    By romina in forum New To Java
    Replies: 1
    Last Post: 07-26-2007, 09:34 AM
  4. My error is: ')' expected
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 04:49 PM
  5. MSG ERROR: : expected
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-31-2007, 02:21 AM

Posting Permissions

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