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 11 of 11

Thread: Loading an array from a .txt file

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loading an array from a .txt file

    First post I am making here and hopefully it will be a good one.

    I have the following code:

    // File:  Array07.java
     
    /*  *********************************************
        The program creates a file based on the random values
        generated for an array..
     
    	********************************************* */
    import java.util.Scanner;
    import java.io.*;
     
    public class Array07
    {
     
        public static void main(String[ ] args)
    	    throws java.io.IOException
    	{
     
    		String endMsg = "***** end of Array07 *****";
     
    		FileReader arrayIn = new FileReader("Array07Data.txt");
    		int [] [] array07 = new int [7] [8];
    		int i = 0;
    		int j = 0;
    		Scanner inData = new Scanner(arrayIn);
    /*
    		[B]Develop the code to load the array
    		from the file Array07.txt. In order for the for loop
    		below to work properly, the code to fill the array
    		should be placed here...[/B]*/
     
    		for(i = 0; i < array07.length; i++) {
    			for(j = 0; j < array07[i].length; j++) {
    				System.out.printf("%5d",array07[i] [j]);
    			}
    			System.out.println();
    		}
    		arrayIn.close();
    		System.out.println(endMsg);
        } // end of method main
     
    } // end of Array07 class

    I have highlighted in bold what I am asked to do. I am supposed to load the array in the file "Array07Data.txt". The "Array07Data.txt" contains the following array:

    1422
    927
    259
    400
    1170
    356
    1193
    876
    114
    1454
    286
    1237
    1406
    237
    32
    577
    921
    1407
    692
    656
    1002
    365
    1191
    1136
    1382
    196
    783
    1454
    712
    607
    243
    1113
    185
    1438
    174
    385
    258
    1002
    593
    727
    1483
    1386
    782
    626
    973
    173
    945
    633
    986
    657
    993
    479
    84
    673
    781
    914


    How do I load this into the program? Running the program as it is, it just gives me an array of zeros. It's probably simple stuff but to me it isn't.
    Last edited by Norm; December 3rd, 2013 at 06:59 PM. Reason: changed quote to code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Loading an array from a .txt file

    Where does the code assign any values to the array?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Loading an array from a .txt file

    Quote Originally Posted by NEPALII View Post
    How do I load this into the program? Running the program as it is, it just gives me an array of zeros.
    Just only
    Scanner inData = new Scanner(arrayIn);

    does not read any file content. You have to read each number token using (presumably, being int values) nextInt() of Scanner, and assign these values to array elements.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loading an array from a .txt file

    Quote Originally Posted by andbin View Post
    Just only
    Scanner inData = new Scanner(arrayIn);

    does not read any file content. You have to read each number token using (presumably, being int values) nextInt() of Scanner, and assign these values to array elements.
    So I have to put Scanner nextInt() in order to read file content? I'm confused.

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Loading an array from a .txt file

    Quote Originally Posted by NEPALII View Post
    So I have to put Scanner nextInt() in order to read file content? I'm confused.
    The main purpose of Scanner is to read "tokens", by default separated by any number of "whitespaces" (also end-of-lines). In your file you have many integer tokens. Thus, you have to invoke many times nextInt() to read each single integer token.

    In your initial code, the double-for loop to print the array is correct. Do a similar double-for loop to read integers and assign them into the array.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loading an array from a .txt file

    I have no clue on how to get a loop to read integers and assign them into the array. That double for loop, like you said, prints out the array into the program, right? Sorry to annoy you with this.

  7. #7
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Loading an array from a .txt file

    Quote Originally Posted by NEPALII View Post
    I have no clue on how to get a loop to read integers and assign them into the array. That double for loop, like you said, prints out the array into the program, right?
    Yes, the double-for loop you have already written is correct but prints only the content of the matrix.
    It would be possible to read each integer into that loop, just before the System.out.printf. But the matrix would be useless (you read and print immediately a value, where is the meaning of the matrix?).

    So before that double-loop, make a pratically equal loop, with one difference: instead to print, invoke nextInt() on the Scanner and assign the return value to the array element at the row/column i,j.


    P.S. It's better to close the Scanner ... not the FileReader.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  8. #8
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loading an array from a .txt file

    Something like this?

    Scanner int x = x.next.Int();
    Scanner int Array07Data = Array07Data.next.Int();

    I used the x as an example. My professor isn't clear on explaining this to me or the rest of the class. It's funny how most of us have to search on the internet for answers. I have managed so far with most of the stuff, but this is getting me lost completely.

  9. #9
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Loading an array from a .txt file

    Quote Originally Posted by NEPALII View Post
    Something like this?

    Scanner int x = x.next.Int();
    Scanner int Array07Data = Array07Data.next.Int();
    No, both are wrong (for syntax and meaning).

    This reads 1 integer token (where inData is the reference to your Scanner instance):

    int value = inData.nextInt();

    While the following fills an int[] (monodimensional):

    int[] arr = new int[10];
     
    for (int i = 0; i < arr.length; i++) {
        arr[i] = inData.nextInt();
    }

    Your case is slightly different: you have a bidimensional array. Thus, do a double-for loop and into this loop read the integer token and assign the value to the array element at row/column i,j.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Loading an array from a .txt file

    @micronome Please don't spoonfeed code.
    See: http://www.javaprogrammingforums.com...n-feeding.html
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loading an array from a .txt file

    Quote Originally Posted by andbin View Post
    No, both are wrong (for syntax and meaning).

    This reads 1 integer token (where inData is the reference to your Scanner instance):

    int value = inData.nextInt();

    While the following fills an int[] (monodimensional):

    int[] arr = new int[10];
     
    for (int i = 0; i < arr.length; i++) {
        arr[i] = inData.nextInt();
    }

    Your case is slightly different: you have a bidimensional array. Thus, do a double-for loop and into this loop read the integer token and assign the value to the array element at row/column i,j.
    Sorry I didn't say anything. I was able to get the numbers from the .txt into my array. Thank you very much for the help. Here is the code I got:

    for(i = 0; i < array07.length; i++) {
    for(j = 0; j < array07[i].length; j++) {
    array07[i][j] = inData.nextInt();

    It worked like this. Again, thank you.

Similar Threads

  1. Txt file to array
    By cywfong in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 25th, 2013, 02:46 PM
  2. [SOLVED] Array Population via .txt File
    By Blackrabbitjack in forum What's Wrong With My Code?
    Replies: 18
    Last Post: March 19th, 2012, 10:07 AM
  3. Building an array from .txt file.
    By brudley5 in forum Object Oriented Programming
    Replies: 3
    Last Post: December 4th, 2011, 09:14 PM
  4. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  5. loading things from a text file into an array list
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 6th, 2011, 03:32 PM