By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,166 Members | 1,775 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,166 IT Pros & Developers. It's quick & easy.

Reading a file into arrays

P: 4
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
Share this Question
Share on Google+
7 Replies


10K+
P: 13,264
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
Expert 2.5K+
P: 3,652
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

P: 4
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

10K+
P: 13,264
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

P: 4
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

10K+
P: 13,264
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

P: 4
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.