问题
My problem is that I have to write code to check if a customers name is already in my txt file (Customers.txt).
As you can see, I write my customers name ,every fourth line in my txt File. I would like to have the input of a customers name with SimpleInOutDialog en then that inputs needs to be compared with al the customers name in my txt file. How do I need to do this?
The code that I already have is below.
//make SimpleInOutDialog
SimpleInOutDialog input = new SimpleInOutDialog("Customers");
namecustomer = input.readString("Give in the name");
try{
BufferedReader br = new BufferedReader(new FileReader("L:\\Documents/Informatica/6de jaar/GIP/klanten.txt"));
HashSet<String> hs = new HashSet<String>();
int i = 0;
while ((line = br.readLine()) != null)
{
i++;
if (i % 4 == 0){
if(hs.contains(namecustomer)){
//customer exists
input.showString("The customer exists", "");}
else
{setNewcustomer();}}
}
}catch (Exception e){//Catch wanneer er errors zijn
System.err.println("Error: " + e.getMessage());}
return line;
}
public void setNewcustomer(){
// 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);
//Closing the writer
out.close();
}catch (Exception e){//Catch when there are errors
System.err.println("Error: " + e.getMessage());
}
}
The changes need to be in be in getCustomer() Thanks!!
Hi , Solved my problem , here is the solution:
public void getKlant() {
// SimpleInOutDialog aanmaken
SimpleInOutDialog input = new SimpleInOutDialog("Klanten");
naamklant = input.readString("Geef de volledige naam in");
try{
BufferedReader br = new BufferedReader(new FileReader("L:\\Documents/Informatica/6de jaar/GIP/klanten.txt"));
HashSet<String> hs = new HashSet<String>();
int i = 0;
while ((line = br.readLine()) != null)
{
i++;
if (i == 1){hs.add(br.readLine());}
if (i % 4 == 0){hs.add(br.readLine());}
}
if(hs.contains(naamklant)){
//klant bestaat
input.showString("De klant bestaat", "");
}else{setNieuweKlant();}
}catch (Exception e){//Catch wanneer er errors zijn
System.err.println("Error: " + e.getMessage());}
}
回答1:
Use a HashSet to store all of the names you read and then it's just to call the contains methode to see if the customer already exists. If the names are stored on the forth line the code should look like this
HashSet<String> hs = new HashSet<String>();
int i = 0;
while ((line = br.readLine()) != null)
{
i++;
if (i % 4 == 0)
{
if(hs.contains(line))
//name already exist
else
hs.add(line);
// new customer do what you want with it
}
}
来源:https://stackoverflow.com/questions/9589977/checking-if-a-customers-name-is-already-in-a-txt-file-with-filereader