1

I need to preface this with I am not allowed to use an IDE in class, I must use TextPad to compile and run. Any help would be greatly appreciated.

There are two files associated with this assignment, one called Lab5 and the data file called StateCapitals. When I compile the program, I get the following errors. I "think" it cant find the files in the same folder, but they are.

The purpose of the assignment is to generate a dialog box that asks for the capital of a randomly chosen state from the StateCapitals file. If the answer matches the data in the array contained in that file, the dialog box closes and a new one opens telling the user that the answer is correct or wrong whichever is applicable. Once the user has had 10 attempts, the program displays a new dialog box with the number of correct and wrong answers.

Program Code:

 import java.util.*;
 import javax.swing.*;
 import java.util.Random;

 public class Lab5
 {
 public static void main(String[] args)
 {

 StateCapitals caps = new StateCapitals();
 String [][] stateCapital = caps.GetCapital();
 Random rand = new Random();
 String [][] answer = new String [49][2];
 int col = 0;

 // Declare the variables
 String stateName = "";
 int count = 0;
 String capital = "";
 int correct = 0;
 int wrong = 0;


 // Create a scanner
 Scanner input = new Scanner(System.in);

  while (count < 10)
  {
  int randNum = rand.nextInt (48) + 1;

  // Input box for the capital
  String stateCapitalQuestionString = JOptionPane.showInputDialog(
   "What is the capital of " + stateCapital[randNum][0] + "?");
   capital = input.next();

  if (capital.matches(stateCapital[randNum][1]))
  {
     String stateCapitalRespString = JOptionPane.showInputDialog(
     "That is CORRECT!");
     correct++;
  }
  else
  {
     String stateCapitalRespString = JOptionPane.showInputDialog(
     "Incorrect Response!");
     wrong++;
  }
   count++;
 }

  String stateCapitalSummaryString = JOptionPane.showInputDialog(
  ("You answered " + correct + "correctly!\n) (There were " + wrong + "incorrect          answers."));
  }
}

Compile Error:

F:\Java\Lab 5\Lab5.java:14: error: cannot find symbol
 StateCapitals caps = new StateCapitals();
 ^
  symbol:   class StateCapitals
  location: class Lab5
F:\Java\Lab 5\Lab5.java:14: error: cannot find symbol
 StateCapitals caps = new StateCapitals();
                      ^
  symbol:   class StateCapitals
  location: class Lab5

Any help would be appreciated! Thanks!

2
  • 3
    What are you calling to compile? javac ... Commented Mar 14, 2012 at 18:50
  • 2
    I am not allowed to use an IDE in class, but you are allowed to ask other people to help you by using their IDEs, or do I have to use TextPad too? Commented Mar 14, 2012 at 18:54

7 Answers 7

1

Based on your description, you have to read data in from StateCapitals. That means a FileReader(probably wrapped in BufferedReader) to get the data into a usable format in your code. Unless StateCapitals is actually a java file, then you just need to make sure both are compiled and in the same package.

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

Comments

1

Based on the code you've posted, the class StateCapitals doesn't exist. Have you forgotten to import a package?

Comments

1

Just compile your StateCapitals class first and then try to compile Lab5 or classes which are using StateCapitals class, or else in StateCapital in different package you need to import that,

For compiling all java files in current directory you can use

javac *.java

1 Comment

You're probably trying to compile each .java file separately. However, the javac executable does not assume anything but the jre libraries are in your classpath. You should either compile both files at the same time as above, or use "javac -cp . File.java" to compile them one at a time.
0

If you don't import StateCapitals, both your classes need to be in the same package. This is done by adding at the first line of both file package packagename;.

Comments

0

Is StateCapitals a class you defined? If so, you will have to import it just like you imported other library such as import java.util.Random;.

Comments

0

Looks like a classpath problem What are you passing to javac? Try with javac -classpath . Lab5.java. That is, assuming both Lab5 and StateCapitals are in the same package. If not, you need to import StateCapitals in Lab5

Comments

0

I use TextPad myself, and would only get that error if there were no "StateCapital" class imported, or no StateCapital file in source folder OR as is most likely in your case I think is that you haven't compiled StateCapitals.java, it needs to contain at least the following code:

public class StateCapitals
{

}

Comments

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.