My java program won't compile, help?
hi,
Does anyone know why my java program won't compile, this is the code:
Code:
import java.util.Scanner;
/* Let the user type in 10 names, and print them in reverse order. */
class nameList
{
public static void main(String[] args)
{
String[] names = {nicole, becky};
String listNames;
listNames = names[0];
names[0] = names[1];
names[1] = listNames;
System.out.println(names[0]);
System.out.println(names[1]);
}
}
These are the errors that I've got given:
Code:
nameList.java:8: cannot find symbol
symbol : variable nicole
location: class nameList
String[] names = {nicole, becky};
^
nameList.java:8: cannot find symbol
symbol : variable becky
location: class nameList
String[] names = {nicole, becky};
^
2 errors
I've rechecked the code and don't see anything wrong with it.
Re: My java program won't compile, help?
The compiler can not find definitions for the variables: becky and nicole.
Where are those variables defined?
Do you mean for those symbols to be Strings?
If so, enclose them in "s.
Re: My java program won't compile, help?
Hi meaganicole, welcome to the forums!
When you post code the formatting will be presented properly if you use the "code" tags. Put [code] at the start of the code and [/code] at the end.
Re: My java program won't compile, help?
Moved from 'Advanced Java'
Please keep beginner questions in the 'New to Java' section. Asking how to write compilable code cannot possibly be construed as an advanced topic.
db
Re: My java program won't compile, help?
try defining the array like this:
String[] aas = { "array", "of", "String" };
Re: My java program won't compile, help?
Your Scanner import is never used, and your main() method does nothing like what the comment indicates it should. Hint: you'll want an array with 10 elements, and two for loops: one to populate the array (that's where the Scanner import comes in handy), and the other to loop backwards through it and display the names.
Re: My java program won't compile, help?
Code:
String[] names = {nicole, becky};
String[] names = {"nicole", "becky"};
Re: My java program won't compile, help?
Sort of redundant, isn't it, to give the code after the problem and technique has been given several times already.
Re: My java program won't compile, help?
No.String type should be defined with quote.Like
String str = "...";