• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help this newbie...what's wrong with my code?  RSS feed

 
N. Gonzalez
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I struggling through a Java course and could use all the help I can get.
Can't get past a certain point in my Strings homework...don't understand why I'm getting this error message when I added the code to make all my entered words each print on a separate line (everything before it worked:

stringAssignment.java:31: cannot resolve symbol
symbol : variable i
location: class stringAssignment
System.out.println (entry[i]);


Here's my program so far:

/**
* Homework: Arrays and Strings-Chapter 4
* Enter numerous words as command line arguments
* Print all entered words as a single string
* Print all entered words each on a separate line
* Sort the words
* Print all entered words each on a separate line
* Print all entered words as a single string
*/
import java.util.*;

public class stringAssignment{
public static void main (String [] entry){
if (entry.length < 2){
System.out.println ("\n Try again! Enter more than one word, please.");
System.exit(-1);
}
else{
System.out.println ("\n Thank you for entering " + entry.length + " words. Now the fun begins...");
}

String entrystring = "";
for (int i=0; i < entry.length; i++)
entrystring += entry[i] + "";

System.out.println ("\n Here are the entered words as a single string:" + "\n\t" + entrystring);

for (int i=0; i < entry.length; i++)
System.out.println ("\n Here are the words each printed on a separate line:");
System.out.println (entry[i]);

}
}
 
Jherald Lacambra
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe your length method does not work.. try to use length().. i think the length without () are use in javascript not in java :roll: :roll:
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A for loop only repeats the single statement that immediately follows it. Also, the counter variable i is only available in that single statement. In programming parlance, we say that the variable "has scope" or "is in scope" for that single statement. In order to make the for loop repeat multiple statements (and use the counter variable in those statements), you must put { and } around the multiple statements that you want to run.

I hope my verbal description is enough to help you figure out how to fix the problem.

Layne

p.s. It will help a lot if you use UBB CODE tags and proper formatting to help us read your code.
[ July 17, 2005: Message edited by: Layne Lund ]
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the scope of variable i in the 2nd for loop


Instead use braces for the for loop,


Now try and compile code see if it still gives problems.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jherald Lacambra:
maybe your length method does not work.. try to use length().. i think the length without () are use in javascript not in java :roll: :roll:


The syntax is correct and is not causing the error. Since entry is an array, it is incorrect to use length() as a method. It is a field instead.

Layne
 
Marilyn de Queiroz
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you make it a habit to always use braces on all if, while and for statements, you will have fewer problems of this kind. (Formatting your code using indentation will also probably help.) Because you didn't use braces following your last for loop, you effectively have



Notice how 'i' is defined inside the for loop (good), but the print statement is outside the loop?
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe your length method does not work.. try to use length().. i think the length without () are use in javascript not in java


Gonzalez is right on the length method.
 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code does not compile because the variable i used in entry[i] is outside the scope of the for loop. The variable i is only available to code inside the loop. The line System.out.println (entry[i]); is outside the for loop.

If you have no braces used in your loop, the first line of code is the only line inside the scope of the loop, all others will be outside the scope.

Putting braces on the loop will fix the problem, e.g.
for (int i=0; i < entry.length; i++) {
System.out.println ("\n Here are the words each printed on a separate line:");
System.out.println (entry[i]);
}
 
N. Gonzalez
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaah, yes, how could I forget scope? The braces did the trick!
I can now move on to the next part of my assignment. I'll be sure to return if I run into trouble.
Thank you so much for the speedy and truly helpful responses.
This community is wonderful...so generous with your knowledge!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!