Getting weird characters when trying to write output to file
Hello everyone. I am trying to get my program write its output to a txt file. I ran it in command prompt on Windows 7 and in Eclipse on Ubuntu, and all I got was a bunch of strange looking symbols. Could anyone please help me understand why this is happening? Here is the code that I'm running. Thanks in advance.
Code:
import java.io.*;
import java.util.*;
class Problem1 {
static Boolean fits(int width, int height, int font, Vector<String> vc) {
for (int l = 2; l < vc.size(); l++)
if ((vc.get(l).length() * font > width) || (font > height))
return false;
int i = font;
int j = 0;
int t = 2;
while (t < vc.size()) {
j = 0;
while (j <= width && t < vc.size()) {
j += ((vc.get(t).length() + 1) * font);
if (j > width)
i += font;
if (j <= width + font)
t++;
if (t >= vc.size() && j > width)
i--;
}
}
return (i <= height);
}
public static void main(String args[]) {
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("input.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = br.readLine();
int numberOfLines;
int width;
int height;
numberOfLines = Integer.parseInt(strLine);
// File file1=new File("output.txt");
FileWriter fstreamout = new FileWriter("output.txt");
BufferedWriter out = new BufferedWriter(fstreamout);
// Read File Line By Line
for (int m = 0; m < numberOfLines; m++) {
strLine = br.readLine();
Vector<String> vc = new Vector<String>();
StringTokenizer token;
token = new StringTokenizer(strLine);
while (token.hasMoreTokens()) {
vc.add(token.nextToken());
}
// process the input string
height = Integer.parseInt((String) (vc.get(1)));
width = Integer.parseInt((String) (vc.get(0)));
int h = height < width ? height : width;
int upperBound = h + 1;
int lowerBound = 1;
if (!(fits(width, height, 1, vc))) {
out.write("0");
out.newLine();
} else {
while (upperBound != lowerBound) {
int ups = upperBound;
int lows = lowerBound;
if (fits(width, height, lowerBound
+ (upperBound - lowerBound) / 2, vc)) {
lowerBound = lowerBound + (upperBound - lowerBound)
/ 2;
} else {
upperBound = lowerBound + (upperBound - lowerBound)
/ 2 - 1;
}
if (ups == upperBound && lows == lowerBound)
break;
}
if (upperBound == lowerBound) {
out.write(upperBound);
out.newLine();
} else {
if (fits(width, height, upperBound, vc)) {
out.write(upperBound);
out.newLine();
} else {
out.write(lowerBound);
out.newLine();
}
}
}// end else
}
// Close the input stream
in.close();
out.close();
} catch (Exception e) {// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
Re: Getting weird characters when trying to write output to file
Code:
out.write(upperBound);
...
out.write(lowerBound);
upperBound and lowerBound are both ints, so these calls are using write(int).As you can see from the API:
"
Writes a single character.
"
That int is representing a UTF character, not the character representation of that number...consequently you are getting interesting stuff.
Out of suriority, why are you creating a DataInputStream?
Re: Getting weird characters when trying to write output to file
Have you read the API doc for the write() method you are using?
What does it say it will write to the file?
What do you want written to the file? Your code appears to write the contents of int variables, not Strings.
Have you looked at the contents of the file in a hex_editor to see what was written there?
Re: Getting weird characters when trying to write output to file
Tolls, Norm, thanks you. I fixed the problem by using PrintWriter instead of BufferedWriter and then print(Integer.toString(int)).
Quote:
Originally Posted by
Tolls
Out of suriority, why are you creating a DataInputStream?
In order to read the input. I'm not really sure what you mean. Is anything wrong with it?
Quote:
Originally Posted by
Norm
Have you read the API doc for the write() method you are using?
I did...but apparently I confused everything. It works now.