Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7629351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:48:52+00:00 2026-05-31T05:48:52+00:00

My problem is that I have to write code to check if a customers

  • 0

My problem is that I have to write code to check if a customers name is already in my txt file (Customers.txt).

The problem is that I check with hashset if the customer is in the file and he says that he isn’t in the file while he is.(Checked it manually)
Please help me solve this.

The code that I already have is below.

    public class Customer {
    //declaration
    public static String S;
    private String line ;
    public String nameCustomer;


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

/**Checking of the customer is in the file
 */
public void getCustomer() {
    // 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/Customers.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(nameCustomer)){
        //the customer exists
     input.showString("The customer exists", "");
     }else{input.showString("The customer does not exist, we will make a new one", "");
         setNieuweKlant();}


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

}


/**
 * make a new customer
 */

public void Make new customer(){
    // make SimpleInOutDialog     
    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();

}


/**
 * Schrijft de gegevens weg naar het bestand
 */


 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());
    }
    }

Dutcht Code

public class Klant {
    //declaratie van de variabele die de tekst voorsteld
    public static String S;
    private String line ;
    public String naamklant;


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

/**Controleerd of de klant al bestaat
 */
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{input.showString("De klant bestaat niet, er wordt een nieuwe klant aangemaakt", "");
         setNieuweKlant();}


    }catch (Exception e){//Catch wanneer er errors zijn
        System.err.println("Error: " + e.getMessage());}

}


/**
 * Maakt een nieuwe klant aan 
 */

public void setNieuweKlant(){
    // SimpleInOutDialog aanmaken     
    SimpleInOutDialog  input = new SimpleInOutDialog("Een nieuwe klant");
    //input
    S = input.readString("Geef de volledige naam in");
    WriteToFile();
    S = "Adres: " + input.readString("Geef het adres op");
    WriteToFile();
    S = "Telefoonummer: " + input.readString("Geef het telefoonnummer op");
    WriteToFile();
    //een klantennummer aanmaken 
      UUID idKlant = UUID.randomUUID();  
    S = "Klantnummer: " + idKlant.toString();
    WriteToFile();

}


/**
 * Schrijft de gegevens weg naar het bestand
 */
public void WriteToFile(){

    try{

        FileWriter writer = new FileWriter("L:\\Documents/Informatica/6de jaar/GIP/Klanten.txt", true);
        BufferedWriter out = new BufferedWriter(writer);
        //uw gegevens wegschrijven
        out.write(S);
        out.newLine();
        //de writer sluiten
        out.close();


    }catch (Exception e){//Catch wanneer er errors zijn
        System.err.println("Error: " + e.getMessage());}



    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:48:53+00:00Added an answer on May 31, 2026 at 5:48 am

    I’m really not sure what you’re trying to do overall (reading every 4th line, etc.), but I wouldn’t use a HashSet if all you want is to check if a string exists in a file.

    Here’s a complete program for checking if a string exists in a file:

    import java.io.*;
    
    public class CheckName
    {
      private static boolean doesNameExistInFile(String name, String filename)
        throws FileNotFoundException, IOException
      {
        BufferedReader reader = new BufferedReader(new FileReader(filename));
    
        String line = null;
    
        try {
          while ((line = reader.readLine()) != null) {
            if (line.equals(name))
              return true;
          }
    
        } finally {
          // Always close() before exiting.
          reader.close();
        }
    
        return false;
      }
    
      public static void main(String[] args)
        throws FileNotFoundException, IOException
      {
        boolean exists = doesNameExistInFile("Bender", "input.txt");
    
        System.out.println(exists ? "Exists!" : "Does not exist.");
      }
    }
    

    Contents of input file input.txt:

    Leela
    Fry
    Professor
    Hermes
    Zoidberg
    

    A couple of things to note:

    1. We don’t read the entire file. We stop reading once we’ve found the string, as that’s all we’re interested in.
    2. Always close() before exiting, whether string is found or not. We put the call in the finally block.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is that I have to write code to check if a customers
I have an intermittent problem with some code that writes to a Windows Event
I have to write an applet that brings up a password dialog. The problem
I have problem with fancybox. I want to write a function that will run
I'd like to write some code that would take a given data frame, check
My problem is that I can't figure out how to read the customers name
Basically, I have to write a basic program that solves the n-queen problem, which
I have wrote an application that syncs two folders together. The problem with the
I have encountered a problem that I have not come accross yet when setting
We have the problem that we have the open project files in our SVN

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.