Results 1 to 3 of 3
  1. #1
    ccarpenter117 is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Storing a txt file into a 2d char array then printing it.

    Okay, so I've been working on this assignment for the past few days trying to figure out why I keep getting errors. I have tried many different ways and I haven't had any luck. The program is supposed to take a text file (they are all ascii images) based on user input and store it into a 2d char array. Then based on how many times the user wants to tile it, it is supposed to print it out that many times. When it comes to outputting the file, I either get the memory locations or an outofbounds exception. I'm not even sure that I am storing the file into the array correctly. Can anyone help? Here's my code:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    class TileMap
    
    {
            public static char response;
            public static int MAXSIDE = 100;
            public static Scanner scan = new Scanner(System.in);
            public static String fileName = "";
            public static int tilesAcross = 0;
            public static int tilesDown = 0;
            public static int imageHeight = 0;
            public static int imageWidth = 0;
            public static char userInput = 0;    
            static char [][] buffer = new char[MAXSIDE][MAXSIDE];
            static FileInputStream fstream = null;
    
    // Start of Main Function
    
        public static void main(String args[])
        {
            TileMap tilemap = new TileMap();
            
            
            System.out.println("Would you like to tile an image in a file?");
            
                if(askPermission(response))
            {
                
                getParameters();
                getImage();
                doTileJob();
                
            }
    
            }
    
    
    
        public static boolean askPermission(char response)
            {
            System.out.println("If yes, type a 'y', else type 'n':");
                response = scan.next().charAt(0);
                
                char y = 0;
                Scanner scan = new Scanner(System.in);
    
                boolean yes = (response == y);
                
                if(yes = true)
                    return true;
                else
                    return false;
            }
    
    
    
    
        public static void getParameters()
        {
                    System.out.println("Please enter the file name: ");
                        fileName = scan.next();    
                    System.out.println("Please enter the number of tiles across you want: ");
                        tilesAcross = scan.nextInt();
                    System.out.println("Please enter the number of tiles down you want: ");
                        tilesDown = scan.nextInt();
        }
    
    
        public static void getImage()
        {
            System.out.println("Getting Image...");
            
            try
            {    
                String line;
                File file = new File(fileName);
                Scanner fstream = new Scanner(file);
                int i = 0;
                imageHeight = fstream.nextInt();
                imageWidth = fstream.nextInt();
                while (fstream.hasNextLine())
                {
                    buffer[i++] = fstream.nextLine().toCharArray();
                }
                fstream.close();
        
            }
            
            
            catch (Exception e)
            {
                System.err.println("Error: " + e.getMessage());
            }
            
            
        }
    
    
    //  This function uses for loops to display the images. The inner most for loop prints one line of the picture.
    
        public static void doTileJob ()
             {
                
                for(int i=0; i < imageHeight; i++)
                {
                    for(int w = 0; w < imageWidth; w++)
                    {
                        for(int t = 0; t < tilesAcross; t++)
                        {
                            for(int a = 0; a < tilesDown; a++)
                            {
                                System.out.print(buffer);
                            }
                        }
                    }
        }
             }
    }
    Last edited by ccarpenter117; 09-19-2013 at 09:09 AM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,712
    Rep Power
    5

    Default Re: Storing a txt file into a 2d char array then printing it.

    - repost the code using code formatting tags (find them in the advanced editor)
    - post the actual error you get
    - be sure to point out which line is causing it

    But in the mean time - WHY do you not know to proceed when you run into problems? You get an OutOfBoundsException you say. Great! Java is telling you what is wrong and where it goes wrong; it points you to the exact line even. You may not be sure what the exception means, but then I have to wonder why you don't go find out.

    Don't be afraid of exceptions - they are your best friends and you are going to see more of them in your life than you will see ants on the floor. Read them, use them to your advantage. And when necessary - research them, figure out under which conditions you get them.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    ccarpenter117 is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: Storing a txt file into a 2d char array then printing it.

    Okay, I fixed the code.
    I am no longer getting any of the out of bound exceptions or null errors. The only issue I am having now is getting the array to print in a block. The way I have it each time the file prints it is printing on top of itself.. anyone have any clue as to why or how I can change it? Here's the updated code:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    class TileMap
    
    {
    		public static boolean yes;
    		public static char response;
    		public static int MAXSIDE = 100;
    		public static Scanner scan = new Scanner(System.in);
    		public static String fileName = "";
    		public static int tilesAcross = 0;
    		public static int tilesDown = 0;
    		public static int imageHeight = 0;
    		public static int imageWidth = 0;
    		public static char userInput = 0;	
    		static char [][] buffer = new char[MAXSIDE][MAXSIDE];
    		static FileInputStream fstream = null;
    
    		
    	public static void main(String args[])
    	{
    		do
    		{
    				askPermission();	
    				getParameters();
    				getImage();
    				doTileJob();
    		}
    		while(yes = true);
    		
    	}
    
    
    	public static boolean askPermission()
            {
    		System.out.println("Would you like to tile an image in a file?");	
    		System.out.println("If yes, type a 'y', else type 'n':");
    			response = scan.next().charAt(0);
    			
    			char y = 0;
    			Scanner scan = new Scanner(System.in);
    
    			boolean yes = (response == y);
    			yes = true;
    			
    			if(yes == true)
    				return true;
    			else
    				return false;
            }
    
    	public static void getParameters()
    	{
    				System.out.println("Please enter the file name: ");
    					fileName = scan.next();	
    				System.out.println("Please enter the number of tiles across you want: ");
    					tilesAcross = scan.nextInt();
    				System.out.println("Please enter the number of tiles down you want: ");
    					tilesDown = scan.nextInt();
    	}
    
    	public static void getImage()
    	{
    		System.out.println("Getting Image...");
    		
    		try
    		{	
    			
    			File file = new File(fileName);
    			Scanner fstream = new Scanner(file);
    			BufferedReader reader = new BufferedReader(new FileReader(fileName));
    			int i = 0;
    			imageHeight = fstream.nextInt();
    			imageWidth = fstream.nextInt();
    			while(fstream.hasNextLine())
    			{
    				String line = fstream.nextLine();
    				
    					for(int l = 0; l < line.length(); l++)
    					{
    						buffer[i][l] = line.charAt(l);
    					}
    				i++;
    			}
    			
    			fstream.close();
    			reader.close();
    		}
    		
    		
    		catch (Exception e)
    		{
    			System.err.println("Error: " + e.getMessage());
    		}
    		
    		
    	}
    
    
    
    //  This function uses for loops to display the images. The inner most for loop prints one line of the picture.
    
    	public static void doTileJob ()
         	{
    			
    			for(int i= 0; i < imageHeight;)
    			{
    				for(int w = 0; w < imageWidth;)
    				{	
    					
    					for(int m = 0; m < tilesDown; m++)
    						{
    						for (int n = 0; n < tilesAcross; n++)
    							{
    							
    							
    							}
    						System.out.print(buffer[w]);
    							System.out.println(buffer[i]);
    						w++; 
    						i++;
    						}
    				}			
    			System.out.println("");
    			}
    			
    	}
         	}
    Last edited by ccarpenter117; 09-19-2013 at 06:51 PM. Reason: figured part of it out.

Similar Threads

  1. reading in file and storing in array
    By cavsfan223 in forum New To Java
    Replies: 1
    Last Post: 11-22-2012, 12:16 AM
  2. read a data file and storing it in an array
    By jwl in forum New To Java
    Replies: 3
    Last Post: 10-27-2012, 12:44 AM
  3. Replies: 2
    Last Post: 03-08-2011, 06:20 AM
  4. Replies: 3
    Last Post: 11-11-2010, 10:22 AM
  5. Reading a txt file and then storing it in a 2d array
    By blkshp1990 in forum New To Java
    Replies: 2
    Last Post: 11-05-2009, 12:31 AM

Tags for this Thread

Posting Permissions

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