import javaio import javautilStringTokenizer public class Pr

import java.io.*;
import java.util.StringTokenizer;
public class Procedure {

//============ Properties =============
private String procCode;
private String procName;
private String procDesc;
private float cost;

//============ Constructors ===========
public Procedure(){
procCode = \"\";
procName=\"\";
procDesc=\"\";
cost = 0.00f;
}
public Procedure(String c,String n,String d,float co){
procCode=c;
procName=n;
procDesc=d;
cost=co;
}

//=========== Behavior ==============
public void setProcCode(String c){procCode=c;}
public String getProcCode(){
return procCode;}

public void setProcName( String n){ procName = n;}
public String getProcName(){
return procName;}

public void setProcDesc(String d){procDesc =d;}
public String getProcDesc(){
return procDesc;}

public void setCost(float co){ cost=co;}
public float getCost(){
return cost;}

public void display(){

System.out.println(\"Procedure Code = \"+getProcCode());
System.out.println(\"Procedure Name = \"+getProcName());
System.out.println(\"Procedure Discription = \"+getProcDesc());
System.out.println(\"Procedure Cost = \"+getCost());

}//end display()

public void select(String pcode)throws Exception{
FileInputStream inFile= null;
InputStreamReader inReader= null;
BufferedReader reader=null;
StringTokenizer str;
String line;
inFile = new FileInputStream(\"Procedures.txt\");
inReader = new InputStreamReader(inFile);
reader = new BufferedReader(inReader);



line = reader.readLine();
  
//System.out.println(\" data line = \"+line);
//System.out.println();
if(line.contains(pcode)){
str = new StringTokenizer(line,\":\");
procCode = str.nextToken();
procName = str.nextToken();
procDesc = str.nextToken();
cost = Float.parseFloat(str.nextToken());
  
while((line =reader.readLine())!= null){
for(int i = 0;i   
System.out.println(line);

  
break;
}
}
}
}



public static void main(String args[])throws Exception{


Procedure p1= new Procedure();
p1.select(\"P114\");
p1.display();
  
Procedure p2= new Procedure();
p2.select(\"P119\");
p2.display();


}//end main
}//end class

(\"Procedures.txt\"):

P114:CleaningExam:TeethCleaning:99.99
P119:XRays:TakeXRays:320.00
P122:Whitening:TeathWhitening:122.99
P321:Cavity:FillACavity:319.00
P650:TopDentures:AddTopDentures:1950.00
P660:BottomDentures:AddBottomDentures:1950.00
P780:Crown:PutInACrown:795.00
P790:RootCanel:ReplaceBadTooth:1019.00
P888:Polishing:xxxxxxxxxx:299.99

I am trying to read from this file line by line. I can only print the first line properly and the rest of the lines print at the top. please solve this problem using StringTokenizer to read from this file line by line and display it as it is called by its procCode. So when P321 is called only that one shows in the result.

here is what I am getting for my result when I run my program:

P119:XRays:TakeXRays:320.00
P122:Whitening:TeathWhitening:122.99
P321:Cavity:FillACavity:319.00
P650:TopDentures:AddTopDentures:1950.00
P660:BottomDentures:AddBottomDentures:1950.00
P780:Crown:PutInACrown:795.00
P790:RootCanel:ReplaceBadTooth:1019.00
P888:Polishing:xxxxxxxxxx:299.99
Procedure Code = P114
Procedure Name = CleaningExam
Procedure Discription = TeethCleaning
Procedure Cost = 99.99
Procedure Code =
Procedure Name =
Procedure Discription =
Procedure Cost = 0.0

Thanks

Solution

import java.io.*;
import java.util.StringTokenizer;
public class Procedure {

//============ Properties =============
private String procCode;
private String procName;
private String procDesc;
private float cost;

//============ Constructors ===========
public Procedure (){
procCode = \"\";
procName=\"\";
procDesc=\"\";
cost = 0.00f;
}
public Procedure (String c,String n,String d,float co){
procCode=c;
procName=n;
procDesc=d;
cost=co;
}

//=========== Behavior ==============
public void setProcCode(String c){procCode=c;}
public String getProcCode(){
return procCode;}

public void setProcName( String n){ procName = n;}
public String getProcName(){
return procName;}

public void setProcDesc(String d){procDesc =d;}
public String getProcDesc(){
return procDesc;}

public void setCost(float co){ cost=co;}
public float getCost(){
return cost;}

public void display(){

System.out.println(\"Procedure Code = \"+getProcCode());
System.out.println(\"Procedure Name = \"+getProcName());
System.out.println(\"Procedure Discription = \"+getProcDesc());
System.out.println(\"Procedure Cost = \"+getCost());

}//end display()

public void select(String pcode)throws Exception{
FileInputStream inFile= null;
InputStreamReader inReader= null;
BufferedReader reader=null;
StringTokenizer str;
String line;
inFile = new FileInputStream(\"Procedures.txt\");
inReader = new InputStreamReader(inFile);
reader = new BufferedReader(inReader);


while((line =reader.readLine())!= null){
//System.out.println(\" data line = \"+line);
//System.out.println(line);
if(line.contains(pcode)){
str = new StringTokenizer(line,\":\");
procCode = str.nextToken();
procName = str.nextToken();
procDesc = str.nextToken();
cost = Float.parseFloat(str.nextToken());
}
}

}


public static void main(String args[])throws Exception{

try {

           String content = \"P114:CleaningExam:TeethCleaning:99.99\ P119:XRays:TakeXRays:320.00\ P122:Whitening:TeathWhitening:122.99\ P321:Cavity:FillACavity:319.00\ P650:TopDentures:AddTopDentures:1950.00\";
           content+=\"P660:BottomDentures:AddBottomDentures:1950.00\ P780:Crown:PutInACrown:795.00\ P790:RootCanel:ReplaceBadTooth:1019.00\ P888:Polishing:xxxxxxxxxx:299.99\ \";

           File file = new File(\"Procedures.txt\");

           // if file doesnt exists, then create it
           if (!file.exists()) {
               file.createNewFile();
           }

           FileWriter fw = new FileWriter(file.getAbsoluteFile());
           BufferedWriter bw = new BufferedWriter(fw);
           bw.write(content);
           bw.close();

       } catch (IOException e) {
           e.printStackTrace();
       }
      

Procedure p1= new Procedure ();
p1.select(\"P114\");
p1.display();

Procedure p2= new Procedure ();
p2.select(\"P119\");
p2.display();


}//end main
}//end class

The reason you are not getting the other lines is because while reading the file you are just reading the first line.

You have to write a while read read through EOF and then do strink tokenization and then compare and whatever we want to do.

I had made this change after the statement whihc initializes readder variable.

After making the chnage, the output is now as belo:

Procedure Code = P114                                                                                                                                                                                                                         

Procedure Name = CleaningExam                                                                                                                                                                                                                 

Procedure Discription = TeethCleaning                                                                                                                                                                                                         

Procedure Cost = 99.99                                                                                                                                                                                                                        

Procedure Code = P119                                                                                                                                                                                                                         

Procedure Name = XRays                                                                                                                                                                                                                        

Procedure Discription = TakeXRays                                                                                                                                                                                                             

Procedure Cost = 320.0     

import java.io.*; import java.util.StringTokenizer; public class Procedure { //============ Properties ============= private String procCode; private String pro
import java.io.*; import java.util.StringTokenizer; public class Procedure { //============ Properties ============= private String procCode; private String pro
import java.io.*; import java.util.StringTokenizer; public class Procedure { //============ Properties ============= private String procCode; private String pro
import java.io.*; import java.util.StringTokenizer; public class Procedure { //============ Properties ============= private String procCode; private String pro
import java.io.*; import java.util.StringTokenizer; public class Procedure { //============ Properties ============= private String procCode; private String pro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site