aspose file tools*
The moose likes Beginning Java and the fly likes Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help" Watch "Help" New topic
Author

Help

luk Hann
Ranch Hand

Joined: Jan 29, 2001
Posts: 48
Hi, everyboday,
For following question:
Write a Java application to print a list of numbers from 1 through 30 and the sum of the numbers as they are printed. Thus,
1
2 3
3 6
4 10
etc.

I wrote the following code:
public class Count {
public static void main (String args[]) {
int i = 1;
int sum = 0;
}
for (int i; i<100; i++){
sum = sum + i;
}
System.out.println(i +" "+ sum"\n");
}
When I tried to compile it, the following error info. came up:
1. Count.java:6: illegal start of type
for (int i; i<100; i++){
^
2. Count.java:9: <identifier> expected
System.out.println(i +" "+ sum"\n");
^
I amself can't figure out the problem and would highly appreciate if someone in this forum give me a lead! Thanks so much!
luk
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Your for statement is outside your main statement. Also you declared i twice, the compiler will not like that (as soon as you move the for into the main).


"JavaRanch, where the deer and the Certified play" - David O'Meara
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Luk.
A couple of other points--
1. The newline character in your println statement needs to be concatenated to the rest of what you are printing out:

instead of

2. If you want to print out every i-sum pair, you will need to place your println statement within your "for" loop block.
3. If you only want to print out the first thirty iterations, your for loop should read

Good luck!
Art
luk Hann
Ranch Hand

Joined: Jan 29, 2001
Posts: 48
Thanks so much for your inputs, Cindy & Art.
I tried Cindy's code, but there is still an error:
Count.java:7: cannot resolve symbol
symbol : variable i
location: class Count
System.out.println( i + " " + sum + "\n" );
What does that mean?
luk
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
When you put the print in the loop it will resolve the symbol.
The other choice would have been to declare the i outside the loop (as you did before) and DO NOT re-declare it in the loop - just initialize it in there.
int i
for(i=1 , i<100 , i++){}
luk Hann
Ranch Hand

Joined: Jan 29, 2001
Posts: 48
Cindy,
Thanks so much. I figured it out. The only thing is that the redundent declarations of "int i" outside and inside the main method didn't look good. Is that the way it should be for this case? Thanks again.
luk
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
No - you CAN'T do it. The compiler will give you an error saying that "variable i is already defined in this method".
Every time you declare "int i" you are asking the compiler to create a new variable for you. It can't have two variables with the same name.
However if you declare the variable outside the loop, you can still initialize it to a value in the loop.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help