Question

I'm having trouble with a program that unexpectedly closes after a file is loaded. The program is meant to open a file, announce the errors, and display all the valid numbers.

Here's the main code:

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.*;

public class Project3 {

    static TemperatureGUI myGUI;
    static TemperatureList myList = new TemperatureList();
    static Temperature[] myArray = new Temperature[100];
    static int count = 0;

    public static void main(String[] args) {
        myGUI = new TemperatureGUI();
        myGUI.setVisible(true);
    }//main

    public static boolean isValid(String x) {
        Pattern p;
        Matcher m;
        String pattern = "^-?\\d{1,3}(\\.\\d{1,2})?$";

        p = Pattern.compile(pattern);
        m = p.matcher(x);

        return m.matches();
    }//isValid


    public static void addMore(Temperature x) {
        myList.insert(x); // add new temperature to the list
        myArray[count++] = x;

        SelectionSort newSorted = new SelectionSort(myArray, count);

        myGUI.clear();

        for (int i = 0; i < count; i++) {
            myGUI.appendList(myList.getList(i).toString());
            myGUI.appendArray(newSorted.get(i).toString());

        }

    }//AddMore

    public static void handleFile(File file) {
        TextFileInput in = new TextFileInput(file.getAbsolutePath());

        String line = in.readLine();

        while (line != null) {
            //Need to Convert the String to Temperature
            //Before being able to put it into the Array and List
            try {
                if (!isValid(line))
                    throw new IllegalTemperatureException(line);

                float parsedTemp = Float.parseFloat(line);

                Temperature temp = new Temperature(parsedTemp);

                myArray[count++] = temp;
                myList.insert(temp);
            } catch (IllegalTemperatureException e) {
                JOptionPane.showMessageDialog(null, line + " is not a valid temperature.");
            } finally {
                line = in.readLine();
            }

        }//while


        SelectionSort sortedArray = new SelectionSort(myArray, count);


        //Add it to GUI
        for (int i = 0; i < count; i++) {
            myGUI.appendArray(sortedArray.get(i).toString());
            myGUI.appendList(myList.getList(i).toString());
        }


        for (int i = 0; i < count; i++) {

            System.out.println(myList.getList(i));
        }

    }
}//class

FileMenuHandler.java:

import javax.swing.*;

import java.awt.event.*;
import java.io.File;
import java.util.regex.*;


public class FileMenuHandler implements ActionListener {
    JFrame jframe;
    JFileChooser chooser = new JFileChooser();
    Temperature temp; //new input from "Add"

    /**
     *
     * @param jf Which Jframe the the FileMenuHandler should be on
     */
    public FileMenuHandler(JFrame jf) {
        jframe = jf;
    }//constructor

    /**
     * Checks which item was clicked and act according to to that event
     */
    public void actionPerformed(ActionEvent event) {
        String menuName = event.getActionCommand();

        if (menuName.equals("Open")) {
            int val = chooser.showOpenDialog(null);
            if (val == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                Project3.handleFile(file);
            } else {
                JOptionPane.showMessageDialog(null, "Goodbye, forever! Don't forget the cake!");
            }
        } else if (menuName.equals("Add")) {
            while (true) {
                String input = null;
                //Make sure the input value is valid.
                try {
                    input = JOptionPane.showInputDialog(null, "Input a New Temperature.");

                    if (!isValid(input))
                        throw new IllegalTemperatureException(input);

                    temp = new Temperature(Float.parseFloat(input));
                }

                catch (IllegalTemperatureException ite) {
                    JOptionPane.showMessageDialog(null, input + " is an invalid temperature.");
                }

                //If the valid, then pass the temp value to Project 3 and call on the "addMore" method. 
                Project3.addMore(temp); //add more to the GUI

            }//while
        }//Add


        else if (menuName.equals("Quit"));
        System.exit(0);
    }//event---

    //Make sure the value is okay using Regular Expressions
    public static boolean isValid(String x) {
        Pattern p;
        Matcher m;

        String pattern = "^-?\\d{1,3}(\\.\\d{1,2})?$";

        p = Pattern.compile(pattern);
        m = p.matcher(x);

        return m.matches();
    }//isValid
}//Handler

The error comes in Project3.handleFile where the errors would be displayed in messages, but the program wouldn't show the numbers on the window. It would continue to print to the console. In the console, only "Exception while removing reference." is displayed without anything else saying what the problem could be. Nowhere in my code is this shown. Why is the program terminating after displaying the errors and not showing the numbers onto the window?

EDIT: This is the TemperatureGUI

import java.awt.GridLayout;

import javax.swing.; import java.awt.;

public class TemperatureGUI extends JFrame{

JTextArea txtArray = new JTextArea();
JTextArea txtList = new JTextArea();

JMenuBar menuBar;
JMenuItem item;


public TemperatureGUI(){
    //Set Up the Display
    setTitle("Sorted Numbers");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(200, 444);
    setLayout(new GridLayout(1, 2));
    setResizable(false);
    setLocation(200,200);

    createFileMenu();   

    txtArray.setEditable(false);
    txtList.setEditable(false);

    JScrollPane scrollArray = new JScrollPane(txtArray);
    getContentPane().add(scrollArray);

    JScrollPane scrollList = new JScrollPane(txtList);
    getContentPane().add(scrollList);

    //setVisible(true);


    setResizable(false);
}//create GUI   



public void createFileMenu(){
    menuBar  = new JMenuBar();
    JMenu       fileMenu = new JMenu("File");
    FileMenuHandler fmh  = new FileMenuHandler(this);

    item = new JMenuItem("Open");  
    item.addActionListener( fmh );
    fileMenu.add( item );

    fileMenu.addSeparator();          

    item = new JMenuItem("Add");     
    item.addActionListener( fmh );
    fileMenu.add( item );

    setJMenuBar(menuBar);
    menuBar.add(fileMenu);

    fileMenu.addSeparator();

    item = new JMenuItem("Quit");
    item.addActionListener(fmh);
    fileMenu.add(item);

}//create Menu

//Separate Methods for Adding New Values
public void appendArray(String tempArray){
    txtArray.append(tempArray+"\n");
}

public void appendList(String tempList){
    txtList.append(tempList +"\n");
}



//Clean out the GUI for updated list
public void clear(){
    txtArray.setText(null);
    txtList.setText(null);
}

   }//TemperatureGUI

As for the input. My professor provided us a TextFileInput class which is to be used to retrieve data, line by line, from a text file. I've tried things such as the break points, but it doesn't seem to be helping.

Was it helpful?

Solution

There's a semicolon in an unusual place in this bit of the code:

    else if (menuName.equals("Quit"));
    System.exit(0);

The first semicolon ends the if statement, so System.exit(0); on the next line gets called regardless of what happened earlier.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top