Results 1 to 5 of 5
Thread: Txt to ArrayList only prints []
- 04-27-2011, 10:28 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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.
The frame code is here: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"); } }
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; } }
- 04-27-2011, 11:15 AM #2
- 04-27-2011, 11:18 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-27-2011, 06:57 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
My text file looks like this:
And so on and so forth. It also contains incorrect entires like MAT with a concentration or ACO without, etc.Java Code:Trump, Donald, 1.2, MAT Skywalker, Luke, 3.8, ACO, Database Jones, Indiana, 4.2, ACO, Network
I commented out in.useDelimiter and made the arg.length >= 2 but now I don't get any results at all?
- 04-27-2011, 07:16 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Canvas Prints
By canvas02 in forum Reviews / AdvertisingReplies: 0Last Post: 02-18-2011, 05:33 PM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
List prints only the first row repeatedly in a loop
By yss1287b in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 11-16-2010, 02:21 PM -
Count each cell as it prints.
By Shyamz1 in forum New To JavaReplies: 9Last Post: 10-29-2010, 12:58 AM -
Doclet that prints out all members of the class
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks