Results 1 to 8 of 8
  1. #1
    NewbieGramer is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Collection.sort when getting array values from a text file

    how to make this into an arraylist.
    One way is to have a loop that accesses each element in the array and adds it to the 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.

  3. #3
    NewbieGramer is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default 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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.

  5. #5
    NewbieGramer is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default 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.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Collection.sort when getting array values from a text file

    First thing I see is:
    Java Code:
    String[] array = file.toFile().list();
    That simply lists the files in a directory, or null if it's not a directory.

    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 **

  7. #7
    NewbieGramer is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Re: Collection.sort when getting array values from a text file

    Quote Originally Posted by Tolls View Post
    First thing I see is:
    Java Code:
    String[] array = file.toFile().list();
    That simply lists the files in a directory, or null if it's not a directory.

    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.
    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;
    		}
    }
    Is this what you meant?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Collection.sort when getting array values from a text file

    Something like that, though I'd name it what it is.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Text file to Array working values
    By kraster in forum New To Java
    Replies: 10
    Last Post: 06-24-2012, 06:34 PM
  2. Error when doing bubble sort using a text file
    By welshcrossy in forum New To Java
    Replies: 12
    Last Post: 04-14-2012, 09:16 PM
  3. Read text file and sort in ArrayList
    By Tabula Rasa in forum New To Java
    Replies: 7
    Last Post: 04-17-2011, 05:08 AM
  4. Replies: 14
    Last Post: 01-22-2011, 03:36 PM
  5. Replies: 6
    Last Post: 12-01-2010, 06:31 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
  •