I've been trying to create a personal GUI trivia program that chooses a random question from a database of 60 questions and asks you 30 questions total with a possibility of 4 answers for each question. Here is my full code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; import java.io.*; import java.util.*; import java.awt.Frame; import java.awt.TextArea; import java.awt.TextField; import java.awt.BorderLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class Game extends JFrame { //SELECTIONS JButton jbtFirstAnswer = new JButton("First Answer"); JButton jbtSecondAnswer = new JButton("Second Answer"); JButton jbtThirdAnswer = new JButton("Third Answer"); JButton jbtFourthAnswer = new JButton("Fourth Answer"); //QUESTION PANE JLabel jlbHeader = new JLabel("Question: "); JLabel jlbAnswerOne = new JLabel("A: "); JLabel jlbAnswerTwo = new JLabel("B: "); JLabel jlbAnswerThree = new JLabel("C: "); JLabel jlbAnswerFour = new JLabel("D: "); JTextField jtfQuestion = new JTextField(40); JTextField jtfAnswerOne = new JTextField(20); JTextField jtfAnswerTwo = new JTextField(20); JTextField jtfAnswerThree = new JTextField(20); JTextField jtfAnswerFour = new JTextField(20); public Game()throws IOException { //MAKE TEXT BOXES UNEDITABLE jtfQuestion.setEditable(false); jtfAnswerOne.setEditable(false); jtfAnswerTwo.setEditable(false); jtfAnswerThree.setEditable(false); jtfAnswerFour.setEditable(false); //START QUESTIONS JPanel questionsPanel = new JPanel(); questionsPanel.setLayout(new FlowLayout()); questionsPanel.add(jlbHeader, BorderLayout.NORTH); questionsPanel.add(jtfQuestion); //END QUESTIONS //START ANSWERS JPanel answerPanelOne = new JPanel(); answerPanelOne.setLayout(new FlowLayout()); answerPanelOne.add(jlbAnswerOne); answerPanelOne.add(jtfAnswerOne); JPanel answerPanelTwo = new JPanel(); answerPanelTwo.setLayout(new FlowLayout()); answerPanelTwo.add(jlbAnswerTwo); answerPanelTwo.add(jtfAnswerTwo); JPanel answerPanelThree = new JPanel(); answerPanelThree.setLayout(new FlowLayout()); answerPanelThree.add(jlbAnswerThree); answerPanelThree.add(jtfAnswerThree); JPanel answerPaneFour = new JPanel(); answerPaneFour.setLayout(new FlowLayout()); answerPaneFour.add(jlbAnswerFour); answerPaneFour.add(jtfAnswerFour); //ANSWER PANEL TOTAL JPanel answerMainPane = new JPanel(); answerMainPane.setLayout(new GridLayout(4,1)); answerMainPane.add(answerPanelOne); answerMainPane.add(answerPanelTwo); answerMainPane.add(answerPanelThree); answerMainPane.add(answerPaneFour); //START SELECTIONS JPanel choiceOnePanel = new JPanel(); choiceOnePanel.setLayout(new BorderLayout()); choiceOnePanel.add(jbtFirstAnswer); JPanel choiceTwoPanel = new JPanel(); choiceTwoPanel.setLayout(new BorderLayout()); choiceTwoPanel.add(jbtSecondAnswer); JPanel choiceThreePanel = new JPanel(); choiceThreePanel.setLayout(new BorderLayout()); choiceThreePanel.add(jbtThirdAnswer); JPanel choiceFourthPanel = new JPanel(); choiceFourthPanel.setLayout(new BorderLayout()); choiceFourthPanel.add(jbtFourthAnswer); JPanel choicePanel = new JPanel(); choicePanel.setLayout(new BorderLayout()); choicePanel.add(choiceOnePanel, BorderLayout.NORTH); choicePanel.add(choiceTwoPanel, BorderLayout.CENTER); choicePanel.add(choiceThreePanel, BorderLayout.SOUTH); choicePanel.add(choiceFourthPanel); choicePanel.setLayout(new FlowLayout()); add(choicePanel); add(questionsPanel); //END SELECTIONS JPanel mainPanel = new JPanel(); mainPanel.setLayout(new FlowLayout()); add(questionsPanel, BorderLayout.NORTH); add(answerMainPane, BorderLayout.CENTER); add(choicePanel, BorderLayout.SOUTH); //QUESTION DATABASE //Fills out the questionDatabase array by going through a for loop 30 times.. //First randomly generates 1 - 60 and chooses a question coorresponding to that number //by using a switch statement... //Lots of coding but couldn't find a better way. //Also sets up answer variables Scanner sf = new Scanner(new File ("answerKey.in")); String answerDatabase[] = new String[240]; //declare more than we need for(int j = 0; j < 240; j++) { answerDatabase[j] = sf.nextLine(); } Random randomGenerator = new Random(); int answerSet = -5; String answerOne = ""; String answerTwo = ""; String answerThree = ""; String answerFour = ""; String questionDatabase[] = new String[61]; int amountCorrect = 0; int amountWrong = 0; int correctAnswer = 0; int correctDatabase[] = new int[61]; int answerSetDatabase[] = new int[61]; for(int j = 0; j < 30; j++) { int randomInt = randomGenerator.nextInt(60); switch(randomInt + 1) { case 1: questionDatabase[j] = "Who was the first president of the United States?"; answerSetDatabase[j] = 0; correctAnswer = 2; correctDatabase[j] = 2; break; case 2: questionDatabase[j] = "Which of the following is a letter?"; answerSetDatabase[j] = 4; correctAnswer = 0; correctDatabase[j] = 0; break; case 3: questionDatabase[j] = "Which of these people play ice hockey?"; answerSetDatabase[j] = 8; correctAnswer = 3; correctDatabase[j] = 3; break; case 4: questionDatabase[j] = "Which of these is a soda?"; answerSetDatabase[j] = 12; correctAnswer = 1; correctDatabase[j] = 1; break; case 5: questionDatabase[j] = "Which of these is a professional sport?"; answerSetDatabase[j] = 16; correctAnswer = 2; correctDatabase[j] = 2; break; case 6: questionDatabase[j] = "As of April 2012, how many pokemon are there?"; answerSetDatabase[j] = 20; correctAnswer = 0; correctDatabase[j] = 0; break; case 7: questionDatabase[j] = "Do you like Programming?"; answerSetDatabase[j] = 24; correctAnswer = 3; correctDatabase[j] = 3; break; case 8: questionDatabase[j] = "Who's the best pony?"; answerSetDatabase[j] = 28; correctAnswer = 1; correctDatabase[j] = 1; break; case 9: questionDatabase[j] = "Which of these is an actual video game?"; answerSetDatabase[j] = 32; correctAnswer = 0; correctDatabase[j] = 0; break; case 10: questionDatabase[j] = "What does DSL mean in Networking?"; answerSetDatabase[j] = 36; correctAnswer = 3; correctDatabase[j] = 3; break; case 11: questionDatabase[j] = "How many permanent teeth do humans have?"; answerSetDatabase[j] = 40; correctAnswer = 1; correctDatabase[j] = 1; break; case 12: questionDatabase[j] = "Which of these is not in the digestive system?"; answerSetDatabase[j] = 44; correctAnswer = 3; correctDatabase[j] = 3; break; case 13: questionDatabase[j] = "Who has won the most Stanley Cups?"; answerSetDatabase[j] = 48; correctAnswer = 0; correctDatabase[j] = 0; break; case 14: questionDatabase[j] = "Who has won the most Stanley Cups?"; answerSetDatabase[j] = 52; correctAnswer = 2; correctDatabase[j] = 2; break; case 15: questionDatabase[j] = "Who holds the record for most goals in one NHL season?"; answerSetDatabase[j] = 56; correctAnswer = 1; correctDatabase[j] = 1; break; case 16: questionDatabase[j] = "How many keys are on a piano?"; answerSetDatabase[j] = 60; correctAnswer = 3; correctDatabase[j] = 3; break; case 17: questionDatabase[j] = "What was the first animal cloned?"; answerSetDatabase[j] = 64; correctAnswer = 0; correctDatabase[j] = 0; break; case 18: questionDatabase[j] = "What is the softest mineral?"; answerSetDatabase[j] = 68; correctAnswer = 0; correctDatabase[j] = 0; break; case 19: questionDatabase[j] = "What is the name of the scale that measures the hardness of minerals?"; answerSetDatabase[j] = 72; correctAnswer = 3; correctDatabase[j] = 3; break; case 20: questionDatabase[j] = "How many countries are on the equator?"; answerSetDatabase[j] = 76; correctAnswer = 2; correctDatabase[j] = 2; break; case 21: questionDatabase[j] = "The continent of Africa is made up of how many countries?"; answerSetDatabase[j] = 80; correctAnswer = 1; correctDatabase[j] = 1; break; case 22: questionDatabase[j] = "What is the best selling instrument in the world?"; answerSetDatabase[j] = 84; correctAnswer = 3; correctDatabase[j] = 3; break; case 23: questionDatabase[j] = "What band was once called the 'Golden Gate Rhythm Section'?"; answerSetDatabase[j] = 88; correctAnswer = 1; correctDatabase[j] = 1; break; case 24: questionDatabase[j] = "What is a 'Sea Wasp'?"; answerSetDatabase[j] = 92; correctAnswer = 2; correctDatabase[j] = 2; break; case 25: questionDatabase[j] = "Which country has not fought a war since 1814?"; answerSetDatabase[j] = 96; correctAnswer = 0; correctDatabase[j] = 0; break; case 26: questionDatabase[j] = "What's the most common non-contagious disease in the world?"; answerSetDatabase[j] = 100; correctAnswer = 3; correctDatabase[j] = 3; break; case 27: questionDatabase[j] = "Which country owns the island of Bermuda?"; answerSetDatabase[j] = 104; correctAnswer = 1; correctDatabase[j] = 1; break; case 28: questionDatabase[j] = "Which is the only lake in the world which has sharks?"; answerSetDatabase[j] = 108; correctAnswer = 0; correctDatabase[j] = 0; break; case 29: questionDatabase[j] = "Who said these words - 'I am the president of United States and I am not going to eat any more broccoli'."; answerSetDatabase[j] = 112; correctAnswer = 2; correctDatabase[j] = 2; break; case 30: questionDatabase[j] = "Which vehicle was invented to circumvent trench warfare?"; answerSetDatabase[j] = 116; correctAnswer = 3; correctDatabase[j] = 3; break; case 31: questionDatabase[j] = "What does the abbreviation GUI stand for?"; answerSetDatabase[j] = 120; correctAnswer = 1; correctDatabase[j] = 1; break; case 32: questionDatabase[j] = "Who was the first African-American player to join Major League Baseball?"; answerSetDatabase[j] = 124; correctAnswer = 0; correctDatabase[j] = 0; break; case 33: questionDatabase[j] = "In 1993 which sport did Michael Jordan give up basketball for?"; answerSetDatabase[j] = 128; correctAnswer = 3; correctDatabase[j] = 3; break; case 34: questionDatabase[j] = "Which of these company produces ice hockey equipment?"; answerSetDatabase[j] = 132; correctAnswer = 1; correctDatabase[j] = 1; break; case 35: questionDatabase[j] = "Which is the deadliest snake in the world?"; answerSetDatabase[j] = 136; correctAnswer = 0; correctDatabase[j] = 0; break; case 36: questionDatabase[j] = "How many hearts does an octopus have?"; answerSetDatabase[j] = 140; correctAnswer = 2; correctDatabase[j] = 2; break; case 37: questionDatabase[j] = "How many life boats did the Titanic have?"; answerSetDatabase[j] = 144; correctAnswer = 3; correctDatabase[j] = 3; break; case 38: questionDatabase[j] = "What is a group of frogs known as?"; answerSetDatabase[j] = 148; correctAnswer = 0; correctDatabase[j] = 0; break; case 39: questionDatabase[j] = "What was Walt Disney afraid of??"; answerSetDatabase[j] = 152; correctAnswer = 2; correctDatabase[j] = 2; break; case 40: questionDatabase[j] = "Who was the inventor of the elevator?"; answerSetDatabase[j] = 156; correctAnswer = 2; correctDatabase[j] = 2; break; case 41: questionDatabase[j] = "When did the World War II begin?"; answerSetDatabase[j] = 160; correctAnswer = 1; correctDatabase[j] = 1; break; case 42: questionDatabase[j] = "Who was the oldest US president to take office?"; answerSetDatabase[j] = 164; correctAnswer = 3; correctDatabase[j] = 3; break; case 43: questionDatabase[j] = "Which is the largest freshwater lake in the world?"; answerSetDatabase[j] = 168; correctAnswer = 0; correctDatabase[j] = 0; break; case 44: questionDatabase[j] = "Which of these is closest to the height of Mt. Everest?"; answerSetDatabase[j] = 172; correctAnswer = 1; correctDatabase[j] = 1; break; case 45: questionDatabase[j] = "Which is the deepest ocean in the world?"; answerSetDatabase[j] = 176; correctAnswer = 2; correctDatabase[j] = 2; break; case 46: questionDatabase[j] = "What is the most widely eaten fish in the world?"; answerSetDatabase[j] = 180; correctAnswer = 3; correctDatabase[j] = 3; break; case 47: questionDatabase[j] = "What planet is closest in size to our moon?"; answerSetDatabase[j] = 184; correctAnswer = 1; correctDatabase[j] = 1; break; case 48: questionDatabase[j] = "What's the most malleable metal?"; answerSetDatabase[j] = 188; correctAnswer = 3; correctDatabase[j] = 3; break; case 49: questionDatabase[j] = "How many Megabytes are in a Gigabyte?"; answerSetDatabase[j] = 192; correctAnswer = 1; correctDatabase[j] = 1; break; case 50: questionDatabase[j] = "What number does 'giga' stand for?"; answerSetDatabase[j] = 196; correctAnswer = 1; correctDatabase[j] = 1; break; case 51: questionDatabase[j] = "What's short for 'binary digit'?"; answerSetDatabase[j] = 200; correctAnswer = 0; correctDatabase[j] = 0; break; case 52: questionDatabase[j] = "How many U.S. states border the Gulf of Mexico?"; answerSetDatabase[j] = 204; correctAnswer = 1; correctDatabase[j] = 1; break; case 53: questionDatabase[j] = "What is the eastern most capital in the United Sates?"; answerSetDatabase[j] = 208; correctAnswer = 3; correctDatabase[j] = 3; break; case 54: questionDatabase[j] = "What's removed from water in the process of desalination?"; answerSetDatabase[j] = 212; correctAnswer = 2; correctDatabase[j] = 2; break; case 55: questionDatabase[j] = "What's the U. S. equivalent of 0.45 kilograms?"; answerSetDatabase[j] = 216; correctAnswer = 0; correctDatabase[j] = 0; break; case 56: questionDatabase[j] = "What's the common name for a cubic decimeter?"; answerSetDatabase[j] = 220; correctAnswer = 1; correctDatabase[j] = 1; break; case 57: questionDatabase[j] = "What tree gives us prunes?"; answerSetDatabase[j] = 224; correctAnswer = 3; correctDatabase[j] = 3; break; case 58: questionDatabase[j] = "What's the medical term for low blood sugar?"; answerSetDatabase[j] = 228; correctAnswer = 2; correctDatabase[j] = 2; break; case 59: questionDatabase[j] = "What disease is the focus of oncology?"; answerSetDatabase[j] = 232; correctAnswer = 0; correctDatabase[j] = 0; break; case 60: questionDatabase[j] = "What do leukemia sufferers have too many of?"; answerSetDatabase[j] = 236; correctAnswer = 1; correctDatabase[j] = 1; break; } } jtfQuestion.setText(questionDatabase[0]); answerOne = answerDatabase[answerSetDatabase[0]]; answerTwo = answerDatabase[answerSetDatabase[0] + 1]; answerThree = answerDatabase[answerSetDatabase[0] + 2]; answerFour = answerDatabase[answerSetDatabase[0] + 3]; //SET-UP ANSWERS jtfAnswerOne.setText(answerOne); jtfAnswerTwo.setText(answerTwo); jtfAnswerThree.setText(answerThree); jtfAnswerFour.setText(answerFour); jbtFirstAnswer.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(correctDatabase[0] == 0) { amountCorrect = amountCorrect + 1; } else { amountWrong = amountWrong + 1; } } }); } public static void main(String[] args)throws IOException { Game frame = new Game(); frame.setTitle("Game"); frame.setSize(700,500); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
Here is the file I am reading in:
answerKey.in
Jack Johnson George Bush George Washington No one J 47 !@#$ Purple Francisco Perez Daniel Derpentine Jessica Simpson Mike Smith Duff Soda Seven Up SuperBlunder Derpbutt Monitor Throwing Football Eating Ice Hockey Internet Licking 649 523 1,235 493 No Maybe I like creating internet. Yes Moniterdash Rainbow Dash Turtle Mare I am Irritating Stick Quest of Soul Suckers Sodaman Goes to Mars World of Doorcraft Dual Screen Lite Doesn't Seem Likely Days Since Login Digital Subscriber Line 26 32 20 38 Larynx Gallbladder Large Intestine Gastrocnemius Montreal Canadiens Detroit Red Wings Edmonton Oilers Toronto Maple Leafs Gordie Howe Wayne Gretzky Henri Richard Maurice 'Rocket' Richard Jack Johnson Wayne Gretzky Sidney Crosby Maurice Richard 81 76 102 88 A Sheep A Monkey A Rabbit A Mouse Talc Gold Onyx Copper DOS Scale Hard Scale Derp Scale Moh's Scale 5 3 14 11 53 47 25 65 Electric Guitar Violin Drums Harmonica Metallica Journey Justin Bieber Megadeth An Octopus A Great White Shark A Jellyfish An Electric Eel Sweden Finland France China Eczema HPV Herpes Tooth Decay United States Great Britian Finland No one Lake Nicaragua Lake Superior Lake Lavon Lake Sharktopia Barrack Obama George Washington George Bush John McCain Army Jeep SR-71 The Aeroplane The Tank Graphic Ursa Interface Graphical User Interface Great Uranus Institution Greece Ukraine Industries Jackie Robinson Sammy Sosa Tim Teebow Trevor Daley Ice Hockey He stayed in basketball Bowling Baseball Microsoft Bauer Wilson Optiplex The Black Mamba Spitting Cobra Viper Anaconda 1 5 3 2 132 73 40 20 An Army A Dart A Swarm A Pack Water Jellyfish Mice Ducks Jack Johnson Thomas Edison Elisha Otis Daniel Radcliff 1892 1939 1962 1921 John McCain George Bush George Washington William Harrison Lake Superior Lake Omitakawa Lake Lavon Lake Travis 10,421 Meters 8,848 Meters 21,518 Meters 4,581 Meters Atlantic Ocean Arctic Ocean Pacific Ocean The Baltic Sea Cod Fish Catfish The Bass The Herring Pluto Mercury Venus Mars Iron Talc Adamantium Gold 1,600 1,024 512 256 1,000 1,000,000,000 1,000,000 1,000,000,000,000 A bit A bite 1 0 7 5 3 12 Austin Sacramento Norfolk Augusta Sugar Microorganisms Salt Saliva 1 Pound 50 Pounds 2 Pounds 10 Pounds A Pint A Liter A Gallon A Meter The Pineapple Tree The Coconut Tree The Prune Tree The Plum Tree Hepatits Hemoglocyoma Hypoglycemia HypoThermia Cancer AIDs HIV Herpes Red Blood Cells White Blood Cells Monohydrogen Dioxide De-oxyinated Bood Cells
And here are the errors I get when I attempt to run the program:
error: local variable amountCorrect is accessed from within inner class; needs to be declared final line 623 error: local variable amountCorrect is accessed from within inner class; needs to be declared final line 623 error: local variable amountWrong is accessed from within inner class; needs to be declared final line 627 error: local variable amountWrong is accessed from within inner class; needs to be declared final line 627 error: local variable correctDatabase is accessed from within inner class; needs to be declared final line 621
I've attempted to put the code from lines 146 to 593 in different sections along with lines 617 to 630 but to no avail. What I want this code to do is it will ask you a question, give 4 possible answers, and then allow you to choose the answer via a button at the bottom of the screen then ask you a different one until 30 questions have been answered then tell you how many you got right and how many you got wrong. Can anyone please tell me how to get these 4 buttons to work correctly as I have tried everything. Thank you very much and I appreciate your help.