1

I moved 3 files I was working on to a new package and now have a new error. I am still in the process of getting it working. The error is "Question cannot be resolved to a type" . I am only learning Java and am very new to it. Do I need to add in the other files as well? EDIT : This is question I added to the package but I have an error on this one now "The static field Question.nQuestion should be accessed in a static way" and I don't know why? I used two Asterisk to highlight the section with the error

package com.quiz6;
import javax.swing.JOptionPane;

public  abstract class Question {
  static int nCorrectAnswer = 0;
  static int nQuestion = 0;
  String question;
  String correctAnswer;
  //abstract method ask
  public abstract String ask();
  //the same method
  public void check() {
      String answer = ask();
      **this.nQuestion++;**
      if (answer.equalsIgnoreCase(this.correctAnswer)) {
          JOptionPane.showMessageDialog(null, "Correct!");
          **this.nCorrectAnswer++;**
      } else {
          JOptionPane.showMessageDialog(null, "Incorrect");
      }
  }
  public static void showResults() {
      JOptionPane.showMessageDialog(null, nCorrectAnswer + " correct out of questions " + nQuestion);
  }
}
package com.quiz6;
import com.quiz6.MultipleChoiceQuestion6;
import com.quiz6.TrueFalseQuestion;

public class quiz6 {

  public static void main(String[] args) {

    **
    Question question;
    question = new MultipleChoiceQuestion6("What is the capitol of Singapore?", **
      "Singapore",
      "Dublin",
      "Tokyo",
      "Shanghai",
      "Bangkok.",
      "A");

    question.check();

    question = new MultipleChoiceQuestion6("What is the capitol of America?",
      "New York",
      "San Francisco",
      "Alaska",
      "Washington DC",
      "Minnesota",
      "d");
    question.check();

    question = new MultipleChoiceQuestion6("What is the capitol of Europe?",
      "Ireland",
      "Germany",
      "Czech Republic",
      "France",
      "Trick Question",
      "e");
    question.check();

    question = new MultipleChoiceQuestion6("Who accidentally overwrote their Quiz file?",
      "Yourself",
      "Johann",
      "Norberto",
      "Trump",
      "Me!",
      "e");
    question.check();

    //true or false questions here
    question = new TrueFalseQuestion("Sharks are mammals", "FALSE");
    question.check();

    question = new TrueFalseQuestion("Spiders have six legs.", "FALSE");
    question.check();

    question = new TrueFalseQuestion("My brain is sore from Java.", "TRUE");
    question.check();
    //we show the results of the quiz
    question.showResults();
  }
}
Naalgoehk
  • 13
  • 4
  • you'll need to show the rest of your classes. what classes can't it find? are they in the correct package? do they have the correct package statements? – Stultuske May 13 '20 at 05:47
  • 2
    I assume you added the `**` before `Question` to mark the error line? What package does the `Question` class belong to? – dpr May 13 '20 at 05:48
  • The error says `Question` is not defined. You have probably not copied `Question` class. – Omid.N May 13 '20 at 05:53
  • Also [What's the difference between JavaScript and Java?](https://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java) – mplungjan May 13 '20 at 05:58
  • Please read [mcve] and enhance your question accordingly. But note: this is a very common problem, and the real answer is to actually carefully check whether the names of your classes (and packages) are 100% consistent with your directory structure and file names. – GhostCat May 13 '20 at 06:05
  • You might need to compile the classes in their new packages. – NomadMaker May 13 '20 at 06:46
  • Hi all, I added the question file in and it cleared that error. Now I am facing other errors in that file. "The static field Question.nQuestion should be accessed in a static way" (I couldn't fit all the code in sorry) package com.quiz6; import javax.swing.JOptionPane; public abstract class Question { static int nCorrectAnswer = 0; static int nQuestion = 0; String question; String correctAnswer; public abstract String ask(); public void check(){ String answer=ask(); this.nQuestion++;if (answer.equalsIgnoreCase(this.correctAnswer)) {JOptionPane.showMessageDialog(null, "Correct!"); – Naalgoehk May 13 '20 at 06:47
  • For new problems, create a new question please. – dpr May 13 '20 at 07:09

2 Answers2

0

"Question cannot be resolved to a type" you are getting this error because the program is unable to find the class named 'Question'. A very similar thing happened with me once, this happens when that file or class is not available withing the same package so, you need to copy the class 'Question' in that same package and hopefully this will solve the issue.

Siddhant
  • 626
  • 1
  • 7
  • 20
0

Please check the access modifier you have used for your class.

If you have not specified any access modifier to your class which means default modifier, then your class can only be accessed in the same package.

If you want to use your class from some other package try putting public access modifier in front of your class.

ex-

public class Question {

}
Anshul Sharma
  • 1,018
  • 3
  • 17
  • 39