Hi i need help.. Im actually trying to read a file line by line from the input text file and then write word by word into the output text file. In case a word is found to match exactly an abbreviation name in the database object, this method will additionally output the full names. The max_linewidth arguement has to be at least 30, and translafeFile has to ensure that the number of characters including space in every line in the output text file does not exceed max_linewidth. Below is my code so far.. i have not been able to sovle the max_linewidth problem to print out properly.
txt file
Code:The MOE has given out a total of 420 teaching scholarships and awards, the highest number of scholarships and awards given out since 2003. MOE said that it has a bumper harvest this year because of the strong interest in teaching among quality candidates. Four candidates have been awarded the prestigious Overseas Merit Scholarships for Teaching by the Public Service Commission, while 17 others have been given the coveted Education Merit Scholarship.
Code:import java.io.*; import java.util.StringTokenizer; public class Translate{ private Data db; public Translate(Database database){ this.database=database; } public int translateF(String ifilename,String filename,int max_lwidth){ int numAbbr=0; try{ BufferedReader br =new BufferedReader(new FileReader(input_filename+".txt")); PrintWriter pw =new PrintWriter(new BufferedWriter(new FileWriter(output_filename+".txt"))); String returnStr=processToken(br.readLine()); if(returnStr.length()-1<max_linewidth) pw.write(returnStr.substring(0,returnStr.length()-1)); //test System.out.println("check:"+returnStr.substring(returnStr.length()-1)); //writer.write(); br.close(); pw.close(); } catch(FileNotFoundException e){ System.out.println("TOError: File not found! -"+input_filename); System.exit(0); } catch(IOException e){ System.out.println(e.getMessage()); } return -1; } public String processToken(String file){ StringTokenizer st=new StringTokenizer(file); int count=0; String str;//store token String line=""; while(st.hasMoreTokens()){ str=st.nextToken(); line+= str+" "; //loop for the total number of abbreviations for(int i=0;i<database.getNumAbbreviations();i++){ //loop for total number of fullname in a abbreviation for(int y=0;y<database.getAbbreviationByIndex(i).getNumFullNames();y++){ System.out.println(database.getAbbreviationByIndex(i).getAbbrName().trim()); if(database.getAbbreviationByIndex(i).getAbbrName().trim().equalsIgnoreCase(str.trim())){ line+= "("+database.getAbbreviationByIndex(i).getFullName(y)+") "; count++; } }//end for }//end for }//end while System.out.println(line); if(count!=0){ return line+count; } else return null; } }




Reply With Quote