Hi, i am facing a problem on my java assignment recently which require me to read customer records from a text file and pass it into array. After that i need to change the first letter of the name to uppercase and sort it according to the name. However i could only get the data from the text file and not pass it into arraylist. Can anyone help pls? If possible can provide some example? thank in advance=D Below is an attachment of my code and the text file as well as my output.

---------------file.txt-------------

Account Id = 123
Name = Matt Damon
Address = 465 Ripley Boulevard, Oscar Mansion, Singapore 7666322
DOB = 10-10-1970
Phone Number = 790-3233
Account Balance = 405600.00

Account Id = 126
Name = Ben Affleck
Address = 200 Hunting Street, Singapore 784563
DOB = 25-10-1968
Phone Number = 432-4579
Account Balance = 530045.00

Account Id = 65
Name = Salma Hayek
Address = 45 Mexican Boulevard, Hotel California, Singapore 467822
DOB = 06-04-73
Phone Number = 790-0000
Account Balance = 2345.00

Account Id = 78
Name = Phua Chu Kang
Address = 50 PCK Avenue, Singapore 639798
DOB = 11-08-64
Phone Number = 345-6780
Account Balance = 0.00

Account Id = 234
Name = Zoe Tay
Address = 100 Blue Eyed St, Singapore 456872
DOB = 15-02-68
Phone Number = 456-1234
Account Balance = 600.00

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
 
public class Customer {
 
        public int acctId;
        public String name;
        public String address;
        public String dob;
        public String phone;
        public double accBal;
        static ArrayList custRec = new ArrayList();
      
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        
        Scanner input = new Scanner(System.in);
       
        String choice,value,fileName;
 
        do{
            menu();
            System.out.println("Please enter your choice: ");
            value = input.next();
            choice = value;
                    
           if(choice.equals("1")){
               System.out.println("Please enter name of input file: ");
               fileName = input.next();
               readFile(fileName);
           }else if(choice.equals("2")){
               System.out.println("choice 2");
           }else if(choice.equals("3")){
               System.out.println("choice 3");
           }else if(choice.equals("q")){
               System.out.println("exit");
           }
             
          }while(!choice.equals("q"));
     
       }
    
    public static void readFile(String fileName) throws IOException{
      
     BufferedReader br = null;
     String line;
     
     try {
            br = new BufferedReader(new FileReader(fileName));
            br.ready();
            
            while((line = br.readLine()) != null){
                
                StringTokenizer st = new StringTokenizer(line, "=");           
                 
                while(st.hasMoreTokens()){
                  
                     st.nextToken();
                     System.out.println(st.nextToken().trim());
                 }
                
            }
            
            br.close();
 
         } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
         }
        
    }
    
    public static void menu(){
 
        //Menu
        System.out.println("\n************Menu************");
        System.out.println("1) Input data");
        System.out.println("2) Display data");
        System.out.println("3) Output data");
        System.out.println("q) Quit\n");
 
    }
     
}

------------My output------------------

************Menu************
1) Input data
2) Display data
3) Output data
q) Quit

Please enter your choice:
1
Please enter name of input file:
customers.txt
123
Matt Damon
465 Ripley Boulevard, Oscar Mansion, Singapore 7666322
10-10-1970
790-3233
405600.00
126
Ben Affleck
200 Hunting Street, Singapore 784563
25-10-1968
432-4579
530045.00
65
Salma Hayek
45 Mexican Boulevard, Hotel California, Singapore 467822
06-04-73
790-0000
2345.00
78
Phua Chu Kang
50 PCK Avenue, Singapore 639798
11-08-64
345-6780
0.00
234
Zoe Tay
100 Blue Eyed St, Singapore 456872
15-02-68
456-1234
600.00

I made it a "small" changes to your coding. Now it is like this :)

Customer.java

public class Customer {
      
    public static void main(String[] args) {
        CustomerFunctions cf = new CustomerFunctions();
        cf.start();     
    }
}

CustomerFunctions.java

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Vector;
import java.util.Scanner;
import java.util.*;

public class CustomerFunctions {
	private CustomerData cData = new CustomerData();
	private Vector fileIn;
	private ArrayList<CustomerData> custRec = new ArrayList<CustomerData>();
	
    public CustomerFunctions() {}
    
