Results 1 to 4 of 4
  1. #1
    Rabid Hamster is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default 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());
    		}
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Getting weird characters when trying to write output to file

    Java 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?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default 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?

  4. #4
    Rabid Hamster is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default 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 View Post
    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 View Post
    Have you read the API doc for the write() method you are using?
    I did...but apparently I confused everything. It works now.

Similar Threads

  1. Need to write chinese characters to csv file
    By Jayashreevmenon in forum Advanced Java
    Replies: 0
    Last Post: 08-04-2011, 10:50 AM
  2. Output write into a file
    By priya2184 in forum New To Java
    Replies: 11
    Last Post: 05-13-2011, 06:42 PM
  3. how to write output to a txt file? thanks!!!
    By sunsnow86 in forum New To Java
    Replies: 5
    Last Post: 01-24-2011, 01:05 PM
  4. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  5. Replies: 1
    Last Post: 07-24-2007, 02:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •