Any help would be appreciated.

I have a working command line program (trivia question) and decided to try to put it in a Gui. I got the user menu to print, but it gets to line 162 and blows up (going line by line) where I'm thinking it should do getText then parse to an integer and go through the if statements. Our HW's are just command line, but I guess I like punishing myself, I've been working with it for a couple of hours looking at examples but I can't figure it out.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.*;

import javax.swing.*;

public class Program8Gui extends JPanel implements Serializable,
		ContainerListener, ActionListener {

	JTextArea display;
	JPanel buttonPanel;
	JButton gameAdminButton, playTriviaButton, clearButton;
	Vector<JButton> buttonList;
	static final String ADMIN = "Game Administration";
	static final String PLAY = "Play Trivia";
	static final String CLEAR = "Clear";
	static final String newline = "\n";

	public Program8Gui() {
		super(new GridBagLayout());
		GridBagLayout gridbag = (GridBagLayout) getLayout();
		GridBagConstraints c = new GridBagConstraints();

		// Initialize an empty list of buttons.
		buttonList = new Vector<JButton>(10, 10);

		// Create all the components.
		gameAdminButton = new JButton("Options");
		gameAdminButton.setActionCommand(ADMIN);
		gameAdminButton.addActionListener(this);

		playTriviaButton = new JButton("Play the Trivia Game");
		playTriviaButton.setActionCommand(PLAY);
		playTriviaButton.addActionListener(this);

		buttonPanel = new JPanel(new GridLayout(1, 1));
		buttonPanel.setPreferredSize(new Dimension(200, 75));
		buttonPanel.addContainerListener(this);

		display = new JTextArea(40, 30);
		display.setEditable(true);
		JScrollPane scrollPane = new JScrollPane(display);
		scrollPane.setPreferredSize(new Dimension(200, 75));

		clearButton = new JButton("Clear text area");
		clearButton.setActionCommand(CLEAR);
		clearButton.addActionListener(this);

		c.fill = GridBagConstraints.BOTH; // Fill entire cell.
		c.weighty = 1.0; // Button area and message area have equal height.
		c.gridwidth = GridBagConstraints.REMAINDER; // end of row
		gridbag.setConstraints(scrollPane, c);
		add(scrollPane);

		c.weighty = 0.0;
		gridbag.setConstraints(clearButton, c);
		add(clearButton);

		c.weightx = 1.0; // Add/remove buttons have equal width.
		c.gridwidth = 1; // NOT end of row
		gridbag.setConstraints(gameAdminButton, c);
		add(gameAdminButton);

		c.gridwidth = GridBagConstraints.REMAINDER; // end of row
		gridbag.setConstraints(playTriviaButton, c);
		add(playTriviaButton);

		c.weighty = 1.0; // Button area and message area have equal height.
		gridbag.setConstraints(buttonPanel, c);
		add(buttonPanel);

		setPreferredSize(new Dimension(400, 400));
		setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
	}

	void displayMessage(String action, ContainerEvent e) {
		display.setText(((JButton) e.getChild()).getText() + " was" + action
				+ e.getContainer().getClass().getName() + newline);
		display.setCaretPosition(display.getDocument().getLength());
	}

	/*
	 * This could have been implemented as two or three classes or objects, for
	 * clarity.
	 */
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();

		if (ADMIN.equals(command)) {

			// class GameAdmin implements Serializable{
			final long serialVersionUID = 2;
			Vector<Question> questionList;
			int numQuestions = 0;
			final String newline = "\n";

			// public GameAdmin() {

			// read from dat file if it exists

			/**
			 * @param args
			 */
			Vector<Question> vector = new Vector<Question>();
			questionList = vector;
			File file = new File("questions.dat");
			String fileName = "questions.dat";
			if (!file.exists()) {
				writeQuestionList(questionList, fileName);
			}
			Question q;

			try {
				ObjectInputStream in = new ObjectInputStream(
						new FileInputStream(file));
				try {
					questionList = (Vector<Question>) in.readObject();
					in.close();
					int questionListSize = questionList.size();
					display.setText("Question list size = "
							+ questionList.size());

				} catch (Exception e1) {
					display.setText(e1.toString());
					display.setText("No questions are present in the file");
				}
			} catch (FileNotFoundException e1) {
				display.setText("No question file found.");
			} catch (IOException e1) {
				display.setText("IOException.");
			}

			// write questions to file

			int guess = 0;
			String tempQuestion = null;
			String tempAnswer = null;
			int tempValue = 0;

			// Make menu
			// while (guess != 4) {
			// String string =
			// "Menu"+newline+"Please Choose a number"+newline;
			display.setText("Menu");
			display.append(newline + "Please Choose a number" + newline
					+ "1. Print Questions" + newline + "2. Add Question"
					+ newline + "3. Delete Question" + newline
					+ "4. End Program" + newline);

			String s = display.getText();
			guess = Integer.parseInt(s);
			 //Scanner scanner = new Scanner(System.in);
			 //guess = scanner.nextInt();

			// print out questions
			if (guess == 1) {
				if (questionList.size() <= 0) {
					display.append("The question list is empty");
				} else {
					for (int j = 0; j < questionList.size(); j++) {
						Question que = questionList.get(j);
						display.append("Question " + j + ". " + que.toString());
					}
				}
			}
			// add question
			if (guess == 2) {
				display.append("Enter a question");

				tempQuestion = display.getText();
				display.append(newline);
				System.out.println("Enter the answer to the question");
				tempAnswer = display.getText();
				display.append(newline);
				System.out.println("Enter the value 1 to 3");
				s = display.getText();
				tempValue = Integer.parseInt(s);
				display.append(newline);
				q = new Question(tempQuestion, tempAnswer, tempValue);
				questionList.add(q);
				numQuestions++;
			}
			// delete question
			if (guess == 3) {
				display.append("What question number do you want to delete");
				s = display.getText();
				display.append(newline);
				int j = Integer.parseInt(s);
				questionList.remove(j);
				numQuestions--;
			}
			// end program
			if (guess == 4) {
				writeQuestionList(questionList, fileName);
				display.append("End of program.");
				display.append(newline);
				System.exit(0);
			}
		}
		// }

		// }
		// }
		else if (PLAY.equals(command)) {
			int lastIndex = buttonList.size() - 1;
			try {
				display.append("play the game to be added");
			} catch (ArrayIndexOutOfBoundsException exc) {
			}
		} else if (CLEAR.equals(command)) {
			display.setText("");
		}
	}

	@Override
	public void componentAdded(ContainerEvent e) {
		// TODO Auto-generated method stub
		displayMessage(" added to ", e);
	}

	@Override
	public void componentRemoved(ContainerEvent e) {
		// TODO Auto-generated method stub
		displayMessage(" removed from ", e);
	}

	public String writeQuestionList(Vector<Question> questionList,
			String fileName) {
		try {
			// File data = new File(fileName);
			ObjectOutputStream out = new ObjectOutputStream(
					new FileOutputStream("questions.dat"));

			out.writeObject(questionList);

			return ("Question list saved to file");
		} catch (Exception e) {
			return ("Error saving to " + fileName + ". Question not saved.\n" + e);
		}
	}

	/**
	 * Create the GUI and show it. 
	 * 
	 */
	private static void createAndShowGUI() {
		// Create and set up the window.
		JFrame frame = new JFrame("ContainerEventDemo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Create and set up the content pane.
		JComponent newContentPane = new Program8Gui();
		newContentPane.setOpaque(true); // content panes must be opaque
		frame.setContentPane(newContentPane);

		// Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}

}// end class program8Gui

What's the error it's reporting?
I can make some guesses, but it's a lot easier if I know what the problem is...

AFAICS you have a text area in which you put the complete text of a menu. You then get that complete text back into a String, then try to parse the whole thing as a single integer. Obviously it's not just an integer, so Exception.
This is more than just a coding problem. Your design follows the pattern for a command line interface, but that's not going to work in a GUI. You need to prompt the user, wait for a response, then deal with the user's input. Have a look at JOptionPane for a good way to handle user input.

The code which you shows is helpful in making the mini projects.....

My errors are:
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Program8Gui.actionPerformed(Program8Gui.java:163)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

I didn't realize it would take the menu already printed into getText instead of my user input. How do I wait for user input in the same text area?

I wanted to enter in the questions in the same area which go into Vector questionList and then get written to a file.

My game program asks the question then compares the answer string entered by the user to the one from the file. I'm trying to do everyting in one text area to keep it simple

That's not the way to make in simple in a GUI. Look at JOptionPane

Thanks for all the help, it still has a few bugs like if it has an empty file it gives the score instead of saying no quesitons, I still have to comment it, and I will probably loop it a little differently, but other than that it works pretty well, and I would have never gotten it without the suggestions.

Thanks again.

import java.io.*;
import java.util.Scanner;
import java.util.Vector;

import javax.swing.JOptionPane;

public class Program8Gui implements Serializable {
	
	private static final long serialVersionUID = 1L;
	static final String newline = "\n";
	int guess = 0;
	String tempQuestion = null;
	String tempAnswer = null;
	int tempValue = 0;
	Vector<Question> questionList;
	File file = new File("questions.dat");
	String fileName = "questions.dat";
	private int score = 0;
	private int questionNum = 0;
	
	public Program8Gui() { // constructor header

		File file = new File("questions.dat");
		String fileName = "questions.dat";
		Vector<Question> vector = new Vector<Question>();
		questionList = vector;

		if (!file.exists()) {
			writeQuestionList(questionList, fileName);
		}
		Question q;

		try {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(
					file));
			try {
				questionList = (Vector<Question>) in.readObject();
				in.close();
				System.out.println("Question list size = "
						+ questionList.size());

			} catch (Exception e) {
				System.out.println(e.toString());
				System.out.println("No questions are present in the file");
			}
		} catch (FileNotFoundException e) {
			System.out.println("No question file found.");
		} catch (IOException e) {
			System.out.println("IOException.");
		}

		Object[] selectionValues = { "Play Trivia", "Game Administration" };
		String initialSelection = "Play Trivia";
		Object selection = JOptionPane.showInputDialog(null,
				"Do you want to administer the game or play it", "Trivia Game",
				JOptionPane.QUESTION_MESSAGE, null, selectionValues,
				initialSelection);
		System.out.println(selection);

		if (selection.equals("Game Administration")) {
			while (guess != 4) {
				String userInput = JOptionPane.showInputDialog("Menu" + newline
						+ "What number do you prefer?" + newline
						+ "1. Print Questions" + newline + "2. Add Question"
						+ newline + "3. Delete Question" + newline
						+ "4. End Program" + newline);
				guess = Integer.parseInt(userInput);

				if (guess == 1) {
					String s1 = null;
					String s2 = null;
					if (questionList.size() <= 0) {
						JOptionPane
								.showInputDialog("The question list is empty");
					} else {
						for (int j = 0; j < questionList.size(); j++) {
							Question que = questionList.get(j);
							s1 = ("Question " + j + ". " + que.toString() + newline);
							if (s2 == null) {
								s2 = s1;
							} else {
								s2 += s1;
							}
							System.out.println(s2);
						}
						JOptionPane.showMessageDialog(null, s2);
					}
				}
				// add question
				if (guess == 2) {
					
					tempQuestion = JOptionPane
							.showInputDialog("What is the question");
					tempAnswer = JOptionPane
							.showInputDialog("What is the answer to the question?");
					JOptionPane
							.showInputDialog("What is the value from 1 to 3?");
					tempValue = Integer.parseInt(userInput);
					q = new Question(tempQuestion, tempAnswer, tempValue);
					questionList.add(q);
					
				}

				// delete question
				if (guess == 3) {
					String s = JOptionPane
							.showInputDialog("What question number do you want to delete?");
					int j = Integer.parseInt(s);
					questionList.remove(j);
					}

				// end program
				if (guess == 4) {
					writeQuestionList(questionList, fileName);
					System.out.println("End of program.");
					System.out.println(newline);
					System.exit(0);
				}

			}
		} else {
			while (this.askNextQuestion()) {				
			}
			this.showScore();
			JOptionPane.showMessageDialog(null,
					"Game over! Thanks for playing!");
		}
	}

	/*************************************************************/
	public boolean askNextQuestion() {
		Question temp = null;
		
		if (questionNum < questionList.size()) {			
			temp = questionList.get(questionNum);			
			String guess = JOptionPane.showInputDialog("Question " + questionNum
					+ newline + temp.getQuestion());			
				guess = guess.toLowerCase();
			if (guess.equals(temp.getAnswer().toLowerCase())) {
				score += temp.getValue();
				JOptionPane.showMessageDialog(null, "That is correct! You get "
						+ temp.getValue() + " more points for a total of "+score);				
			} else {
				JOptionPane.showMessageDialog(null,
						"Wrong. The correct answer is:  " + temp.getAnswer());
			}
			 questionNum++;
			return true;
		} else {
			return false;
		}
	}

	/*************************************************************/
	public void showScore() {
		JOptionPane.showMessageDialog(null, "Your total score is now " + score);
	}

	/*************************************************************/
	public String writeQuestionList(Vector<Question> questionList,
			String fileName) {
		try {
			ObjectOutputStream out = new ObjectOutputStream(
					new FileOutputStream("questions.dat"));
			System.out.println(questionList.size());
			out.writeObject(questionList);

			return ("Question list saved to file");
		} catch (Exception e) {
			return ("Error saving to " + fileName + ". Question not saved.\n" + e);
		}
	}

	/************************************************************/
	public static void main(String[] args) {// main
		new Program8Gui();// instantiation
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.