1 Replies - 1622 Views - Last Post: 27 February 2010 - 10:13 AM Rate Topic: -----

#1 Jayp806  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 26-October 09

Saving and reading to Text file

Posted 27 February 2010 - 09:58 AM

Alright, for a school project I'm supposed to use a customer database .txt file provided by the instructor and create a few classes to utilize reading and writing features. I'm supposed to create a data array of CustomerRecords objects via CustomerList, add the information to the data array, which in turn will add it to the text file. I'm also supposed to be able to scan the database for a user Id which will in turn print the users info on the screen, and another fuction that will save the text file and close the operation. I haven't done the last two parts yet because I can't scan the data array for a user Id since I'm having problems adding the information to the array, and there's no info to save if it isn't in the array. In a nutshell, I'm having trouble transferring information from a text file, saving it into an object called a CustomerRecord, then in turn saving the CustomerRecord into the CustomerList. Finally, When you type "q" into the scanner, it saves all customer information to the text file and closes the program. I'm not getting any compile errors, but it still isn't working correctly. Can anyone help?

The main problem is just getting my Driver class to enter the Customer Records into the customer list.

(Also, would closing the data stream automatically save the info to the text file?)

Here's the 3 classes. (Last one isn't finished yet




public class CustomerRecords {

//Unique number assigned to each Customer

private int customerNumber = 0;

//Customer first name

private String firstName;

//Customer last name

private String lastName;

//Customer balance
private double balance =0;


public CustomerRecords(int newNum,String newFirst, String newLast,double newBalance){

	firstName = newFirst;

	lastName = newLast;

	customerNumber = newNum;

        balance = newBalance;
}

public String getLast(){

	return lastName;
}

public String getFirst(){

	return firstName;
}

public int getNumber(){

	return customerNumber;
}

public double getBalance(){
    return balance;
}

public void setLast(String newLast){

	lastName = newLast;
}

public void setFirst(String newFirst){

	firstName = newFirst;

}

public void setNumber(int newNum){

	customerNumber = newNum;
}

public void setBalance(double newBalance){

    balance = newBalance;
}

public String customerString(){
    return customerNumber + " " +firstName + " " + lastName + " " + balance;
}
}





import java.io.*;
import java.util.Scanner;

public class CustomerList{

//Number of customers in the list

	private int count = 0;

 //array of CustomerRecords objects

	private CustomerRecords[] data;


public void CustomerList(File fileName){

  //Create a file instance
       

        //Create a Scanner for the file
       try{
        Scanner input = new Scanner(fileName);

        //Read data from a file

        while(input.hasNext()){
        int idNum = input.nextInt();
        String first = input.next();
        String last = input.next();
        double balance = input.nextDouble();

        CustomerRecords CR1 = new CustomerRecords(idNum,first,last,balance);

        }
       }

         catch (IOException e){
             System.out.print("Error, file could not be opened. Exception:" +e);
             System.exit(-1);
       }


    }




public void enterCustomerRecords(CustomerRecords newRecord){

data[count] = newRecord;

count++;

}

public void getCustomer(int key){

     for (int i = 0; i< count; i++){
         int newNum =data[i].getNumber();
         if (newNum == (key)){
            System.out.println(data[i].getNumber() + " " +
                               data[i].getFirst() + " " + data[i].getLast()+
                               " " + data[i].getBalance());

         }
         else
        System.out.print("Customer number does not exist. Please try again");

}

}
}



import java.io.*;
import java.util.Scanner;

public class CustomerListDriver {

    public static void main(String[] args) throws Exception {

        File file = new File("database.txt");
        CustomerList CR2 = new CustomerList(file);

  Scanner input = new Scanner(System.in);
	while(true){
    System.out.print("\nCommand list:\n");
    System.out.print("a - add customer information to database\n");
    System.out.print("f - use customer ID number to find customer\n");
    System.out.print("q - update the customer list and terminate the program\n");
    System.out.print("Enter command: \n");
    String cmnd = input.next();

    if( cmnd.equalsIgnoreCase("a")){
        System.out.print("Enter 5 digit identification number: ");
        int idNum = input.nextInt();
        System.out.print("Enter first name: ");
        String first = input.next();
        System.out.print("Enter last name: ");
        String last = input.next();
        System.out.print("Enter customer balance: ");
        double balance = input.nextDouble();

       CustomerRecords CR1 = new CustomerRecords(idNum,first,last,balance);

       CR2.enterCustomerRecords(CR1);

      

       PrintWriter output = new PrintWriter(file);

       output.print(idNum + " " + first + " " + last + " " + balance);




    }
            
   

    }

    }

}



Thanks for any help or suggestions you can provide guys. :)


The text file provided by the teacher contains the following information:

12345 Jacky Smith 123.22
11111 Sarah Smith 45.89
22222 Sue Johnson 7765.98
33333 Billy Hunts 374.99



Is This A Good Question/Topic? 0
  • +

Replies To: Saving and reading to Text file

#2 javabie  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 117
  • Joined: 12-February 10

Re: Saving and reading to Text file

Posted 27 February 2010 - 10:13 AM

private CustomerRecords[] data;
public void CustomerList(File fileName){
        //Create a Scanner for the file
       try{
        Scanner input = new Scanner(fileName);
        //Read data from a file
        while(input.hasNext()){
        int idNum = input.nextInt();
        String first = input.next();
        String last = input.next();
        double balance = input.nextDouble();
        CustomerRecords CR1 = new CustomerRecords(idNum,first,last,balance);
        }
       }




you are reading it from the file..... and creating a CR1 object.. but you have a array called data of type CustomerRecords...
see that CR1 and data are 2 different variables and they don't have any connection between them...

you might do sumthing like this...
one thing is that array is kind of a bad idea for this problem... since you don't how many item will be in the file, you should create a ArrayList and add it.

import java.util.*;

public static ArrayList<CustomerRecords> data;

public void CustomerList(File fileName){
        //Create a Scanner for the file
       data = new ArrayList<CustomerRecords>();
       try{
        int i = 0;        
 
        Scanner input = new Scanner(fileName);
        //Read data from a file
        while(input.hasNext()){
        int idNum = input.nextInt();
        String first = input.next();
        String last = input.next();
        double balance = input.nextDouble();
        data.add(new CustomerRecords(idNum,first,last,balance));
        }
       }



somewhere else in your code.. you could put data.get(index) to get the object at index.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1