Results 1 to 9 of 9
  1. #1
    meaganicole is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default My java program won't compile, help?

    hi,

    Does anyone know why my java program won't compile, this is the code:
    Java 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:

    Java 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.
    Last edited by pbrockway2; 01-01-2012 at 01:56 AM. Reason: code tags added

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default 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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    ideaman is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: My java program won't compile, help?

    try defining the array like this:
    String[] aas = { "array", "of", "String" };

  6. #6
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default 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.

  7. #7
    qinix is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Re: My java program won't compile, help?

    Java Code:
    String[] names = {nicole, becky};
    String[] names = {"nicole", "becky"};

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default 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.

  9. #9
    qinix is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Re: My java program won't compile, help?

    No.String type should be defined with quote.Like
    String str = "...";

Similar Threads

  1. Written Java Program in Netbeans won't compile..
    By ptrasd12 in forum New To Java
    Replies: 5
    Last Post: 12-01-2011, 01:43 PM
  2. Compile and run a java program through another one
    By z.zojaji in forum Advanced Java
    Replies: 9
    Last Post: 07-15-2010, 07:06 AM
  3. How to compile java program in other than bin directory
    By amritpalpathak in forum New To Java
    Replies: 3
    Last Post: 06-10-2010, 03:14 PM
  4. Teach Me Pls How To Compile Java Program Using CMD
    By Lilsimple in forum Java Applets
    Replies: 1
    Last Post: 03-11-2010, 04:26 PM
  5. My Java program will not compile and run?
    By sabrown311313 in forum Java Applets
    Replies: 1
    Last Post: 09-23-2008, 09:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •