Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-10-2009, 06:20 AM
Member
 
Join Date: Jan 2009
Location: WI
Posts: 10
Rep Power: 0
Radman23 is on a distinguished road
Default Class Expected error
Looking for direction on how I can find a resolution to this error.

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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-10-2009, 06:25 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,488
Rep Power: 8
Fubarable is on a distinguished road
Default
Here:
Code:
if(gallons[i] < 10.00);
{
  underTen = gallons[];
}
are you missing an "i" array index? Should it be instead:

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 [code] at the top of your block of code and the tag [/code] at the bottom, like so:

Code:
[code]
  // your code block goes here.
  // note the differences between the tag at the top vs the bottom.
[/code]
Best of luck
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-10-2009, 06:27 AM
Senior Member
 
Join Date: Dec 2008
Location: Hong Kong
Posts: 473
Rep Power: 2
mtyoung is on a distinguished road
Default
in for loop , gallons.length should be used

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

underTen is not initialized


and

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

and

Code:
System.out.print("Sum is:" + sum);
should use println or print method?

Last edited by mtyoung; 01-10-2009 at 06:36 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-10-2009, 06:29 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,488
Rep Power: 8
Fubarable is on a distinguished road
Default
what are your instructions here? what are you trying to calculate anyway?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-11-2009, 01:05 AM
Member
 
Join Date: Jan 2009
Location: WI
Posts: 10
Rep Power: 0
Radman23 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-11-2009, 04:32 AM
Member
 
Join Date: Jan 2009
Location: WI
Posts: 10
Rep Power: 0
Radman23 is on a distinguished road
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.

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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-12-2009, 10:36 AM
Senior Member
 
Join Date: Dec 2008
Location: Hong Kong
Posts: 473
Rep Power: 2
mtyoung is on a distinguished road
Default
is that means gallons only find 1 element > 10?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-13-2009, 01:34 AM
Member
 
Join Date: Jan 2009
Location: WI
Posts: 10
Rep Power: 0
Radman23 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-13-2009, 05:01 AM
Senior Member
 
Join Date: Dec 2008
Location: Hong Kong
Posts: 473
Rep Power: 2
mtyoung is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-13-2009, 05:36 AM
Member
 
Join Date: Jan 2009
Location: WI
Posts: 10
Rep Power: 0
Radman23 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 01-13-2009, 09:40 AM
Senior Member
 
Join Date: Dec 2008
Location: Hong Kong
Posts: 473
Rep Power: 2
mtyoung is on a distinguished road
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 09:47 AM.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 01-14-2009, 05:12 AM
Member
 
Join Date: Jan 2009
Location: WI
Posts: 10
Rep Power: 0
Radman23 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Identifier expected error vasu18 New To Java 1 01-01-2008 06:49 PM
Error: ')' expected baltimore New To Java 1 08-07-2007 07:32 AM
Error: '{' expected romina New To Java 1 07-26-2007 10:34 AM
My error is: ')' expected silvia New To Java 1 07-18-2007 05:49 PM
MSG ERROR: : expected Marty New To Java 1 05-31-2007 03:21 AM


All times are GMT +2. The time now is 05:52 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org