0

I am trying to make an array of objects. I have Two classes, the main and another class for the methods. The one i am running the program in is TestTrivia, the class with all the methods for TestTrivia is Question. Constructor for this class is public Question() { question = "null"; answer = "null"; pointValue = 0; } i am trying to do something like this Person personArray[] = new Person[5];

   for(int i=0; i< personArray.length; i++)
   {
    personArray[i] = new Person();
  }

So i have tried Question QuestionArray[] = new Question[5]; for(int i=0; i< QuestionArray.length; i++) { QuestionArray[i] = new Question(); } I have tried to use it in both classes, the main and the one full of methods. It should go into the main class correct? The error i am getting is the whole for statement is underlined and says: illegal start type cannot find symbol symbol: class i location: class triviagame.TriviaGame package QuestionArray does not exist triviagame is the package name, "TriviaGame" is the class name, "Question" is another class name.

9
  • 1
    Please start variable names with lower case, and arrays are typically declared as Question[] questionArray in Java (although I'd call it questions). Please show the exact code you're trying, not just a fragment. Commented Nov 20, 2011 at 23:39
  • the rest of the code is not really needed for this question, i am just trying to figure out why it is not working, no other method or line of code will affect this, that much i do know. I do not want to confuse people with this giant bulk of code when none of it directly or indirectly affects trying to make an array of objects Commented Nov 20, 2011 at 23:53
  • Oh, well, good luck then. You don't think it's a little weird it's referring to class i? There's absolutely nothing wrong with the fragment in isolation except for the misleading variable name. Commented Nov 20, 2011 at 23:54
  • I am just going off my book, and in all the examples it uses i or index. Commented Nov 20, 2011 at 23:59
  • As I said, nothing is wrong with the code as shown. That's why I believe the issue has to do with something besides the code as shown, and why I asked for its context. The misleading variable name is QuestionArray, not i. Commented Nov 21, 2011 at 0:02

2 Answers 2

1

The loop code needs to be within a method, like main.

Also, if they are not in the same package, you will need to import classes not in the "current" package. For example, if Question is in the default package, it will need to be imported into the TriviaGame class.

package triviagame;

import Question;

public class TriviaGame {
    public static void main(String[] args) {
        System.out.println("Welcome to the New Age Trivia game");
        System.out.println("You will be asked five questions, let us begin");
        Question questionArray[] = new Question[5];
        for (int i = 0; i < questionArray.length; i++) {
            questionArray[i] = new Question();
        }
    }
}

If the Question class is in the triviagame package as well, the import is unnecessary.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah i just noticed, i copied from another place and i copied it outside of the main block, still making those simple mistakes
@RedDragon The simple mistakes never stop ;) They just get easier to find.
1

You're trying to make methods calls outside of a method or constructor or initializer block. Don't do this, but instead make sure your code is in the proper location such as in the main or another method.

public class TriviaGame {

   public static void main(String[] args) 
   {
      System.out.println("Welcome to the New Age Trivia game");
      System.out.println("You will be asked five questions, let us begin");
   }

   // this code is sitting out in the middle of no-where
   Question questionArray[] = new Question[5];
   for(int i=0; i< questionArray.length; i++)
   {
     questionArray[i] = new Question();
   }

}

2 Comments

Seems i am not done with making simple mistakes yet, i copied it outside the block
@RedDragon: then you'll want to copy it inside the block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.