hey guys,
i'm new to java ,,,and i have a problem wish u to get ur answer ASAP
i'm reading from a text file by PrintWriter , and there are 3 rows first row is ID
second row is name and third row is address. and i want to get the IDs into the java program so
that i could call a name with it.for example : if in the text file 001 is linked to "name" i want to call
that name by calling the id in the program....thanks.
stultuske 1,116 Posting Maven Featured Poster
and what exactly are you having trouble with? can you show us the code you've got so far and tell us where it's going wrong?
lody 0 Newbie Poster
i explained everything in the text file....if it's possible please take a look and give me a proper solution :)
thank you very much ...
Scroll down to (=====>>>)
TExt file is patient.txt
2nd text file is doctor.txt
the patient text is :
001 lod london
002 daniweb USA
003 neyaw china
second class :
package assignment;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class second {
private String did;
private String pid;
private Scanner x;
private Scanner y;
private Formatter a;
private Formatter b;
private String DExpertise;
public void openFile(){
//just reads the file for special use !
try{
x=new Scanner(new File("doctor.txt"));
} catch (Exception e) {
System.out.println("Error while reading file due to "+e);
}
try{
y=new Scanner(new File("patient.txt"));
}catch(Exception e){
System.out.println("Error while rading fille due to "+e);
}
}
public void writePfile(String id,String name,String address){
try{
PrintWriter pt = new PrintWriter(new BufferedWriter(new FileWriter("patient.txt",true)));
pt.printf("\r\n%s %s %s \n", id,name,address);
pt.close();
}
catch(Exception e){
System.out.println("Error while reading "+e);
}
}
public void writeDfile(String id,String name,String Ex,int numb){
try{
PrintWriter dr = new PrintWriter(new BufferedWriter(new FileWriter("doctor.txt",true)));
dr.printf("\r\n%s %s %s %02d \n", id,name,Ex,numb);
}
catch(Exception e){
System.out.println("Error while reading "+e);
}
}
}
}catch(Exception e){
System.out.println("error while reading "+e);
}
}
public void readDoctorfile(){
while(x.hasNext()){
String a=x.next();
String b=x.next();
String c=x.next();
String d=x.next();
System.out.printf("%s %s %s %s \n",a,b,c,d);
}
}
public void readPatientfile(){
while(y.hasNext()){
String e=y.next();
String f=y.next();
String g=y.next();
System.out.printf("%s %s %s \n",e,f,g);
}
}
public void deletepatient (){
=========>PROBLEM HERE !!!!<<<=====
=========>PROBLEM HERE !!!!<<<=====
when the user gives an input to delete a patient only the
id should be entered from the user <<< and the how row
assigned to the ID must be removed ...for ex :
if user enters 002 the row assigned to 002 must be deleted
in this case the program is taking ID but nothing is happening !
>>>>>AND IF it is possible can u suggest a way to update a patient
for example if the user enter 002 the name and address of 002 be updated
from the user ! ;;;;thanks very much
File input=new File("patient.txt");
File output=new File("temp.txt");
try{
BufferedReader reader = new BufferedReader(new FileReader(input));
BufferedWriter writer = new BufferedWriter(new FileWriter(output));
String lineToRemove = remove;
String currentLine;
while((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove)) continue;
writer.write(currentLine);
}
boolean successful = output.renameTo(input);
}catch(IOException e){
System.out.println("Error while reading "+e);
}
}
public void closefiles(){
x.close();
y.close();
}
}
main class :
package assignment;
import java.util.*;
public class Assignment {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
second sec=new second();
int choice =0;
String pID;
String dID;
String pName;
String dName;
String pAddress;
String dExpertise;
int dNumbP;
sec.openFile();
sec.readDoctorfile();
sec.readPatientfile();
while (choice !=13){
System.out.println("1=> Patient Registration (Insertion)");
System.out.println("2=> Delete a Patient");
System.out.println("3=> Update Patient Data");
System.out.println("4=> Insert a New Doctor");
System.out.println("5=> Delete a Doctor");
System.out.println("6=> Update Doctor Data");
System.out.println("7=> Select a Doctor");
System.out.println("9=> Patient Query");
System.out.println("10=> List All Patients");
System.out.println("11=> List All Doctors");
System.out.println("12=> Write into Files");
System.out.println("13=> Exit");
System.out.println("Please select by entering a number :");
choice=input.nextInt();
if(choice==1){
System.out.println("Enter ID of the patient : ");
pID=input.next();
System.out.println("Enter Name of the patient :");
pName=input.next();
System.out.println("Enter the Address the patient : ");
pAddress=input.next();
System.out.println("Patieent added to list succesfully !");
sec.writePfile(pID,pName,pAddress);
}
if (choice==2){
System.out.println("Enter the ID of the patient : ");
pID=input.next();
sec.delete(pID);
}
else if (choice==3){
System.out.println("Enter the ID of the patient : ");
pID=input.next();
}
else if(choice==4) {
System.out.println("Enter the ID of the the Doctor : ");
dID=input.next();
System.out.println("Enter the Name of the doctor : ");
dName=input.next();
System.out.println("Enter the Expertise of the doctor :");
dExpertise=input.next();
System.out.println("Enter the number of patients of the doctor has :");
dNumbP=input.nextInt();
System.out.println("Doctor added to list successfully !");
sec.writeDfile(dID, dName, dExpertise, dNumbP);
}
else if (choice ==5){
System.out.println("Enter the ID of the Doctor : ");
dID=input.next();
}
else if (choice ==6){
System.out.println("Enter the ID of the Doctor : ");
dID=input.next();
}
else if (choice ==7){
System.out.println("Enter the ID of the doctor : ");
dID=input.next();
System.out.println("Enter the ID of the Patient : ");
pID=input.next();
}
else if (choice ==8){
System.out.println("Enter the ID of the patient : ");
pID=input.next();
}
else if (choice ==9){
System.out.println("Enter the ID of the doctor :");
dID=input.next();
}
else if (choice ==10){
sec.openFile();
sec.readPatientfile();
sec.closefiles();
}
else if (choice==11){
sec.openFile();
sec.readDoctorfile();
sec.closefiles();
}
else if (choice==12){
int ch=0;
while (ch !=3){
System.out.println("1=> Update Doctor file :");
System.out.println("2=> Update Patient file : ");
System.out.println("plese choose by selecting the number");
ch=input.nextInt();
if (ch==1){
System.out.println("Enter the doctor ID to be updated :");
dID=input.next();
System.out.println("Enter the name of the doctor to be updated :");
dName=input.next();
System.out.println("Enter the Experstise of the doctor to be updated :");
dExpertise=input.next();
System.out.println("Enter the number of the patients of the doctor : ");
dNumbP=input.nextInt();
sec.writeDfile(dID, dName, dExpertise, dNumbP);
}
else if(ch==2){
System.out.println("Enter the patient ID to be updated :");
pID=input.next();
System.out.println("Enter the name of the patient to be updated :");
pName=input.next();
System.out.println("Enter the address of the patient to be updated :");
pAddress=input.next();
sec.writePfile(pID,pName,pAddress);
}
}
}
}
}
}
stultuske 1,116 Posting Maven Featured Poster
I don't really see you entering or using the input, so what exactly are you trying to do?
for instance:
String lineToRemove = remove;
where do you instantiate the 'remove' variable? it's pretty hard to see in that .txt. can't you paste your code in a post here, in a seperate code block per class?
or, for this:
if(trimmedLine.equals(lineToRemove)) continue;
now, as I said, I'm not entirely sure, but, you only read the number, say 002.
have you tried:
if(trimmedLine.startsWith(lineToRemove)) continue;
personally, I would recommend against using the continue; keyword.
replace it with something like this:
if(!(trimmedLine.equals(lineToRemove)))
writer.write(currentLine);
or, if I'm right with my guess that lineToRemove doesn't contain the value you need:
if(!(trimmedLine.startsWith(lineToRemove)))
writer.write(currentLine);
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.