469,358 Members | 1,605 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

Reading a file into arrays

hi, im having some trouble reading a file into java and then storing it in an array here the code i have so far:

Expand|Select|Wrap|Line Numbers
  1. import java.io.FileNotFoundException;
  2. import java.io.FileReader;
  3. import java.util.Scanner;
  4. import javax.swing.JOptionPane;
  5.  
  6. public class samplecode {
  7.  
  8.  
  9.     int i = 0;
  10.     int j = 0;
  11.  
  12.     public static void main(String[] args) {
  13.  
  14.     double [][] data = new double [24][4]; //declaring array
  15.  
  16.     Scanner sc = new Scanner("sampledata.txt");//reading in data
  17.  
  18.         while(sc.hasNextDouble()){ //reading data from file
  19.              sc.nextDouble();
  20.  
  21.  
  22.          while ( j < 24){ //outer loop for columns
  23.  
  24.         while (i <4){ //inner loop for rows
  25.         i++;
  26.         }
  27.             j++;
  28.  
  29.             }
  30.  
  31.         }
  32.  
  33.     }
  34.  
  35. }
thanks

also it says that i can't make a static referance to a non-static field j and i. i dont understand what this means.
Nov 8 '07 #1
7 4084
r035198x
13,262 8TB
hi, im having some trouble reading a file into java and then storing it in an array here the code i have so far:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class samplecode {


int i = 0;
int j = 0;

public static void main(String[] args) {

double [][] data = new double [24][4]; //declaring array

Scanner sc = new Scanner("sampledata.txt");//reading in data

while(sc.hasNextDouble()){ //reading data from file
sc.nextDouble();


while ( j < 24){ //outer loop for columns

while (i <4){ //inner loop for rows
i++;
}
j++;

}

}

}

}
thanks

also it says that i can't make a static referance to a non-static field j and i. i dont understand what this means.
1.) Please use code tags when posting code.
2.) Read this article.
Nov 8 '07 #2
Ganon11
3,652 Expert 2GB
I see you reading data just fine with a loop involving your scanner. I also see two nested while loops that will properly iterate through a double[][] array. What I don't see is:

1) any use of the array inside your loops.
2) you reading the doubles from your Scanner into anything.

All your second set of loops does is increment i and j. Oh, and since you never reset j's value, the second time your code gets to that loop, the check will fail immediately. This is why for loops are usually used to iterate over arrays.

Basically, you've got all the pieces there - why haven't you put them all together?
Nov 8 '07 #3
I see you reading data just fine with a loop involving your scanner. I also see two nested while loops that will properly iterate through a double[][] array. What I don't see is:

1) any use of the array inside your loops.
2) you reading the doubles from your Scanner into anything.

All your second set of loops does is increment i and j. Oh, and since you never reset j's value, the second time your code gets to that loop, the check will fail immediately. This is why for loops are usually used to iterate over arrays.

Basically, you've got all the pieces there - why haven't you put them all together?
I'm not sure how to put them together. How do I get the file into the array? I'm trying to use the i and j to put the information in to the array. The file is made up of user ids, item ids and the rating the user has for each item. The file starts like this: 1 1 1 881250949
1 2 5 881250949
1 4 2 881250949 ect
Nov 8 '07 #4
r035198x
13,262 8TB
I'm not sure how to put them together. How do I get the file into the array? I'm trying to use the i and j to put the information in to the array. The file is made up of user ids, item ids and the rating the user has for each item. The file starts like this: 1 1 1 881250949
1 2 5 881250949
1 4 2 881250949 ect
Why not have a User class with user id and Map <Integer (itemID), Integer(rating)> as attributes.
Then you can read the file data into User objects that you store in an ArrayList<User>
Nov 8 '07 #5
Why not have a User class with user id and Map <Integer (itemID), Integer(rating)> as attributes.
Then you can read the file data into User objects that you store in an ArrayList<User>
That sound really hard. We've havn't done anything like that in our lectures.
Nov 8 '07 #6
r035198x
13,262 8TB
That sound really hard. We've havn't done anything like that in our lectures.
No it's not. 90% of learning Java should be learning good design. I'm not saying the approach I suggested is the best approach, but I'm showing you some clean ways of solving programs using Java.
The User class is not difficult to make (surely you've done classes before?). Start with that. Help is always available here.
The only "new" thing you might learn is how to use the Map but that is point of assignments anyway.
Nov 8 '07 #7
No it's not. 90% of learning Java should be learning good design. I'm not saying the approach I suggested is the best approach, but I'm showing you some clean ways of solving programs using Java.
The User class is not difficult to make (surely you've done classes before?). Start with that. Help is always available here.
The only "new" thing you might learn is how to use the Map but that is point of assignments anyway.

Cool, il try that so. Thanks
Nov 8 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Christian Christmann | last post: by
6 posts views Thread by guillaume | last post: by
13 posts views Thread by mloichate | last post: by
6 posts views Thread by arne.muller | last post: by
10 posts views Thread by Tyler | last post: by
16 posts views Thread by Ron | last post: by
7 posts views Thread by ianenis.tiryaki | last post: by
21 posts views Thread by Stephen.Schoenberger | last post: by
2 posts views Thread by Compass | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.