• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help this newbie...what am I doing wrong?

 
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]);

}
}
 
shiva viswanathan
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gonzalez

If you see the last few lines

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]);

Here the for loop is not within brackets { }
Hence the for loop is applicable to only the immediate next line

and the last line System.out.println (entry[i]); is not
within the loop
Also i is a var valid only within the loop and the last is outside the loop
Hence i is not recognized

To solve problem just use this

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]);
}

And please post such questions in the scjp forum not in the results
forum

Catch You Later
Shiva
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic