java - Checking of a line is in a file with bufferedread and hashset -
my problem have write code check if customers name in txt file (customers.txt).
the problem check hashset if customer in file , says isn't in file while is.(checked manually) please me solve this.
the code have below.
public class customer { //declaration public static string s; private string line ; public string namecustomer; /** * constructor */ public klant(){} /**checking of customer in file */ public void getcustomer() { // make simpleinoutdialog simpleinoutdialog input = new simpleinoutdialog("customers"); namecustomer = input.readstring("give in name"); try{ bufferedreader br = new bufferedreader(new filereader("l:\\documents/informatica/6de jaar/gip/customers.txt")); hashset<string> hs = new hashset<string>(); int = 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 not exist, make new one", ""); setnieuweklant();} }catch (exception e){//catch when there errors system.err.println("error: " + e.getmessage());} } /** * make new customer */ public void make new customer(){ // make simpleinoutdialog simpleinoutdialog input = new simpleinoutdialog("a new customer"); //input s = "name customer: " + input.readstring("give in name:"); writetofile(); s = "adress: " + input.readstring("give adress"); writetofile(); s = "telephonenummber: " + input.readstring("give telephonenumber"); writetofile(); //making 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 data out.write(s); //closing writer out.close(); }catch (exception e){//catch when there 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 = 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 not sure you're trying overall (reading every 4th line, etc.), wouldn't use hashset
if want check if string exists in file.
here's complete program checking if string exists in 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; } } { // 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 note:
- we don't read entire file. stop reading once we've found string, that's we're interested in.
- always
close()
before exiting, whether string found or not. put call infinally
block.
Comments
Post a Comment