Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Reading from a text file into a two dimensional array

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading from a text file into a two dimensional array

    Hello, this is my first post in this forum. I am a beginner in Java and now I am working on programming Comney's game of life ...
    So I started off by writing the initial patterns in text files and now I am trying to read in the text files into two dimensional array

    I am using these methods currently but they only read in the text into one dimensional array by using getFileSize(); ... how can I retrieve the two dimensions of the text file? what do i change in these three methods that can help me retrieve the number of columns and rows?

    <
           public static void readFile(String[] words, String fileName)throws IOException
          {
             Scanner input = new Scanner(new FileReader(fileName));
             int i=0;									//index for placement in the array
             String line;	
             while (input.hasNextLine())		//while there is another line in the file
             {
                line=input.nextLine();			//read in the next Line in the file and store it in line
                words[i]= line;					//add the line into the array
                i++;									//advance the index of the array         
             }
             input.close();						
          }
     
     
           public static int getFileSize(String fileName)throws IOException
     
          {
             Scanner input = new Scanner(new FileReader(fileName));
             int size=0;
             while (input.hasNextLine())	//while there is another line in the file
             {
                size++;							//add to the size
                input.nextLine();				//go to the next line in the file
             }
             input.close();					//always close the files when you are done
             return size;
          }
     
        //pre: 
         //post:displays all of the elements of the array words
     
           public static void showArray(String[] words)
          {
             for (int i=0; i<words.length; i++)
                System.out.println(words[i] + " ");
             System.out.println();
     
          }
    >
    Last edited by Hayfield; May 13th, 2012 at 11:45 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Reading from a text file into a two dimensional array

    how can I retrieve the two dimensions of the text file?
    Please explain what and where the two dimensions of a text file are.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a text file into a two dimensional array

    The first text file name is "pattern1.txt"
    and this is its content


    ---------------------------------------------------
    ---------------------------------------------------
    ---------------------------------------------------
    ------------------------**-------------------------
    ---------------------***--***----------------------
    ----------------*****--------*****-----------------
    --------------**------------------**---------------
    --------------**------------------**---------------
    ----------------*****--------*****-----------------
    ---------------------***--***----------------------
    ------------------------**-------------------------
    ---------------------------------------------------
    ---------------------------------------------------
    ---------------------------------------------------


    and this is the rest of the code I am trying to modify too

     public static void main(String argv[])throws IOException
          {
     
             int size = getFileSize("patternOptions.TXT");	//holds the # of elements in the file
             String[] list = new String[size];			//array created to file specified size
             //getInfo(list);									//if we wanted the user to input into array
             readFile(list, "patternOptions.TXT");				
             showArray(list);
     
             System.out.println("Enter your choice please");
             int patternNumber = input.nextInt();
     
     
        if(patternNumber==1)
             {
                int size1 = getFileSize("pattern1.TXT");	//holds the # of elements in the file
                String[] list1 = new String[size1];			//array created to file specified size
             //getInfo(list);									//if we wanted the user to input into array
                readFile(list1, "pattern1.TXT");				
                showArray(list1);
                System.out.println(size1);
     
             }
     
             else    // I didn't finish the rest yet

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Reading from a text file into a two dimensional array

    Given that file's contents as input, what do you want in the array(s)?
    What does your current code do? What are your problems?

    Where are the two dimensions?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a text file into a two dimensional array

    I want to input the text file into an array of [26][51] dimensions

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Reading from a text file into a two dimensional array

    Can you describe what goes into each element in the array? If the two dimensions are considered as rows and columns, describe the relationship between each line in the file and each row and column of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a text file into a two dimensional array

    like at list1[0][0] goes "-"
    and like at list1[20][25] goes "*"
    and it goes on in this way

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Reading from a text file into a two dimensional array

    That is code, I'm looking for a description in English of where the characters in the file are to go.
    The file has lines and characters in each line. The array has rows and columns. Explain in those terms.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a text file into a two dimensional array

    I want the characters in the text files to go into the 2D array ..
    for example, the first character in the first line goes into the first column and the first row of the array

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Reading from a text file into a two dimensional array

    What goes in the rest of the first row of the array?
    What goes in the second row of the array?

    Which dimension is the row and which the column? [r][c] or [c][r]
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a text file into a two dimensional array

    ---------------------------------------------------
    this goes into the first row
    the first character goes into the first column ... the second character goes into the second column .. until the last character in the first row which will go in the 51st column ..

    another example ..
    ------------------------**-------------------------
    this goes into the third row
    the first character goes into the first column ... the second character goes into the second column .. until the last character in the first row which will go in the 51st column ..
    so like list1[3][26] is *


    the dimension is [r][c]

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Reading from a text file into a two dimensional array

    Ok, we've got the definitions. Now explain what your problem is.
    Read a row, take the characters one at a time and put them into the array. read next row and do it again. Continue until done.
    Two loops: one for rows, one for characters in that row
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a text file into a two dimensional array

    Ok now I get the idea
    so I have this piece of code that counts the number of rows

     
    public static int getFileSize(String fileName)throws IOException
     
          {
             Scanner input = new Scanner(new FileReader(fileName));
             int size=0;
             while (input.hasNextLine())	//while there is another line in the file
             {
                size++;							//add to the size
                input.nextLine();				//go to the next line in the file
             }
             input.close();					//always close the files when you are done
             return size;
          }


    but how do I count the characters in the first row?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Reading from a text file into a two dimensional array

    If the row/line is read into a String variable, the String class has methods that will tell you how many characters are in the String.
    Last edited by Norm; May 13th, 2012 at 04:37 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a text file into a two dimensional array

    ok so I modified the method of reading the file into this

     
           public static void readFile(String[][] words, String fileName)throws IOException
          {
             Scanner input = new Scanner(new FileReader(fileName));
             int r=0;
             int c=0;									//index for placement in the array
             String dash;	
             while (input.hasNextLine())		//while there is another line in the file
             {
                while(input.hasNextChar())
                {
                   dash=input.nextChar();			//read in the next Line in the file and store it in line
                   words[r][c]= dash;	
                }		
                input.nextLine();		//add the line into the array
                r++;									//advance the index of the array         
             }
             input.close();						
          }

    but obviously it wouldn't compile because there isn't anything such .nextChar but can you tell me an alternative that I can use to know if there are more character in a line?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Reading from a text file into a two dimensional array

    Did you look at what I suggested before?
    If the line has been read into a String, all the characters from the line are in the String. There are no more.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to read a 2 dimensional text file from an array?
    By seaofFire in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 9th, 2012, 07:44 AM
  2. Not Reading From Text File
    By JavaLaxer15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2012, 11:16 AM
  3. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 08:13 PM
  4. Reading lines of a text file into a string array
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 11th, 2010, 10:56 AM
  5. How to write 2 dimensional array of float numbers to binary file?
    By Ghuynh in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 17th, 2010, 04:26 PM