Thanks all! I just read your posts... I'm putting the first part of my code but PLEASE realize, this is my first big (for me) program -- I'm a newbie, I know it is a little scattered and probably not commented like it should be... so grace and mercy would be appreciated, but so would some coding advice! NOT TOO MENTION my layout issue problem.
=======================
import java.text.*; //for formatting
import javax.swing.*; //imports swing classes
import java.awt.event.*; //imports event listeners
import java.awt.Container; // Container methods
import java.awt.*; //layout manager
import java.io.*; // File readers
import java.util.*; // Tokenizer
import java.util.Arrays;
public class TriviaGame extends JFrame {
private JFrame mainFrame; //main container variable
private JButton answerButton; // Calculate button variable -
private JButton exitButton; // Exit button variable
private JTextField answerField; // Length text field variable
private JTextField scoreField; // Score field variable
private JTextField qmissedField; // Questions missed field
private JLabel[] toptenLabel; // Top Ten Labels (array so we can print them all as seperate labels)
private JLabel questionLabel; // The Questions label
private JLabel qmissedLabel; //Questions Missed label
private JLabel scoreLabel; //Score label
private JLabel answerLabel;
private JLabel totalPointLabel, answerReport;
private JLabel TopTenScores, top1, top2, top3, top4, top5, top6, top7, top8, top9, top10; //Top Ten Scores Labels
private String questionsFile = "questions.txt";
private String toptenFile = "topten.txt";
private String inLine, newScore, playerName;
private StringTokenizer st;
private int i, e, score, questionsMissed, randomQuestion;
private int[] qpointsInt, topScoresInt;
private double random;
private String[] answer, fileCustomer, question, qpoints, questionAsked, topScores, topNames, topTenScores;
/** Creates a new instance of TriviaGame */
public TriviaGame() {
mainFrame = new JFrame("Todd's Trivia"); //Defining container with label
//Buttons on interface
answerButton = new JButton("Submit"); //Creating Calc button
exitButton = new JButton("Exit"); // Creating Exit Button
qmissedLabel = new JLabel("Questions Missed:"); //Creating width label
scoreLabel = new JLabel("Score:"); //depth label
answerLabel = new JLabel("Answer");
answerReport = new JLabel("");
TopTenScores = new JLabel("TOP TEN SCORES!");
//text fields for labels
answerField = new JTextField(5);
scoreField = new JTextField(5);
qmissedField = new JTextField(5);
//These strings are for the data that will be read in from the external file (customers.txt)
question = new String[10];
answer = new String[10];
qpoints = new String[10];
qpointsInt = new int[10];
questionAsked = new String[10];
topScoresInt = new int[10]; //Int'ing the top ten scores from the file
topScores = new String[10];
topNames = new String[10];
topTenScores = new String[10]; //Concatenated scores and names for using Array.sort() if it will work!
HighScore[] top = new HighScore[10]; //High Score object that will properly sort the scores
questionsMissed = 0; //Questions Missed variable...duh!
//Assign ALL questions asked with the marker of "n" for NO - this variable will tell us if a question has been asked yet or not.
// Need to change this iteration as we add more questions!!!
for (e=0; e<10; e++)
{
questionAsked[e] = "n";
}
i = 0; //The line counter for reading from file
score = 0; //Score!
//TOP TEN SCORES MECHANISM - implementing the HighScore object!
try //the reader stream must have a means for throwing errors if problem with opening stream
{
BufferedReader br = new BufferedReader (new FileReader(toptenFile)); //Opens stream to read data from file
//The following few lines pull the data from the file line by line and create a seperate variable for each piece of data
while ((inLine = br.readLine())!= null)
{
st = new StringTokenizer (inLine);
topScores = st.nextToken();
topNames = st.nextToken();
topScoresInt=Integer.parseInt(topScores);
top = new HighScore(topScoresInt,topNames);
i++;
}
br.close(); //closing the stream
Arrays.sort(top);
i = 0; //Resetting this variable for the next read.
}
catch(IOException e) //Catches an IOExceptions and gives appropriate response.
{
JOptionPane.showMessageDialog(null, "Top Ten File NOT FOUND - exiting program", "File Not Found!",
JOptionPane.INFORMATION_MESSAGE);
}
top1 = new JLabel("1. " + top[9]);
top2 = new JLabel("2. " + top[8]);
top3 = new JLabel("3. " + top[7]);
top4 = new JLabel("4. " + top[6]);
top5 = new JLabel("5. " + top[5]);
top6 = new JLabel("6. " + top[4]);
top7 = new JLabel("7. " + top[3]);
top8 = new JLabel("4. " + top[2]);
top9 = new JLabel("5. " + top[1]);
top10 = new JLabel("6. " + top[0]);
try //the reader stream must have a means for throwing errors if problem with opening stream
{
BufferedReader br = new BufferedReader (new FileReader(questionsFile)); //Opens stream to read data from file
//The following few lines pull the data from the file line by line and create a seperate variable for each piece of data
random = Math.round(Math.random()*4);
randomQuestion = (int)random;
while ((inLine = br.readLine())!= null)
{
if (i == randomQuestion)
{
st = new StringTokenizer (inLine, "^");
question = st.nextToken();
answer = st.nextToken();
qpoints = st.nextToken();
qpointsInt=Integer.parseInt(qpoints);
questionLabel = new JLabel(question); //This creates a SINGLE question that has been chose randomly
totalPointLabel = new JLabel(qpoints); //This is to tell us what question it is.
questionAsked="y";
break;
}
i++;
}
br.close(); //closing the stream
}
catch(IOException e) //Catches an IOExceptions and gives appropriate response.
{
JOptionPane.showMessageDialog(null, "Question File NOT FOUND - exiting program", "File Not Found!",
JOptionPane.INFORMATION_MESSAGE);
}
Container c = mainFrame.getContentPane(); // Get the content pane
c.setLayout(new FlowLayout(FlowLayout.CENTER)); // Setting Layout
//adding all components to layout in order of appearence
c.add(questionLabel);
c.add(answerField);
c.add(answerLabel);
c.add(answerButton);
c.add(scoreField);
scoreField.setText("0");
c.add(scoreLabel);
c.add(qmissedLabel);
c.add(qmissedField);
c.add(answerButton);
c.add(exitButton);
c.add(totalPointLabel);
c.add(answerReport);
c.add(TopTenScores);
c.add(top1); c.add(top2); c.add(top3); c.add(top4); c.add(top5);
c.add(top6); c.add(top7); c.add(top8); c.add(top9); c.add(top10);
Message was edited by:
tvance929