Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with adding and sorting of arraylist

807589Aug 31 2008 — edited Aug 31 2008
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 28 2008
Added on Aug 31 2008
6 comments
204 views