Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help

 
luk Hann
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Art Metzer
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic