Results 1 to 7 of 7
  1. #1
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default Specific amount of character in each line?

    I write a file with different characters in a single line, how can I write lets say 8 character and go one line down. I do not want one single line, I want to have multiple lines, each one has 8 character on it.
    Java Code:
    outFile.print(pixel);

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You could use perhaps a PrintWriter object that is initialized with your output file or a FileReader object, and then call println on the PrintWriter object just as you do for System.out (which also happens to be a PrintWriter object).

  3. #3
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default

    Yes I am using PrintWriter, actually I read a file which has single int in each line total 64 lin4, and I create two dimentional array [8][8], and each int represent a specific character. and it is time to display this as an image. however when I try to write those character I wrote them in one line. But I want to write them 8 character per line so it will look like an image. I am not sure if I explain my problem clearly.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default

    this is what I have tried;
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    public class Program6
    {
    	static final int ROWS = 8;
        static final int COLS = 8;
    	public static void main(String[] args) throws IOException
    	{
    		
    		Scanner inFile;
    		PrintWriter outFile;
    		inFile = new Scanner(new File("infile.txt"));
    		outFile = new PrintWriter(new FileWriter("outfile.txt"));
    	    String pixel;
    
    		
    		//declare and instantiate an array
    		int [][] picture = new int [ROWS] [COLS];
    		for (int row = 0; row<ROWS; row++)
    		{
    			for(int col = 0; col<COLS; col++)
    			{
    				picture[row][col] = inFile.nextInt();
    			}
    			
    		}
    			
    			
    		for(int row = 0; row<ROWS; row ++)
    		{
    			for (int col = 0; col<COLS; col++)
    			{
    				if(picture[row][col]>=0 && picture[row][col]<=7)
    				{
    				    pixel = " ";
    				}
    				else if(picture[row][col]>=8 && picture[row][col]<=15)
    				{
    					pixel = ".";
    				}
    				else if(picture[row][col]>=16 && picture[row][col]<=23)
    				{
    					pixel =",";
    				}
    				else if(picture[row][col]>=24 && picture[row][col]<=31)
    				{
    					pixel = "-";
    				}
    				else if(picture[row][col]>=32 && picture[row][col]<=39)
    				{
    					pixel = "+";
    				}
    				else if(picture[row][col]>=40 && picture[row][col]<=47)
    				{
    					pixel = "o";
    				}
    				else if(picture[row][col]>=48 && picture[row][col]<=55)
    				{
    					pixel = "0";
    				}
    				else 
    				{
    					pixel = "X";
    				}
    				
    [COLOR="Red"]				for (int counter= 0; counter<9; counter++)
    				     outFile.print(pixel);	[/COLOR]			}	
    							
    		}        	
    				inFile.close();
    				outFile.close();
    	}
    	
    }

  6. #6
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default

    I couldn't figure out how to display those pixels 8 characters per line

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    get rid of the for loop here:
    Java Code:
    for (int counter= 0; counter<9; counter++)
    				     outFile.print(pixel);

    and instead use the nested for loops that you already have.

    Java Code:
    		for(int row = 0; row<ROWS; row ++)
    		{
    			for (int col = 0; col<COLS; col++)
    			{
    				if(picture[row][col]>=0 && picture[row][col]<=7)
    				{
    				    pixel = " ";
    				}
    				else if(picture[row][col]>=8 && picture[row][col]<=15)
    				{
    					pixel = ".";
    				}
    				else if(picture[row][col]>=16 && picture[row][col]<=23)
    				{
    					pixel =",";
    				}
    				else if(picture[row][col]>=24 && picture[row][col]<=31)
    				{
    					pixel = "-";
    				}
    				else if(picture[row][col]>=32 && picture[row][col]<=39)
    				{
    					pixel = "+";
    				}
    				else if(picture[row][col]>=40 && picture[row][col]<=47)
    				{
    					pixel = "o";
    				}
    				else if(picture[row][col]>=48 && picture[row][col]<=55)
    				{
    					pixel = "0";
    				}
    				else 
    				{
    					pixel = "X";
    				}
    				
    				outFile.print(pixel); // prints a pixel for each column
    			}
    		    outFile.println();  // prints a new line for each row
    									
    		}

Similar Threads

  1. Replies: 4
    Last Post: 11-03-2010, 07:17 PM
  2. read a specific line in an input file
    By sara12345 in forum Advanced Java
    Replies: 7
    Last Post: 01-03-2010, 11:40 PM
  3. writing to specific line in text file
    By mickmos in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 02:01 PM
  4. Writing To A Specific Text File Line
    By mokonji in forum New To Java
    Replies: 1
    Last Post: 03-02-2009, 09:13 PM
  5. New line character
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 07:24 PM

Posting Permissions

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