Hi all. I am currently doing a school assignment which requires me to find abbreviations in a given text file and add their full names besides them (a database of abbreviations are given.) and print it on another text file while maintaining each line within the max_linewidth given by the user.
I am stucked with the paragraphing. been trying to solve this whole day. Hope you all can help.
Below is my code.
Code:
import java.util.*;
import java.io.*;
public class TranslateApp
{
private Database database;
public TranslateApp(Database database)
{
this.database = database;
}
public int translateFile(String input_filename, String output_filename, int max_linewidth)
{ int count = 0;
boolean check = true;
Scanner sc = new Scanner(System.in);
StringBuffer buffer = new StringBuffer(max_linewidth);
try{
BufferedReader brStream = new BufferedReader(new FileReader (input_filename));
PrintWriter outwriter = new PrintWriter(new BufferedWriter(new FileWriter(output_filename)));
String input = brStream.readLine();
while (input != null)
{
if(input.length() == 0)
{
outwriter.write("\n");
System.out.print("\n");
}
StringTokenizer token = new StringTokenizer(input," .", true); //true means it will return and print out the delimiters as well
while(token.hasMoreTokens())
{
String temp = token.nextToken();
for (int index = 0; index<database.getnumAbbreviations(); index++)
{
if(database.getAbbreviationsbyIndex(index).matchbyabbrname(temp) == true)
{
if(buffer.length() + temp.length() <= max_linewidth)
buffer.append(temp);
else
{
outwriter.write(buffer.toString() + "\n");
System.out.print(buffer.toString() + "\n");
buffer = new StringBuffer(max_linewidth);
buffer.append(temp);
}
if (buffer.length() + 2 <= max_linewidth)
buffer.append(" (");
else
{
outwriter.write(buffer.toString() + "\n");
System.out.print(buffer.toString() + "\n");
buffer = new StringBuffer(max_linewidth);
buffer.append(" (");
}
StringTokenizer token2 = new StringTokenizer(database.getAbbreviationsbyIndex(index).getfullnames(0), " ", true);
// the StringBuffer will only append if there are capacity left in each line.
while (token2.hasMoreTokens())
{
String temp2 = token2.nextToken();
if (buffer.length() + temp2.length() <= max_linewidth)
buffer.append(temp2);
else
{
outwriter.write(buffer.toString() + "\n");
System.out.print(buffer.toString() + "\n");
buffer = new StringBuffer(max_linewidth);
buffer.append(temp2);
}
}
for(int i = 1; i<database.getAbbreviationsbyIndex(index).getnumfullnames(); i++) //i starts from 1 so that code will take the 2nd fullname in fullname array
{
if(database.getAbbreviationsbyIndex(index).getnumfullnames() > i)
{
if(buffer.length() + 4 <= max_linewidth)
buffer.append(" or ");
else
{
outwriter.write(buffer.toString() + "\n");
System.out.print(buffer.toString() + "\n");
buffer = new StringBuffer(max_linewidth);
buffer.append(" or ");
}
token2 = new StringTokenizer(database.getAbbreviationsbyIndex(index).getfullnames(i), " ", true);
while (token2.hasMoreTokens())
{
String temp3 = token2.nextToken();
if (buffer.length() + temp3.length() <= max_linewidth)
buffer.append(temp3);
else
{
outwriter.write(buffer.toString() + "\n");
System.out.print(buffer.toString() + "\n");
buffer = new StringBuffer(max_linewidth);
buffer.append(temp3);
}
}
}
}
count++;
if ((buffer.length() + 1)<= max_linewidth)
buffer.append(")");
else
{
outwriter.write(buffer.toString() + "\n");
System.out.print(buffer.toString() + "\n");
buffer = new StringBuffer(max_linewidth);
buffer.append(")");
}
check = false; //so that the token ie. the Abbreviation will not be printed again.
}
}
if(check == true)
{
if((buffer.length() + temp.length())<=max_linewidth)
buffer.append(temp);
else
{
outwriter.write(buffer.toString() + "\n");
System.out.print(buffer.toString() + "\n");
buffer = new StringBuffer(max_linewidth);
buffer.append(temp);
}
}
check = true;
}
input = brStream.readLine();
}
outwriter.write(buffer.toString() + "\n");
System.out.print(buffer.toString() + "\n");
brStream.close();
outwriter.close();
}
catch(FileNotFoundException e)
{
System.out.println("IO error!" + e.getMessage());
}
catch(IOException e)
{
System.out.println("IO error!" + e.getMessage());
}
catch(Exception e)
{
System.out.println("Error!" + e.getMessage());
}
return count;
}
public boolean inputCheck(String input_filename)
{
File infile = new File(input_filename);
if((!infile.exists()) || (!infile.canRead()))
{
if(!infile.exists())
{
System.out.println( input_filename + " not found!");
return false;
}
else if (!infile.canRead())
{
System.out.println(input_filename +" cannot be read!");
return false;
}
}
return true;
}
public boolean outputCheck(String output_filename)
{ Scanner sc = new Scanner(System.in);
File outfile = new File(output_filename);
if(outfile.exists())
{
System.out.println("File [" + output_filename + "] currently exists. Do you want to overwrite it");
System.out.print("Y/N: ");
String temp = sc.next();
char ans = temp.charAt(0);
if(ans == 'N')
return false;
}
return true;
}
}
Below is the input that is given to me
Code:
MORE roads will be closed for this year's NDP and preview than last year,
for a first-ever march through the city by the entire parade contingent.
TP officers IC will also be stationed along the Benjamin Sheares Bridge.
Motorists are advised not to stop there to watch the fireworks display.
The bridge will be closed to pedestrians.
Vehicles parked illegally in the area will be towed away. Those parked
at Marina Square should avoid exiting into Raffles Avenue from 2pm onwards.
As large crowds are anticipated in the area, the police advise the public
to consider using alternative stations to City Hall MRT Station, such as
Raffles Place, Clarke Quay and Bugis.
Police officers will regulate areas such as Esplanade Bridge and Merlion
Park where those without tickets to the event can gather to watch the
march and the fireworks display. They may stop people from going there
if necessary to prevent overcrowding.
Instead of getting this output
Code:
MORE roads will be closed for this year's NDP (National Day
Parade) and preview than last year,for a first-ever march
through the city by the entire parade contingent.
TP (Temasek Polytechnic or Traffic Police) officers IC
(Identity card or In charge) will also be stationed along
the Benjamin Sheares Bridge. Motorists are advised not to
stop there to watch the fireworks display. The bridge will
be closed to pedestrians.
Vehicles parked illegally in the area will be towed away.
Those parked at Marina Square should avoid exiting into
Raffles Avenue from 2pm onwards.
As large crowds are anticipated in the area, the police
advise the public to consider using alternative stations
to City Hall MRT (Mass Rapid Transit) Station, such as
Raffles Place, Clarke Quay and Bugis.
Police officers will regulate areas such as Esplanade Bridge
and Merlion Park where those without tickets to the event can
gather to watch the march and the fireworks display. They may
stop people from going there if necessary to prevent overcrowding.
I get this.
Code:
MORE roads will be closed for this year's NDP (National Day
Parade) and preview than last year,for a first-ever march
through the city by the entire parade contingent.TP (Temasek
Polytechnic or Traffic Police) officers IC (Identity card
or In charge) will also be stationed along the Benjamin
Sheares Bridge. Motorists are advised not to stop there to
watch the fireworks display. The bridge will be closed to
pedestrians.Vehicles parked illegally in the area will be
towed away. Those parked at Marina Square should avoid
exiting into Raffles Avenue from 2pm onwards.As large crowds
are anticipated in the area, the police advise the public
to consider using alternative stations to City Hall MRT (
Mass Rapid Transit) Station, such as Raffles Place, Clarke
Quay and Bugis.Police officers will regulate areas such as
Esplanade Bridge and Merlion Park where those without
tickets to the event can gather to watch the march and the
fireworks display. They may stop people from going there if
necessary to prevent overcrowding.