줄을 확인하는 것은 bufferedRead와 hashset 파일에 있습니다.
-
10-12-2019 - |
문제
내 문제는 고객 이름이 이미 내 txt 파일 (customers.txt)에 있는지 확인하는 코드를 작성해야한다는 것입니다.
문제는 고객이 파일에 있으면 해시 세트로 확인하고 그가있는 동안 파일에 있지 않다고 말합니다. (수동으로 확인) 나를 해결하도록 도와주세요.
이미 가지고있는 코드가 아래에 있습니다.
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 코드
.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());}
}
}
해결책
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:
- We don't read the entire file. We stop reading once we've found the string, as that's all we're interested in.
- Always
close()
before exiting, whether string is found or not. We put the call in thefinally
block.
다른 팁
This:
while ((line = br.readLine()) != null)
{
i++;
if (i == 1){hs.add(br.readLine());}
if (i % 4 == 0){hs.add(br.readLine());}
}
is probably supposed to be this:
while ((line = br.readLine()) != null)
{
i++;
if (i == 1)
hs.add(line);
if (i % 4 == 0)
hs.add(line);
}
That is — you probably meant to add
the line that you had just read, rather than reading in a new line and add
ing it.