    public void start(){    	
        Scanner input = new Scanner(System.in);
       
        String choice,value,fileName;
 
        do{
            menu();
            System.out.println("Please enter your choice: ");
            value = input.next();
            choice = value;
            
                    
           if(choice.equals("1")){
               System.out.println("Please enter name of input file: ");
               fileName = input.next();
               readFile(fileName);
               storeData(fileIn);
           }else if(choice.equals("2")){
               displayData();
           }else if(choice.equals("3")){
               System.out.println("choice 3");
           }else if(choice.equals("q")){
               System.out.println("exit");
           }
             
          }while(!choice.equals("q"));
     
       }

    
    public void readFile(String fileName){
      
     BufferedReader br = null;
     String line;
     fileIn = new Vector();
     
     try {
            br = new BufferedReader(new FileReader(fileName));
            br.ready();
            
            while((line = br.readLine()) != null){
            	if(!line.trim().equals(""))//to remove the empty line between clients data
            	{
            		fileIn.add(line);
            	}
            }
            
            br.close();
 
         }
         catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
         }
         catch(IOException ioe){}
    }
    
    public void storeData(Vector fileIn)
    {
    	cData = new CustomerData();
    	for ( int i = 0; i < fileIn.size(); i++ )
    	{
    		switch(i%6)
    		{
    			case 0:
    				cData.setAccountID(Integer.parseInt(getToken((String)fileIn.elementAt(i))));
    				break;
    			case 1:
    				cData.setName(getToken((String)fileIn.elementAt(i)));
    				break;
    			case 2:
    				cData.setAddress(getToken((String)fileIn.elementAt(i)));
    				break;
    			case 3:
    				cData.setDob(getToken((String)fileIn.elementAt(i)));
    				break;
    			case 4:
    				cData.setPhoneNumber(getToken((String)fileIn.elementAt(i)));    				
    				break;
    			case 5:
    				cData.setBalance(Double.parseDouble(getToken((String)fileIn.elementAt(i))));
    				custRec.add(cData);
    				cData = new CustomerData();
    				break;
    			default:
    				break;
    		}      		  				
    	}    		
    }
    
    public void menu(){ 
        //Menu
        System.out.println("\n************Menu************");
        System.out.println("1) Input data");
        System.out.println("2) Display data");
        System.out.println("3) Output data");
        System.out.println("q) Quit\n"); 
    }
    
    public String getToken(String str)
    {
    	int charPos = str.indexOf("=");
    	String data = str.substring(charPos+1).trim();
    	return data;
    }
    
    public void displayData()
    {
    	System.out.println(); // just empty line to separate user choice from array list print
    	for(int i = 0; i < custRec.size(); i++)
    	{
    		cData = (CustomerData) custRec.get(i);
    		System.out.println("Account id : " + cData.getAccountID());
    		System.out.println("Name : " + cData.getName());
    		System.out.println("Address : " + cData.getAddress());
    		System.out.println("DOB : " + cData.getDob());
    		System.out.println("Phone number : " + cData.getPhoneNumber());
    		System.out.println("Account balance : $" + cData.getBalance() + "\n");
    	}
    }
    	
}

CustomerData.java

public class CustomerData {
	
	private int accountID;
	private String name;
	private String address;
	private String dob;
	private String phoneNumber;
	private double balance;
	

    public CustomerData() {}
    
    public void setAccountID(int i){ accountID = i;}
    public int getAccountID() { return accountID;}
    
    public void setName(String str){ name = str;}
    public String getName() {return name;}
    
    public void setAddress(String str) { address = str;}
    public String getAddress() {return address;}
    
    public void setDob(String str){ dob = str;}
    public String getDob() {return dob;}
    
    public void setPhoneNumber(String str) { phoneNumber = str;}
    public String getPhoneNumber() { return phoneNumber;}
    
    public void setBalance(double d) { balance = d;}
    public double getBalance() { return balance;}
    
}

If you not sure about what is going on in the code just ask...

Thank for your reply i really appreciate but my assignment it require me to use array but for your code it is using vector. Is vector an array? And another requirement is i need to declare a customer class that represent each customer record with following attributes int acctId, string name,address,dateOfBirth ,phoneNum and double acctBal. May i know for the switch why you need to use this "i%6"? What does it mean?

Thank for your reply i really appreciate but my assignment it require me to use array but for your code it is using vector. Is vector an array? And another requirement is i need to declare a customer class that represent each customer record with following attributes int acctId, string name,address,dateOfBirth ,phoneNum and double acctBal. May i know for the switch why you need to use this "i%6"? What does it mean?

Has to be ArrayList in whole assignment? If so, it is not difficult to Vector for ArrayList, besides you use array list to store customer date (check out method storeData() in CustomerFunctions.java). The customer class to store data is CustomerData.java. You are are free to swap variable names as you need.

"i%6" modulus or often called remainder of mathematical operation. My reader function omits the empty line between each account record. So the array list data after reading text file will be stored as follows
element 0 - account ID
element 1 - name
element 2 - address
element 3 - DOB
element 4 - phone Number
element 5 - balance
element 6 - account ID
element 7 - name
element 8 - address
element 9 - DOB
etc.
So if i = 7, i%6 = 1 and switch statement will store it in name of the customer

Oh nope. Can i ask you why you need to code the arraylist in this way ArrayList<CustomerData> custRec = new ArrayList<CustomerData>();? What does "<CustomerData>" do? And what is vector? What can it do? For my readFile method i need to return the number of records successfully read may i know whether i can write my code in this way? But if i write this way i will encounter error when i parse the line to the arraylist, may i know why?

public static ArrayList readFile(String fileName) throws IOException{
      
     BufferedReader br = null;
     String line;
     fileIn = new Vector();
     ArrayList<Customer> custRec = new ArrayList<Customer>();
     
     try {
            br = new BufferedReader(new FileReader(fileName));
            line = null;

            while((line = br.readLine()) != null){
                if(!line.trim().equals("")){
//                StringTokenizer st = new StringTokenizer(line, "=");           
//                
//                while(st.hasMoreTokens()){ 
//                    
//                    st.nextToken();  
                     custRec.add(line);
//
//                }
            
                }
            }
            
            //Display arraylist
            for(int i = 0; i < custRec.size();i++){
               System.out.println("Index " + i + ": " +custRec.get(i));
            }

            Collections.sort(custRec);
                 
            br.close();       
         } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
         }
        return custRec;
    }

Why you trying to put everything back to Customer file? Also it is better to catch exception just randomly trow it.

The ArrayList<CustomerData> custRec = new ArrayList<CustomerData>(); say I will be using ArrayList that will store objects of CustomerData type.
If you say only ArrayList custRec = new ArrayList(); will work with general Object type.
To get number of records use custRec.size() .
The record sorting will not work this way Collections.sort(custRec); you need to use bubble sort or implementation of other sorting algorithm

how will i parse this code that is saved from a txtfile to get its frequency count and big oh notation
k=500;
for (i=1;i<=k;i++)
x=x+1;
n=200;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.