Results 1 to 9 of 9
  1. #1
    jmwalloh is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default Scanning text file and inserting contents into array

    Hi, am trying to read a text file using scanner in java and inserting the contents
    into two arrays. One is 1D and the other is 2D.The content of the text file is as
    follows:

    Nancy -1.290 36.820
    Mom -4.040 39.660
    Nelly -0.280 36.070
    Kim -0.090 34.750
    Elly 0.52 35.270
    Tom -1.040 37.090
    Ron -1.140 36.960
    Kelly 1.030 34.990
    Oscar 0.290 34.730


    Here is my code sample which is'nt working:

    Java Code:
    import java.io.File;
        import java.io.FileReader;
        import java.util.Scanner;
        
         
        public class Names {
            public static void main(String[] args) throws Exception {
                        
                File file = null;
                FileReader fr = null;
                Scanner sc = null;
                
                int count = 0;
                int count2 = 0;
                String name[] = new String[25];
                float codes[][] = new float[25][2];
         
                try     {
                    
                    file = new File("namedata.txt");
                    fr = new FileReader(file);           
                    sc = new Scanner(fr);
                    
                    while(sc.hasNext()){
                    	 
                    	 name[count] = sc.next();
                    	 codes[count][count2] = sc.nextFloat();
                    	 codes[count][(count2+1)] = sc.nextFloat();
                    	 count++;
                    	 count2 = 0; //reset count2
                    	System.out.println("Names = "+name[count]+"\n");
                    }
                }catch (Exception ex) {
            	ex.printStackTrace();
            	}finally {
                  fr.close();
                  sc.close();
              } 
            }
        }
    Any help or suggestions are highly welcomed.

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Java Code:
    float codes[][] = new float[25][2];
    is not legit.

    instead, you need to do
    Java Code:
    float codes [][] = new float [25][];
    
    for (int i = 0; i < 25; i ++) {
      codes [i] = new float [2];
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Static allocations can cause lots of issues in runtime. For an example if your file content is more than 25 lines, then how can you read them all?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by iluxa View Post
    Java Code:
    float codes[][] = new float[25][2];
    is not legit.

    instead, you need to do
    Java Code:
    float codes [][] = new float [25][];
    
    for (int i = 0; i < 25; i ++) {
      codes [i] = new float [2];
    }
    So what you are going to solve with this, it's better if you ca explain it. Otherwise OP may confused. :)

  5. #5
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by iluxa View Post
    Java Code:
    float codes[][] = new float[25][2];
    is not legit.
    Can I ask why this isn't legit?

    I have a piece of code that uses
    Java Code:
    public static int[][] cache = new int[numBlocks][blockSize];
    and it works exactly as I want. Your replacement code seems to make a 2D array but give the second dimension no value, then populate the first column with new Arrays (suggesting you don't need the second dimension at all?). I would just like to understand better. Thanks.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yeah, that's why ask the explanation from iluxa. The way you define the array is correct. There are many ways to initialize an arrays in Java. This is one of them.

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Is this specifically an exercise about loading data into arrays? Because if it is not, I would recommend a more object-oriented approach. Let me know if you want more explanation.

    -Gary-

  8. #8
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    I thought as much, thanks for clearing that up. If the OP could post exactly what problem is occuring (what happens when you run it?) it would be easier to help out.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 6
    Last Post: 03-12-2010, 04:54 AM
  2. Replies: 17
    Last Post: 07-13-2009, 05:44 AM
  3. Replies: 1
    Last Post: 07-18-2007, 11:20 PM
  4. Replies: 0
    Last Post: 07-17-2007, 03:30 PM
  5. viewing the contents of a text file in JTextArea
    By warship in forum New To Java
    Replies: 0
    Last Post: 07-17-2007, 02:29 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
  •