Results 1 to 5 of 5
  1. #1
    Quasi Urbane is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Txt to ArrayList only prints []

    Hi I'm new to the forums and I'm having a bit of trouble get the array I created to print correctly. It's supposed to take in the lastName, firstName, gpa, major *and on occassion* concentration. However this array ultimately just displays [] in the resultsArea. Any ideas as to what I'm doing wrong? Any help is greatly appreciated.

    Java Code:
    import javax.swing.JFrame;
    import java.io.FileNotFoundException;
    import java.io.File;
    import java.io.PrintWriter;
    import java.util.*;
    
    public class StudentViewer
    {
        public static void main(String[] args) throws FileNotFoundException, NoSuchElementException
        {
            File inputFile;
            File outputFile;
    
            if (args.length >= 1)
            {
                inputFile = new File(args[0]);
                outputFile = new File(args[1]);
                PrintWriter out = new PrintWriter(outputFile);
                int lineNumber = 1;
    
                Scanner in = new Scanner(inputFile);
                in.useDelimiter("line.seperator");
    
                ArrayList<AppliedStudent> names = new ArrayList<AppliedStudent>();
                while(in.hasNext())
                    try
                    {
                        String line = in.next();
                        Scanner lineScanner = new Scanner(line);
                        lineScanner.useDelimiter("\\s*,\\s*");
    
                        String lastName = lineScanner.next();
                        String firstName = lineScanner.next();
                        double gpa = lineScanner.nextDouble();
                        String major = lineScanner.next();
    
                        if(major.equals("MAT"))
                        {
                            names.add(new AppliedMath(lastName, firstName, gpa, major));
                        }
                        if(major.equals("ACO"))
                        {
                            String concentration = lineScanner.next();
                            if(concentration.equals("Database") || concentration.equals("Network") || concentration.equals("DigitalMedia"))
                                names.add(new AppliedComputing(lastName, firstName, gpa, major, concentration));
                        }
                        lineNumber++;
    
                        System.out.println(names); // Trying to test the results, however only [] prints
                    }
                    catch (InputMismatchException exception)
                    {
                        out.println("/* " + lineNumber + " */ " + "This GPA is not a numerical value");
                    }
                    catch (NoSuchElementException exception)
                    {
                        out.println("/* " + lineNumber + " */ " + "No such element exists");
                    }
                Collections.sort(names);
    
                in.close();
                out.close();
    
                JFrame frame = new StudentFrame(names);
                frame.setTitle("Applied MNS Students");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
            else
                throw new FileNotFoundException("File Not Found");
        }
    }
    The frame code is here:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import javax.swing.border.*;
    
    
    
    public class StudentFrame extends JFrame
    {
        private ArrayList<AppliedStudent> student;
        private JCheckBox compBox;
        private JCheckBox mathBox;
        private JButton displayButton;
        private JLabel gpaLabel;
        private JTextField gpaField;
        private JButton searchButton;
        private JLabel resultLabel;
        private JTextArea resultArea;
    
    
        private static final int FRAME_WIDTH = 440;
        private static final int FRAME_HEIGHT = 450;
    
    
        public StudentFrame(ArrayList<AppliedStudent> names) throws FileNotFoundException
        {
            student = names;
    
            add(createCheckBoxPanel(), BorderLayout.NORTH);
            add(createDisplayPanel(),BorderLayout.CENTER);
            add(createResultPanel(), BorderLayout.SOUTH);
    
            setSize(FRAME_WIDTH, FRAME_HEIGHT);
        }
    
        public JPanel createCheckBoxPanel()
        {
            JPanel checkBoxPanel = new JPanel();
            compBox = new JCheckBox("Applied Computing");
            mathBox = new JCheckBox("Applied Math");
            displayButton = new JButton("Display");
            checkBoxPanel.add(compBox);
            checkBoxPanel.add(mathBox);
            checkBoxPanel.add(displayButton);
    
            return checkBoxPanel;
        }
    
    
    
        public JPanel createDisplayPanel()
        {
            final int FIELD_WIDTH = 10;
            JPanel displayPanel = new JPanel();
            gpaLabel = new JLabel("GPA: ");
            searchButton = new JButton("GPA Search");
            gpaField = new JTextField(FIELD_WIDTH);
            displayPanel.add(gpaLabel);
            displayPanel.add(gpaField);
            displayPanel.add(searchButton);
    
    
            class DisplayListener implements ActionListener
            {
                public void actionPerformed(ActionEvent event)
                {
                    resultArea.setText("");
                    for(int i = 0; i < student.size(); i++)
                    {
                        if(compBox.isSelected() && student.get(i) instanceof AppliedComputing)
                        {
                            resultArea.append(student.get(i).toString() + "\n");
                        }
                        if(mathBox.isSelected() && student.get(i) instanceof AppliedMath)
                        {
                            resultArea.append(student.get(i).toString() + "\n");
                        }
                        if(compBox.isSelected() && mathBox.isSelected() && student.get(i) instanceof AppliedComputing && student.get(i)instanceof AppliedMath)
                        {
                            resultArea.append(student.get(i).toString() + "\n");
                        }
                    }
    
                }
            }
            ActionListener listener = new DisplayListener();
            displayButton.addActionListener(listener);
    
            class SearchListener implements ActionListener
            {
                public void actionPerformed(ActionEvent event)
                {
                    resultArea.setText("");
    
                    try
                    {
                        double mean = Double.parseDouble(gpaField.getText());
                        for(int i = 0; i < student.size(); i++)
    
                        if (mean <= student.get(i).getGPA())
                        {
                            resultArea.append(student.get(i).toString() + "\n");
                        }
    
                        if (mean > 4.3)
                        {
                            gpaField.setText("");
                            resultArea.setText("");
                            JOptionPane.showMessageDialog(null, "Please enter a GPA between 0.0 and 4.3");
                        }
                        if (mean < 0.0)
                        {
                            gpaField.setText("");
                            resultArea.setText("");
                            JOptionPane.showMessageDialog(null, "Please enter a GPA between 0.0 and 4.3");
                        }
    
                    }
                    catch (NumberFormatException exception)
                    {
                        gpaField.setText("");
                        JOptionPane.showMessageDialog(null, "Please enter a numerical value");
                    }
                }
            }
            ActionListener listen = new SearchListener();
            searchButton.addActionListener(listen);
    
            return displayPanel;
        }
    
    
    
        public JPanel createResultPanel()
        {
            JPanel resultPanel = new JPanel();
    
            resultArea = new JTextArea(15, 35);
            JScrollPane pane = new JScrollPane(resultArea);
            resultPanel.add(pane);
            resultPanel.setBorder(new TitledBorder(new EtchedBorder(), "Results"));
    
            resultArea.setEditable(false);
    
            return resultPanel;
        }
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Quasi Urbane View Post
    Hi I'm new to the forums and I'm having a bit of trouble get the array I created to print correctly. It's supposed to take in the lastName, firstName, gpa, major *and on occassion* concentration. However this array ultimately just displays [] in the resultsArea. Any ideas as to what I'm doing wrong? Any help is greatly appreciated.

    how does your inputFile looks like? Can you please post it with some test data in it?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Quasi Urbane View Post
    Java Code:
                in.useDelimiter("line.seperator");
    What do you think this does? Do you want to use the text "line.delimeter" as an actual delimeter? (the dot is 'any char'). b.t.w. check whether or not the length of args is at least 2.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Quasi Urbane is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    My text file looks like this:

    Java Code:
    Trump, Donald, 1.2, MAT
    Skywalker, Luke, 3.8, ACO, Database
    Jones, Indiana, 4.2, ACO, Network
    And so on and so forth. It also contains incorrect entires like MAT with a concentration or ACO without, etc.

    I commented out in.useDelimiter and made the arg.length >= 2 but now I don't get any results at all?

  5. #5
    Quasi Urbane is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    I just figured it out! I just had to change

    Java Code:
    String line = in.next();
    to

    Java Code:
    String line = in.nextLine();
    and now it works. Thank you guys so much!

Similar Threads

  1. Canvas Prints
    By canvas02 in forum Reviews / Advertising
    Replies: 0
    Last Post: 02-18-2011, 06:33 PM
  2. Replies: 7
    Last Post: 11-22-2010, 02:26 PM
  3. List prints only the first row repeatedly in a loop
    By yss1287b in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 11-16-2010, 03:21 PM
  4. Count each cell as it prints.
    By Shyamz1 in forum New To Java
    Replies: 9
    Last Post: 10-29-2010, 01:58 AM
  5. Doclet that prints out all members of the class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 12:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •