Results 1 to 8 of 8
- 11-27-2014, 03:13 AM #1
Member
- Join Date
- Nov 2014
- Posts
- 4
- Rep Power
- 0
Collection.sort when getting array values from a text file
Hello all,
I'm currently a Java student. I working on a bank program that gets a user id, name and balance from a text file. I need to be able to sort based on each field. I'm using Collection.sort and Comparator. I'm using String[] array = file.toFile().list(); to get the values for the array from the text file. I can't figure out how to make this into an arraylist. I understand the concept when hardcoding the data but having trouble doing it from a text file.
- 11-27-2014, 04:37 AM #2
Re: Collection.sort when getting array values from a text file
how to make this into an arraylist.
Also the Arrays class has a useful method.
Post the code you are having problems with. Be sure to wrap the code in code tags.Last edited by Norm; 11-27-2014 at 04:42 AM.
If you don't understand my response, don't ignore it, ask a question.
- 11-27-2014, 05:16 AM #3
Member
- Join Date
- Nov 2014
- Posts
- 4
- Rep Power
- 0
Re: Collection.sort when getting array values from a text file
Here's what I have so far. Thanks in advanced for any help.
Java Code:public class BankSorter extends JFrame { private static final String String = null; private static JTextArea area = new JTextArea(8 , 35); private JLabel totLabel = new JLabel(" Total # of Bank Accounts: "); private static JTextField totacct = new JTextField(5); public BankSorter() { super("Account Sorter"); acct = new JLabel(" Bank Account Sorter"); acct.setFont(new Font("Ariel", Font.BOLD, 26)); FlowLayout flow = new FlowLayout(); setLayout(flow); add(acct); JCheckBox nameBox = new JCheckBox(); JLabel nameLabel = new JLabel("Name:"); JCheckBox custBox = new JCheckBox(); JLabel custLabel = new JLabel("Customer #:"); JCheckBox balanceBox = new JCheckBox(); JLabel balanceLabel = new JLabel("Balance:"); JLabel sortLabel = new JLabel("Sort by "); add(acct); add(area); add(new JScrollPane(area)); add(sortLabel); add(nameLabel); add(nameBox); add(custLabel); add(custBox); add(balanceLabel); add(balanceBox); add(totLabel); add(totacct); nameBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(nameBox.isSelected()) { balanceBox.setSelected(false); custBox.setSelected(false); } } } ); custBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(custBox.isSelected()) { balanceBox.setSelected(false); nameBox.setSelected(false); } } } ); balanceBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(balanceBox.isSelected()) { nameBox.setSelected(false); custBox.setSelected(false); } } } ); } public class Sort { private String name; private double balance; private int idString; public void Name(String n, double b, int i) { name = n; balance = b; idString = i; } public String getName() { return name; } public double getBalance() { return balance; } public int getIdString() { return idString; } public String toString() { return idString + name + balance; } } public static void BankFiles() { Path file = Paths.get("C:\\Java\\Account.txt"); final String EMPTY_ACCT = "0000"; final String ID_FORMAT = "0000"; final String NAME_FORMAT = " "; final String BALANCE_FORMAT = "00000.00"; String temp = ""; String delimiter = ","; String s = ID_FORMAT + delimiter + NAME_FORMAT + delimiter + BALANCE_FORMAT + System.getProperty("line.separator"); String[] array = file.toFile().list(); try { InputStream iStream = new BufferedInputStream(Files.newInputStream(file)); BufferedReader reader = new BufferedReader(new InputStreamReader(iStream)); while(s != null) { array = s.split(delimiter); if(!array[0].equals(EMPTY_ACCT)) { area.append("Customer #: " + array[0] + " " + " Name: " + array[1] + " " + "Balance: " + " $" + array[2] + System.getProperty("line.separator")); } s = reader.readLine(); } reader.close(); } catch(Exception e) { System.out.println("Message: " + e); } totacct.setText(temp); } Collections.sort(array, new Comparator<>() { public int compare(array a1, array a2){ if(a1.getId() > a2.getId()) { return 1; } else if(a1.getId() < a2.getId()) { return -1; } return 0; } }); } public static void main(String[] args) { BankSorter frame = new BankSorter(); frame.setTitle("BankSorter"); frame.setSize(450, 325); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); BankFiles(); } }
Last edited by NewbieGramer; 11-27-2014 at 05:20 AM.
- 11-27-2014, 05:32 AM #4
Re: Collection.sort when getting array values from a text file
Do you have any specific questions?
If you don't understand my response, don't ignore it, ask a question.
- 11-27-2014, 05:42 AM #5
Member
- Join Date
- Nov 2014
- Posts
- 4
- Rep Power
- 0
Re: Collection.sort when getting array values from a text file
Yes,
How to make an array list for the balance,name and id from the text file. So, I can complete the collection.sort and compareTo methods.
- 11-27-2014, 10:26 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: Collection.sort when getting array values from a text file
First thing I see is:
Java Code:String[] array = file.toFile().list();
Not that it matters, as you never use the returned results anyway.
For your actual question, write a class that represents those values and build an ArrayList<BankAccount>.
That's the first step.Please do not ask for code as refusal often offends.
** This space for rent **
- 11-29-2014, 01:40 AM #7
Member
- Join Date
- Nov 2014
- Posts
- 4
- Rep Power
- 0
Re: Collection.sort when getting array values from a text file
Java Code:public class Record { String name; double balance; int idString; public Record(int idString, double balance, String name) { this.idString = idString; this.balance = balance; this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public int getIdString() { return idString; } public void setIdString(int idString) { this.idString = idString; } }
- 12-01-2014, 02:43 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Similar Threads
-
Text file to Array working values
By kraster in forum New To JavaReplies: 10Last Post: 06-24-2012, 06:34 PM -
Error when doing bubble sort using a text file
By welshcrossy in forum New To JavaReplies: 12Last Post: 04-14-2012, 09:16 PM -
Read text file and sort in ArrayList
By Tabula Rasa in forum New To JavaReplies: 7Last Post: 04-17-2011, 05:08 AM -
need to input values from a text file into an array and count values
By pds8475 in forum New To JavaReplies: 14Last Post: 01-22-2011, 03:36 PM -
Write a program that sorts data from a text file and sort them in a file
By danmgz45 in forum New To JavaReplies: 6Last Post: 12-01-2010, 06:31 AM
Bookmarks