Results 1 to 4 of 4
- 01-23-2012, 05:20 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 2
- Rep Power
- 0
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.
Java 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()); } } }
- 01-23-2012, 06:04 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Getting weird characters when trying to write output to file
upperBound and lowerBound are both ints, so these calls are using write(int).As you can see from the API:Java Code:out.write(upperBound); ... out.write(lowerBound);
"
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?
- 01-23-2012, 06:05 PM #3
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?
- 01-24-2012, 04:10 PM #4
Member
- Join Date
- Jan 2012
- Posts
- 2
- Rep Power
- 0
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)).
In order to read the input. I'm not really sure what you mean. Is anything wrong with it?
I did...but apparently I confused everything. It works now.
Similar Threads
-
Need to write chinese characters to csv file
By Jayashreevmenon in forum Advanced JavaReplies: 0Last Post: 08-04-2011, 10:50 AM -
Output write into a file
By priya2184 in forum New To JavaReplies: 11Last Post: 05-13-2011, 06:42 PM -
how to write output to a txt file? thanks!!!
By sunsnow86 in forum New To JavaReplies: 5Last Post: 01-24-2011, 01:05 PM -
how to change the layout of an input file and write to an output file
By renu in forum New To JavaReplies: 8Last Post: 05-12-2010, 07:19 PM -
how to write the output of the console to a file
By fred in forum New To JavaReplies: 1Last Post: 07-24-2007, 02:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks