Results 1 to 4 of 4
  1. #1
    Deathmonger is offline Member
    Join Date
    Feb 2008
    Posts
    16
    Rep Power
    0

    Default Issue using FOR statment with NetBeans

    Hi,

    I'm running into an issue with this FOR statement. I'm using NetBeans as my IDE. I believe this code statement to be correct. However, NetBeans is showing me the following errors:

    "illegal start of type
    <identifier> expected
    <identifier> expected
    <identifier> expected"

    These errors are showing up for the statement below:

    Java Code:
    for(int i =0; i<=cardNames.length();i++) {
    Here is my code for the class. It seems as though NetBeans recognizes each of the "i" variables (created in the for statement,) as separate varibles. Has anyone seen this before, or know why this error is showing up?

    Java Code:
    public class Deck {
        public int totalCardsInDeck = 52;
        public HashMap<Integer, Card> deck = new HashMap<Integer, Card>();
            public String cardNames[] = {"Ace","One","Two","Three","Four","Five","Six",
            "Seven","Eight","Nine","Ten","Jack","Queen","King"};
               
            for(int i =0; i<=cardNames.length();i++) {  // Error shows up on this line
                Card card = new Card(cardNames[i]);
                card.cardValue = i;
                if(cardNames[i].equals("Ace")) {
                    card.cardValue = 11;
                }
                if(i >= 11) {
                    card.cardValue = 10;
                }
                deck.put(i,card);
            }
            
        }
    Jai guru deva om, Nothing's gonna change my world

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Have you tried compiling the program with javac?
    The error looks like an unmatched delimiter of some kind. The compiler has gotten lost somewhere before the flagged statement looking for the end of some statement and doesn't recognize the for() statement.
    If you can't see the missing end bit, comment out some stuff and see if the error message about the for() goes away.

    public String cardNames[] = {
    public String[] cardNames = new String[] {...


    PS. There is no card named One.

  3. #3
    Deathmonger is offline Member
    Join Date
    Feb 2008
    Posts
    16
    Rep Power
    0

    Default

    Lol...I knew that...(and I kept reminding myself that there is no "One" when writing the code.) Anyways, I tried commenting out some code, but that didn't solve the problem. When I put the for() statement in it's own method, then NetBeans recognizes it as a for() statement. Problem solve! Here is the final code (simple fix again) and thanks!

    Java Code:
    public class Deck {
        public int totalCardsInDeck = 52;
        public HashMap<Integer, Card> deck = new HashMap<Integer, Card>();
        public String cardNames[] = new String[]{"Ace","Two","Three","Four","Five","Six",
            "Seven","Eight","Nine","Ten","Jack","Queen","King"};
               
        public void buildDeck() {
            for(int i =0; i<=cardNames.length;i++) {
                Card card = new Card(cardNames[i]);
                card.cardValue = i;
                if(cardNames[i].equals("Ace")) {
                    card.cardValue = 11;
                }
                if(i >= 11) {
                    card.cardValue = 10;
                }
                deck.put(i,card);
            }
            
        }
    }
    Jai guru deva om, Nothing's gonna change my world

  4. #4
    hannehomuth's Avatar
    hannehomuth is offline Member
    Join Date
    Jul 2008
    Location
    Sommerfeld (Brandenburg, Germany)
    Posts
    36
    Rep Power
    0

    Default

    It's clear that the compiler throws this error. You tried to make a for loop in the class body without any method around. You will never be able to do that.

    Java Code:
    class SomeClass{
    
    public String someString; //OK
    public String aSecondSting //OK
    someSting = "HelloWorld"; //Ilegal here
    
    while(i<100) //Ilegal at this location
      i++;
    
    public SomeClass()
    {
       someString = "HelloWorld"; //OK
       while(i<100) //OK at this location
          i++;
    }
    }

Similar Threads

  1. Alignment Issue...
    By chanduseec in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 09-30-2008, 07:47 AM
  2. Compile Issue
    By CrazyShells Slam in forum New To Java
    Replies: 4
    Last Post: 05-14-2008, 09:51 PM
  3. JFormattedTextField Issue
    By teracomp in forum AWT / Swing
    Replies: 1
    Last Post: 01-21-2008, 07:55 PM
  4. Try Catch issue
    By curtis_fraser in forum Advanced Java
    Replies: 2
    Last Post: 12-14-2007, 12:04 AM
  5. Issue
    By chaitu444 in forum New To Java
    Replies: 2
    Last Post: 11-06-2007, 08:49 PM

Posting Permissions

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