I have a JTextField in a JApplet with a ActionListener. I compiled it using Eclipse and it works fine. But when I try to load it in a .html file using applet, the JTextField does not register/recognize the ENTER key when I press it. It seems like the ActionListener did not work. I used:
public void init() {
textField = new JTextField(20);
textField.setText("Enter your question here.");
textField.selectAll();
textField.addActionListener(this);
textArea = new JTextArea(10, 20);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// Add Components to the Applet.
GridBagLayout gridBag = new GridBagLayout();
Container contentPane = getContentPane();
contentPane.setLayout(gridBag);
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
gridBag.setConstraints(textField, c);
contentPane.add(textField);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
gridBag.setConstraints(scrollPane, c);
contentPane.add(scrollPane);
newline = System.getProperty("line.separator");
}
public void actionPerformed(ActionEvent event) {
String text = textField.getText();
String question = "";
String answer = "";
question = textField.getText();
question = ProcessString(question);
answer = Answer(question);
textArea.append(text + newline);
textArea.append(answer + newline);
textField.selectAll();
}
static String noAnswer;
static boolean knowAnswer = true;
// process the question string, take out non-ACSII characters, spaces, to
// lower space
public String ProcessString(String question) {
question = question.toLowerCase();
String[] words = question.split("\\s+");
question = "";
for (int wordCount = 0; wordCount < words.length; wordCount++) {
words[wordCount] = words[wordCount].replaceAll("[^A-Za-z]", "");
if (wordCount != words.length - 1)
question = question + words[wordCount] + " ";
else
question = question + words[wordCount];
// System.out.println(words[wordCount]);
}
return question;
}
public String Answer(String question) {
String answer = "";
/*
if the database know the answer (did not not know), then return the
answer. if the database does not know the answer, then recover the
answer in database.
*/
if (knowAnswer == true) {
// open the database file and search if questions matches any one in
// the
// database
Scanner sc = null;
try {
sc = new Scanner(new File("database.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int answerFrequency = 0;
boolean matchFound = false;
while (sc.hasNext()) {
int questionCount = sc.nextInt();
String line = sc.nextLine();
String[] databaseLine = line.split("\\s+");
String databaseQuestion = "";
String databaseAnswer = "";
// collect words for database questions
for (int wordCount = 1; wordCount <= questionCount; wordCount++) {
if (wordCount != questionCount)
databaseQuestion = databaseQuestion
+ databaseLine[wordCount] + " ";
else
databaseQuestion = databaseQuestion
+ databaseLine[wordCount];
}
// collect words for database answer
for (int wordCount = questionCount + 2; wordCount < databaseLine.length; wordCount++) {
databaseAnswer = databaseAnswer + databaseLine[wordCount]
+ " ";
}
// if the question is found in database, print answer
if (question.equals(databaseQuestion)) {
matchFound = true;
// the current answer is more frequency than the previous
// found
// answer, reassign the current answer the find answer
if (answerFrequency < Integer
.parseInt(databaseLine[questionCount + 1])) {
answerFrequency = Integer
.parseInt(databaseLine[questionCount + 1]);
answer = databaseAnswer;
}
}
}
if (matchFound == true) {
// System.out.println(answer);
knowAnswer = true;
} else {
// System.out.println("I don't know what you mean. How should I answer your question?");
knowAnswer = false;
noAnswer = question;
answer = "I don't know how to respond. How should I answer that?";
}
sc.close();
} else if (knowAnswer == false) {
String[] word = noAnswer.split(" ");
BufferedWriter writer = null;
answer = question;
try {
writer = new BufferedWriter(
new FileWriter("database.txt", true));
writer.newLine();
writer.write(word.length + " " + noAnswer + " 1 " + answer);
} catch (IOException e) {
} finally {
try {
if (writer != null)
writer.close();
} catch (IOException e) {
System.out.println("Cannot write to file");
}
}
answer = "I got that.";
knowAnswer = true;
}
return answer;
}
Really appreciate the help.
This seems to work for me....
public class TestApplet04 extends JApplet implements ActionListener {
private JTextField textField;
private JTextArea textArea;
private String newline;
public void init() {
textField = new JTextField(20);
textField.setText("Enter your question here.");
textField.selectAll();
textField.addActionListener(this);
textArea = new JTextArea(10, 20);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// Add Components to the Applet.
GridBagLayout gridBag = new GridBagLayout();
Container contentPane = getContentPane();
contentPane.setLayout(gridBag);
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
gridBag.setConstraints(textField, c);
contentPane.add(textField);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
gridBag.setConstraints(scrollPane, c);
contentPane.add(scrollPane);
newline = System.getProperty("line.separator");
}
public void actionPerformed(ActionEvent event) {
String text = textField.getText();
String question = "";
String answer = "";
question = textField.getText();
// question = ProcessString(question);
// answer = Answer(question);
textArea.append(text + newline);
textArea.append(answer + newline);
textField.selectAll();
}
}
Updated after feedback
Here's you major problem...
sc = new Scanner(new File("database.txt"));
This is going to cause you a number of problems. First of all, the applet isn't likely to have access rights to read from the client machine, so you're likely to run into a security exception. Secondly, as the previous statement may have suggested, the file ins't likely to exist on the client machine.
You need to embed this resource within the Jar of your application and use getClass().getResource("...") to gain access to it. This will return a URL, from which you can use URL#openConnection to gain access to a InputStream, which can use in your scanner.
Try adding an Action to the ActionMap and setting the InputMap up for ENTER key.
textField.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter" );
textField.getActionMap().put("enter", new AbstractAction() {...} );
The field will have to have focus.
I'm also a newbie to Java, but found out that JTextfield is only a single line entry.
If you want a multiline entry, you would need to use a JTextArea instead.
Hope this helps :)
Related
On start up my program gives me the message java.io.IOException: stream closed. It then opens with limited components.
I understand what a stream closed exception is, I just can't figure out how to fix it, in my case. I would really appreciate some help. (I'm sorry about the big chunk of code, but I am not sure what you need to help me)
try {
//Defines new file reader and buffered reader
FileReader file_to_read = new FileReader(path);
BufferedReader br1 = new BufferedReader(file_to_read);
//action code
try {
//Finds the line number to see the number of cards in each set
lnr2= new LineNumberReader(new FileReader(new File("C:\\Users\\priceadria\\Desktop\\Words_" + strSetName + ".txt")));
lnr2.skip(Long.MAX_VALUE);
strLineNumber2 = String.valueOf(lnr2.getLineNumber());
lineNumber2 = Integer.parseInt(strLineNumber2);
//Closes the file reader and buffered reader
br1.close();
file_to_read.close();
} catch (IOException | NumberFormatException e) {
strLineNumber2 = "0";
}
//Adds the set name to the set name array
lblDeckName[row] = new JLabel (strSetName);
//Adds all buttons that will be used in the layout into a button array
JButton aryOptions [][] = new JButton [3][lineNumber];
//Defines and formats the Add Words button
JButton btnAddWords = new JButton("Add Words");
btnAddWords.setOpaque(false);
btnAddWords.setContentAreaFilled(false);
btnAddWords.setBorder(null);
btnAddWords.setBorderPainted(false);
//Defines and formats the Play button
JButton btnPlay = new JButton("Play");
btnPlay.setOpaque(false);
btnPlay.setContentAreaFilled(false);
btnPlay.setBorder(null);
btnPlay.setBorderPainted(false);
//Defines and formats the Delete Set button
JButton btnDelete = new JButton("Delete Set");
btnDelete.setOpaque(false);
btnDelete.setContentAreaFilled(false);
btnDelete.setBorder(null);
btnDelete.setBorderPainted(false);
//Adds each button to the array based on the number of sets
aryOptions [0][row] = btnAddWords;
aryOptions [1][row] = btnPlay;
aryOptions [2][row] = btnDelete;
//Formats the GridBagLayout (Spaces are for formatting purposes)
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = row;
layoutBackground.add(lblDeckName[row],gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = row;
layoutBackground.add(new JLabel(" "),gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx =2;
gbc.gridy = row;
layoutBackground.add(new JLabel (strSubjectName),gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 3;
gbc.gridy = row;
layoutBackground.add(new JLabel(" "),gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 4;
gbc.gridy = row;
layoutBackground.add(new JLabel(strLineNumber2),gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 5;
gbc.gridy = row;
layoutBackground.add(new JLabel(" | "),gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 6;
gbc.gridy = row;
layoutBackground.add(aryOptions[0][row],gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 7;
gbc.gridy = row;
layoutBackground.add(new JLabel(" | "),gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 8;
gbc.gridy = row;
layoutBackground.add(aryOptions[1][row],gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 9;
gbc.gridy = row;
layoutBackground.add(new JLabel(" | "),gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 10;
gbc.gridy = row;
layoutBackground.add(aryOptions[2][row],gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 11;
gbc.gridy = row;
layoutBackground.add(new JLabel(" |"),gbc);
//Creates an action event for the delete set button
btnDelete.addActionListener((ActionEvent a) -> {
//Finds the source of the button that was clicked
String strSource = String.valueOf(a.getSource());
String strSourceCut1 = strSource.substring(25,30);
//Finds the index of the button that was clicked
String [] aryIndex = strSourceCut1.split(",");
String strIndex = aryIndex[0];
try {
//Defines new file reader and buffered reader
FileReader file_to_read2 = new FileReader(path);
BufferedReader br2 = new BufferedReader(file_to_read2);
//Finds the numerical index of the button that was clicked
int intIndex = Integer.parseInt(strIndex)/16;
//Creates a counter to count line numbers
int counter = 0;
while((br2.readLine()) != null) {
counter ++;
if(intIndex == 0) { //If the index is the first option the normal try/catch returns null pointer, so a new try/catch had to be created
try {
//Finds the name of the set based on what button was clicked
String chosenDeck2 = br2.readLine();
String [] aryDeleteDeck = chosenDeck2.split(",");
deleteDeck = aryDeleteDeck[0];
//Closes the buffered reader and file reader
br2.close();
file_to_read2.close();
//Creates the path to the file that needs to be deleted
Path p2 = Paths.get("C:\\Users\\priceadria\\Desktop\\Words_" + deleteDeck + ".txt");
Files.delete(p2);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e);
}
} else if ( counter == intIndex) {
//Reads the deck name based on the button that was clicked
String chosenDeck2 = br2.readLine();
//Splits the deck name from the subject
String [] aryDeleteDeck = chosenDeck2.split(",");
deleteDeck = aryDeleteDeck[0];
//Closes the buffered reader and file reader
br2.close();
file_to_read2.close();
//Gets the file that needs to be deleted
Path p1 = Paths.get("C:\\Users\\priceadria\\Desktop\\Words_" + deleteDeck + ".txt");
Files.delete(p1);
}
}
}
catch (HeadlessException | IOException | NumberFormatException e) {
JOptionPane.showMessageDialog(null, e);
}
});
//Creates an action event for the add words button
btnAddWords.addActionListener((ActionEvent a) -> {
//Finds the source of the button that was clicked
String strSource = String.valueOf(a.getSource());
String strSourceCut1 = strSource.substring(25,30);
//Finds the index of the button that was clicked
String [] aryIndex = strSourceCut1.split(",");
String strIndex = aryIndex[0];
try {
//Creates a new file reader and buffered reader
FileReader file_to_read3 = new FileReader(path);
BufferedReader br3 = new BufferedReader(file_to_read3);
//Finds the index value
int intIndex = Integer.parseInt(strIndex)/16;
//Creates a counter to count line numbers
int counter = 0;
while((br3.readLine()) != null) {
//Increases counter value by 1
counter ++;
if(intIndex == 0)
{
try {
//Reads the set and subject for the
String chosenDeck2 = br3.readLine();
//Splits the subject from the set
String [] aryDeck = chosenDeck2.split(",");
chosenDeck = aryDeck[0];
//Closes the buffered reader and file reader
br3.close();
file_to_read3.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
} else if ( counter == intIndex) {
String chosenDeck2 = br3.readLine();
String [] aryDeck = chosenDeck2.split(",");
chosenDeck = aryDeck[0];
br3.close();
file_to_read3.close();
}
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
//Opens the new words frame
frmWords s = new frmWords(deckName, newXPoint, newYPoint, chosenDeck);
s.setVisible(true);
});
}
} catch (Exception e ) {
JOptionPane.showMessageDialog(null, e);
}
What I am finding is that when I am deleting the br1.close() the stream closed error stops, but I can find any reason why? Nothing is using that reader, that I can see. And when I do delete it I get and error saying that the test files are being used by other processes, which I can only presume is br1.
I know this may seem confusing to look at, probably because it is sloppy code, but I would really appreciate some help. I am a high school student.
Thanks :)
You could use the closable feature from Java 7:
try ( FileReader file_to_read = new FileReader(path);
BufferedReader br1 = new BufferedReader(file_to_read)){
//Finds the line number to see the number of cards in each set
lnr2= new LineNumberReader(new FileReader(new File("C:\\Users\\priceadria\\Desktop\\Words_" + strSetName + ".txt")));
lnr2.skip(Long.MAX_VALUE);
strLineNumber2 = String.valueOf(lnr2.getLineNumber());
lineNumber2 = Integer.parseInt(strLineNumber2);
} catch (IOException | NumberFormatException e) {
strLineNumber2 = "0";
}
Then you don't have to care about closing your stream or file in the right order.
Furthermore you could just use libs for doing the "readFile" job like apache commons or guava.
When you construct nested Readers as you do, for example,
FileReader file_to_read = new FileReader(path);
BufferedReader br1 = new BufferedReader(file_to_read);
, closing the outer one automatically closes the inner one, too. You do not need to close the inner one yourself, and if you try to do so after closing the outer one, as you do, you will find that it is already closed.
The best solution is to close only the outer one (as opposed to your workaround of closing only the inner one). You probably don't even need to retain a reference to the inner one -- a more typical idiom would go something like this:
BufferedReader br1 = new BufferedReader(new FileReader(path));
try {
// ... manipulate br1 ...
} finally {
br1.close();
// no need to explicitly close the inner FileReader
}
You could write that in try-with-resources form if that suits your fancy.
Similar applies to nested Writers, InputStreams, and OutputStreams, too.
I am trying to create a java applet that uses text fields to add strings to a linked list. I can not get the search button to work. I am trying to get the string specified by the user in the text field and then search the list and print how many times the word has been found if any.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.ListIterator;
/**
* Created by joshuaogunnote on 31/10/2015.
*/
public class Applet2 extends JApplet {
private String text;
private int text1;
JTextField value1, value2;
LinkedList<String> list = new LinkedList<String>();
public JLabel jLabel;
public int count = 0;
I have created this search_count variable to count up how many times the word has been found.
public int search_count = 0;
public void init() {
JLabel prompt = new JLabel("Please enter a word");
JLabel prompt1 = new JLabel("Please enter a certain letter");
value1 = new JTextField(10);
value2 = new JTextField(10);
JPanel textPanel = new JPanel();
textPanel.add(prompt);
textPanel.add(value1);
add(textPanel, BorderLayout.NORTH);
textPanel.add(prompt1);
textPanel.add(value2);
JPanel centrePanel = new JPanel();
text = "";
jLabel = new JLabel(text);
centrePanel.add(jLabel);
add(centrePanel, BorderLayout.CENTER);
JButton but = new JButton("Add word");
JButton but1 = new JButton("Clear");
JButton but2 = new JButton("Remove first occurrence");
JButton but3 = new JButton("Remove all occurrences");
JButton but4 = new JButton("Display all words begging with certain letter");
JButton but5 = new JButton("Search");
JPanel butPanel = new JPanel();
butPanel.add(but);
butPanel.add(but1);
butPanel.add(but5);
butPanel.add(but2);
butPanel.add(but3);
butPanel.add(but4);
add(butPanel, BorderLayout.SOUTH);
but.addActionListener(new ButtonHandler(this));
but1.addActionListener(new ButtonHandler1(this));
but5.addActionListener(new ButtonHandler2(this));
}
class ButtonHandler implements ActionListener {
private Applet2 theApplet;
public ButtonHandler(Applet2 app) {
theApplet = app;
}
public void actionPerformed(ActionEvent e) {
text = theApplet.value1.getText();
try {
text1 = Integer.parseInt(text);
jLabel.setText("ERROR - The string " + "'" + text1 + "'" + " is not a valid word");
} catch (NumberFormatException e1) {
if (text.length() != 0) {
jLabel.setText("Word " + "'" + text + "'" + " has been added to the list");
count = count + 1;
} else {
jLabel.setText("ERROR - Please enter a word");
}
}
}
}
class ButtonHandler1 implements ActionListener {
private Applet2 theApplet;
public ButtonHandler1(Applet2 app) {
theApplet = app;
}
public void actionPerformed(ActionEvent e) {
list.clear();
jLabel.setText("List has been cleared");
count = 0;
}
}
class ButtonHandler2 implements ActionListener {
private Applet2 theApplet;
public ButtonHandler2(Applet2 app) {
theApplet = app;
}
public void actionPerformed(ActionEvent e) {
String text = theApplet.value1.getText();
Here I am trying to use a for loop to iterate through all the strings in the list and increment search_count if a match has been found. It does not however produce the correct answer. I am also trying to produce an ERROR message when the user tries to search for a word that is not in the list. How do I get the search_count variable and how do I get the ERROR message to show at the correct time?
for (int i = 0; i < list.size(); i++) {
if(text.equals(list.get(i))){
search_count = search_count + 1;
} else {
jLabel.setText("ERROR - word is not in the list")
}
jLabel.setText("Word " + "'" + text + "'" + " was found " + search_count + " time(s) in the list");
if (text.length() == 0) {
jLabel.setText("Please enter a word - The total number of words in the list are: " + count);
}
}
}
}
It seems you did not declare count as a variable before you used it in the loop. If the output is not what you want / expect, the condition of the loop gives you something else then what you think it does.
int count = 0;
My code now works for sum, subtract, multiple, divide.
However it doesn't show the decimal when I put 10.0 and 2.0 or 5.4 and 2.
How should I change, then it can be used for decimal??
This is my code now.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
// instance variables
JLabel num1Label = null;
JTextField num1Text = null;
JLabel num2Label = null;
JTextField num2Text = null;
JButton sumButton = null;
JButton multipleButton=null;
JButton divideButton=null;
JButton subtractButton = null;
JLabel ansLabel = null;
// constructor
public Calculator() {
setLayout(new GridBagLayout()); // set layout (how elements are arranged in window)
// **** label: num 1
GridBagConstraints layoutCon = new GridBagConstraints();
layoutCon.gridx = 0;
layoutCon.gridy = 0;
layoutCon.insets = new Insets(10,10,10,10);
num1Label = new JLabel("Num 1:");
add(num1Label,layoutCon); // added label to JFrame
// **** text field: num 1
layoutCon = new GridBagConstraints();
layoutCon.gridx = 1;
layoutCon.gridy = 0;
layoutCon.insets = new Insets(10,10,10,10);
num1Text = new JTextField(20);
add(num1Text, layoutCon);
// **** label: num 2
layoutCon = new GridBagConstraints();
layoutCon.gridx = 0;
layoutCon.gridy = 1;
layoutCon.insets = new Insets(10,10,10,10);
num2Label = new JLabel("Num 2:");
add(num2Label,layoutCon); // added label to JFrame
// **** text field: num 2
layoutCon = new GridBagConstraints();
layoutCon.gridx = 1;
layoutCon.gridy = 1;
layoutCon.insets = new Insets(10,10,10,10);
num2Text = new JTextField(20);
add(num2Text, layoutCon);
// **** Sum Button
layoutCon = new GridBagConstraints();
layoutCon.gridx = 0;
layoutCon.gridy = 2;
layoutCon.insets = new Insets(10,10,10,10);
sumButton = new JButton("Sum");
add(sumButton, layoutCon);
sumButton.addActionListener(this); // register sumButton with the ActionListener in Calculator
// Multiple Button
layoutCon= new GridBagConstraints();
layoutCon.gridx=0;
layoutCon.gridy=4;
layoutCon.insets=new Insets(10,10,10,10);
multipleButton= new JButton("Multiple");
add(multipleButton, layoutCon);
multipleButton.addActionListener(this);
// Divide Button
layoutCon = new GridBagConstraints();
layoutCon.gridx = 0;
layoutCon.gridy = 5;
layoutCon.insets = new Insets(10,10,10,10);
divideButton = new JButton("Divide");
add(divideButton, layoutCon);
divideButton.addActionListener(this);
// **** label: answer
layoutCon = new GridBagConstraints();
layoutCon.gridx = 1;
layoutCon.gridy = 2;
layoutCon.insets = new Insets(10,10,10,10);
ansLabel = new JLabel("Answer");
add(ansLabel,layoutCon); // added label to JFrame
// **** Subtract Button
layoutCon = new GridBagConstraints();
layoutCon.gridx = 0;
layoutCon.gridy = 3;
layoutCon.insets = new Insets(10,10,10,10);
subtractButton = new JButton("Subtract");
add(subtractButton, layoutCon);
subtractButton.addActionListener(this); // register subtractButton with the ActionListener in Calculator
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Sum")) {
ansLabel.setText("" + (Integer.parseInt(num1Text.getText()) + Integer.parseInt(num2Text.getText())));
}
else if (e.getActionCommand().equals("Subtract")) {
ansLabel.setText("" + (Integer.parseInt(num1Text.getText()) - Integer.parseInt(num2Text.getText())));
}
else if (e.getActionCommand().equals("Multiple")){
ansLabel.setText(""+(double)(Integer.parseInt(num1Text.getText())*Integer.parseInt(num2Text.getText())));
}
else if (e.getActionCommand().equals("Divide")){
ansLabel.setText(""+(Integer.parseInt(num1Text.getText())/Integer.parseInt(num2Text.getText())));
}
}
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.pack(); // resizes window (JFrame) so you can see the elements in it
calc.setVisible(true); // make window visible
calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit program when close window
}
}
actionPerformed is parsing its inputs as int and operating on them as such. If you want to use floating-point numbers, try using double instead.
Parsing as an integer cannot handle double's or any floating point number (decimal) as such change your actionPerformed to parse double's instead or parse int's:
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Sum")) {
ansLabel.setText("" + (Double.parseDouble(num1Text.getText()) + Double.parseDouble(num2Text.getText())));
}
else if (e.getActionCommand().equals("Subtract")) {
ansLabel.setText("" + (Double.parseDouble(num1Text.getText()) - Double.parseDouble(num2Text.getText())));
}
else if (e.getActionCommand().equals("Multiple")){
ansLabel.setText(""+(double)(Double.parseDouble(num1Text.getText())*Double.parseDouble(num2Text.getText())));
}
else if (e.getActionCommand().equals("Divide")){
ansLabel.setText(""+(Double.parseDouble(num1Text.getText())/Double.parseDouble(num2Text.getText())));
}
}
You use
Integer.parseInt(num1Text.getText());
which parses a String to an int.
With
Double.parseDouble(num1Text.getText());
you get a double.
Note that you might want to catch the NumberFormatException, since you most likley don't want your program to stop on malformed user input:
double num1;
try
{
num1 = Double.parseDouble(num1Text.getText());
} catch (NumberFormatException e)
{
// either use a default value or give feedback to the user on what he did wrong
// for example
num1 = 1;
}
If you don't want to print a double value, if only integer values are entered by the user, then you could first try to parse to int, catch the exception (if any) and then parse to double.
Another error in your divison line:
ansLabel.setText(
""+(Integer.parseInt(num1Text.getText())/Integer.parseInt(num2Text.getText())));
You do not check if the second parameter is != 0, otherwise you might divide by 0!
To keep your code more readable you might want to create an extra method for parsing a String to a number:
private static double parseDouble(String s)
{
double num;
try
{
num = Double.parseDouble(num1Text.getText());
} catch (NumberFormatException e)
{
// either use a default value or give feedback to the user on what he did wrong
// for example
num = 1;
}
return num;
}
A possible fix for the division by 0 could be:
double num1 = parseDouble(num1Text.getText());
double num2 = parseDouble(num2Text.getText());
String command = e.getActionCommand();
if (command.equals("Sum"))
{
ansLabel.setText("" + (num1 + num2));
} else if (command.equals("Subtract"))
{
ansLabel.setText("" + (num1 - num2));
} else if (command.equals("Multiple"))
{
ansLabel.setText("" + (num1 * num2));
} else if (command.equals("Divide"))
{
if (num2 == 0)
{
ansLabel.setText("ERROR: Division by zero!");
} else
{
ansLabel.setText("" + (num1 / num2));
}
}
you should use double data type instead of integer for 4 operations
Like this:
public void actionPerformed( ActionEvent e )
{
if( e.getActionCommand().equals( "Sum" ) )
ansLabel.setText( "" + ( Double.
parseDouble(num1Text.getText() ) + Double.parseDouble(
num2Text.getText() ) ) );
// etc.
I think I almost got it if I can just get rid of a few exceptions the program gives me when I try to run it. Here's the code I have:
package RandomMathGame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class RandomMathGame {
public static void main(String[] args) {
RandomProblemGenerator randomProblems = new RandomProblemGenerator(10);
final int numberProblems = 10;
int correctScore = 0;
JPanel panel = new JPanel();
int answer;
int correctAnswer;
JLabel[] mathProblems = new JLabel[numberProblems];
final JTextField[] mathAnswers = new JTextField[numberProblems];
JLabel[] correctYesNo = new JLabel[numberProblems];
final JLabel score = new JLabel(correctScore + "/10");
JButton submit = new JButton("Submit");
for (int i = 1; i <= numberProblems; i++)
{
final int X = randomProblems.createNumberX();
final int Y = randomProblems.createNumberY();
mathProblems[i] = new JLabel("" + X + " * " + Y + " = ");
mathAnswers[i] = new JTextField();
answer = Integer.parseInt(mathAnswers[i].getText());
correctAnswer = X * Y;
if (answer == correctAnswer)
{
correctYesNo[i] = new JLabel("Correct answer; good job!");
correctScore = correctScore + 1;
}
else
{
correctYesNo[i] = new JLabel("Incorrect answer; try again!");
}
panel.add(mathProblems[i]);
panel.add(mathAnswers[i]);
panel.add(correctYesNo[i]);
}
final int temp = correctScore;
submit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
score.setText("Score: " + temp + "/10");
}
});
panel.add(submit);
panel.add(score);
JFrame gameFrame = new JFrame();
gameFrame.setTitle("Random Math Game");
gameFrame.setSize(150, 150);
gameFrame.setVisible(true);
gameFrame.setContentPane(panel);
}
}
And these are the exceptions it's giving me when I run it:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at RandomMathGame.RandomMathGame.main(RandomMathGame.java:33)
Java Result: 1
I'm crunched for time, so any help would be much appreciated.
You are trying to get from a field you just created which is blank.
mathAnswers[i] = new JTextField();
answer = Integer.parseInt(mathAnswers[i].getText());
So in this you are just checking from wrong place.
Seems like on first step you want to create the text fields and the second line should be there at some other event.
Hope it helps
String answerStr = mathAnswers[i].getText();
if(answerStr.isEmpty()){
correctYesNo[i] = new JLabel("Not a valid answer/answer field empty!");
} else {
answer = Integer.parseInt(answerStr);
correctAnswer = X * Y;
//Rest of your code goes here
}
Hope this helps you!
answer = Integer.parseInt(mathAnswers[i].getText());
Is what's causing the error. This is because one of your textfields is blank. Do some validation before accepting user input, unless you want your application to go awry.
I realize this is an unbelievably common issue and have looked very very thoroughly but have had no luck! It seems I am having an outOfBounds Exception issue. My code is as follows wuth errors just after! Thanks again :)
UPDATE:
Thanks for all of your many fast responses. Although it says there's an issue with Analysis Panel, I didn't know exactly if this was the cause as I have other classes which use it with no issues! But below is the other code. Thanks again!
public class AnalysisPanel extends JPanel {
private JTextArea overview_text = GuiComponentGenerator.getJTextArea("");
private JTextArea csv_text = GuiComponentGenerator.getJTextArea("");
private JComboBox analyser_choices;
private String[] analyser_class_names;
private LinkedHashMap<String, ImageAnalysis> analyser_outputs = new LinkedHashMap();
private JTextField[] weka_directory_texts;
private JTextField[] weka_tag_texts;
private JTextField weka_output_file_path_text = GuiComponentGenerator
.getJTextField("");
private JTextField weka_relation_text = GuiComponentGenerator
.getJTextField("");
public AnalysisPanel() {
GuiComponentGenerator.setLook(this);
analyser_class_names = ResourceAndClassDirectories
.getClassNamesInDirectory(ResourceAndClassDirectories.IMAGE_ANALYSERS_CLASS_STEM);
ArrayList<String> choices = new ArrayList(
Arrays.asList(analyser_class_names));
choices.add(0, "All");
analyser_choices = GuiComponentGenerator.getJComboBox(choices);
analyser_choices.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
updateTextBoxes();
}
});
setLayout(new BorderLayout());
add(BorderLayout.NORTH,
GuiComponentGenerator.getJPanel(analyser_choices));
add(BorderLayout.CENTER, getTextsPanel());
add(BorderLayout.SOUTH, getWekaPanel());
}
private JPanel getWekaPanel() {
JPanel weka_panel = GuiComponentGenerator.getJPanel(new BorderLayout());
JPanel details_panel = GuiComponentGenerator.getJPanel(new GridLayout(
6, 1));
JPanel top_panel = GuiComponentGenerator
.getJPanel(new GridLayout(1, 2));
JPanel left_top_panel = GuiComponentGenerator.getJPanel(new BorderLayout());
JPanel right_top_panel = GuiComponentGenerator.getJPanel(new BorderLayout());
left_top_panel.add(BorderLayout.WEST, GuiComponentGenerator.getJLabel("Relation:"));
left_top_panel.add(BorderLayout.CENTER, weka_relation_text);
right_top_panel.add(BorderLayout.WEST, GuiComponentGenerator.getJLabel("Output to:"));
right_top_panel.add(BorderLayout.CENTER, weka_output_file_path_text);
top_panel.add(left_top_panel);
top_panel.add(right_top_panel);
weka_panel.add(BorderLayout.NORTH, top_panel);
weka_directory_texts = new JTextField[5];
weka_tag_texts = new JTextField[5];
JPanel labels_panel = GuiComponentGenerator.getJPanel(new GridLayout(1,
2));
labels_panel.add(GuiComponentGenerator.getCentreAlignedJLabel("Tag"));
labels_panel.add(GuiComponentGenerator
.getCentreAlignedJLabel("Image directory"));
details_panel.add(labels_panel);
for (int pos = 0; pos < 5; pos++)
details_panel.add(getDetailsPanel(pos));
weka_panel.add(BorderLayout.NORTH, top_panel);
weka_panel.add(BorderLayout.CENTER, details_panel);
JPanel weka_bordered_panel = GuiComponentGenerator
.getLeftBorderedJPanel(weka_panel, "Weka");
return weka_bordered_panel;
}
private JPanel getDetailsPanel(int pos) {
JPanel details_panel = GuiComponentGenerator.getJPanel(new GridLayout(
1, 2));
final JTextField weka_directory_text = GuiComponentGenerator
.getJTextField("");
JTextField weka_tag_text = GuiComponentGenerator.getJTextField("");
weka_directory_texts[pos] = weka_directory_text;
weka_tag_texts[pos] = weka_tag_text;
JPanel right_panel = GuiComponentGenerator
.getJPanel(new BorderLayout());
right_panel.add(BorderLayout.CENTER, weka_directory_text);
JButton choose_directory_button = GuiComponentGenerator
.getJButton(GuiComponentGenerator.FILE_DIALOG_ICON);
right_panel.add(BorderLayout.EAST, choose_directory_button);
choose_directory_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
File directory = FileChooserDialog.getFile(
ResourceAndClassDirectories.SOURCE_DIRECTORY, "", true);
if (directory != null)
weka_directory_text.setText(directory.getPath());
}
});
details_panel.add(weka_tag_text);
details_panel.add(right_panel);
return details_panel;
}
public String[] getWekaRunDetails() {
String[] wrd = { weka_relation_text.getText(),
weka_output_file_path_text.getText() };
return wrd;
}
public ArrayList<String[]> getWekaImageDetails() {
ArrayList<String[]> image_details = new ArrayList();
for (int pos = 0; pos < 5; pos++) {
String[] details = { weka_directory_texts[pos].getText(),
weka_tag_texts[pos].getText() };
image_details.add(details);
}
return image_details;
}
private JPanel getTextsPanel() {
JPanel texts_panel = GuiComponentGenerator
.getJPanel(new BorderLayout());
csv_text.setRows(3);
csv_text.setFont(new Font("Courier", Font.PLAIN, 14));
texts_panel.add(BorderLayout.NORTH, GuiComponentGenerator
.getLeftBorderedJPanel(
GuiComponentGenerator.getJScrollPane(csv_text), "CSV"));
texts_panel.add(BorderLayout.CENTER, GuiComponentGenerator
.getLeftBorderedJPanel(
GuiComponentGenerator.getJScrollPane(overview_text),
"Overview"));
return texts_panel;
}
public String getChosenImageAnalyser() {
return (String) analyser_choices.getSelectedItem();
}
public void performAnalyses(final TaggedBufferedImage tbi) {
Thread thread = new Thread() {
public void run() {
analyser_outputs.clear();
String choice = (String) analyser_choices.getSelectedItem();
csv_text.setText("");
if (choice.equals("All")) {
for (String analyser_class_name : analyser_class_names)
performAnalysis(analyser_class_name, tbi);
} else
performAnalysis(choice, tbi);
updateTextBoxes();
}
};
thread.start();
}
private void updateTextBoxes() {
String overview = "";
String csv_headings = "|";
String csv_values = "|";
String choice = (String) analyser_choices.getSelectedItem();
for (String analyser_class_name : analyser_class_names) {
if (choice.equals("All") || choice.equals(analyser_class_name)) {
ImageAnalysis image_analysis = analyser_outputs
.get(analyser_class_name);
if (image_analysis != null) {
overview += analyser_class_name + "\n\n"
+ image_analysis.overview + "\n-----------------\n";
System.out.println("In Analysis Panel: image analsis csv headings " + image_analysis.csv_headings.size());
for (int pos = 0; pos < image_analysis.csv_headings.size(); pos++) {
String heading = image_analysis.csv_headings.get(pos);
String value = image_analysis.csv_values.get(pos);
while (heading.length() < value.length())
heading += " ";
while (value.length() < heading.length())
value += " ";
csv_values += value + "|";
csv_headings += heading + "|";
}
}
}
}
overview_text.setText(overview);
csv_text.setText(csv_headings + "\n" + csv_values);
}
private void performAnalysis(String analyser_class_name,
TaggedBufferedImage tbi) {
try {
overview_text.setText("Analysing " + analyser_class_name);
Class c = Class
.forName(ResourceAndClassDirectories.IMAGE_ANALYSERS_CLASS_STEM
+ "." + analyser_class_name);
ImageAnalyserInterface analyser = (ImageAnalyserInterface) c
.newInstance();
ImageAnalysis image_analysis = analyser.analyseImage(tbi);
analyser_outputs.put(analyser_class_name, image_analysis);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Update: Now working, although the issue was in the below, it needed to be changed in the Colour file itself!
Thanks all :)
If there are 6 elements in the array, then index 5 will be the final index, as it is zero indexed - that is, 0,1,2,3,4,5 is 6 elements
Well based on the title you are trying to access index 6 in an array of size 6. This is not possible since arrays in Java run from index 0 to size-1.
Your max possible index is therefore 5 in an array of size 6.