Question

My problem is that I can't figure out how to read the customers name with File Reader.

I am making a reservation system and I need to know of the customer already exist. That's why I have to read my Customers.txt file so I can check if someone is already a customer. If he is not I will make a new one with File writer(I Already have the code).

The meaning of this reservation system is to make reservations with a barber. I have to put the reservations in another txt file called Reservations.txt , and in that file you can see each reservation time and who made the reservation.

Thanks for the help!

This is the code I already have: (some comments are in Dutch but I will translate them )

package nielbutaye;
import java.io.*;
import java.util.UUID;
/**
 * @author Niel
 *
 */
public class Klant {
    //declaration that represents the text
    public static String S;

    public static String NEWLINE = System.getProperty("line.separator");

/**
 * constructor
 */
public Klant(){}

/**
 * @return
 * By giving the name of the customer you will get all the data from  the customer
 */
public double getCustomer() {


    return 0 ;
}

/**
 * Make a new customer
 */

public void setNew customer(){
    // make a newSimpleInOutDialog     
    SimpleInOutDialog  input = new SimpleInOutDialog("A new customer");
    //input
    S = "Name customer: " + input.readString("Give in your name:");
    WriteToFile();
    S = "Adress: " + input.readString("Give your adress");
    WriteToFile();
    S = "Telephonenummber: " + input.readString("Give your telephonenumber");
    WriteToFile();
    //making a customerID
      UUID idCustomer = UUID.randomUUID();  
    S = "CustomerID: " + customerID.toString();
    WriteToFile();

}

public void WriteToFile(){
try{

    FileWriter writer = new FileWriter("L:\\Documents/Informatica/6de jaar/GIP/Customer.txt", true);
    BufferedWriter out = new BufferedWriter(writer);
    //Wrting away your data
    out.write(S + NEWLINE);
    //Closing the writer
    out.close();


}catch (Exception e){//Catch when there are errors
    System.err.println("Error: " + e.getMessage());
    }
    }
}
Was it helpful?

Solution

A BufferedReader() has a method named readLine(), which you can use to read a line from a file:

BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;

while ((line = br.readLine()) != null)
{
    ...
}

br.close();

From your WriteToFile() method it seems a customer's details occupies four lines, with the name of the customer appearing on the first line. When searching for a customer, arrange the while loop to only examine every fourth line read.

Other points:

  • There appears to be no reason for S to be a member variable, nevermind a static. Just declare a local String instance in setNewCustomer() and pass it as an argument to WriteToFile().
  • Instead of defining a NEWLINE variable you can use BufferedWriter's newLine() method.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top