473,390 Members | 1,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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 4518
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

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

Similar topics

2
by: Christian Christmann | last post by:
Hi, I have a file which has a bunch of entries (student's data). Its structure looks like Number1|ID|name|surname|emailaddress As can be seen, the entries are seperated by a "|". Now I...
6
by: guillaume | last post by:
I have to read and process a large ASCII file containing a mesh : a list of points and triangles. The file is 100 MBytes. I first tried to do it in memory but I think I am running out of memory...
8
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way...
13
by: mloichate | last post by:
I must read a very heavy-weight text plain file (usually .txt extension) )and replace a given character with another given character in all text inside the file. My application was working pretty...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
16
by: Ron | last post by:
Hello everyone, I've created a functioning ATM program, a bank machine. Now I want to implement usernames and Pins into it. So I have a text file with this info. BILL, 1111 TOM, 2222...
7
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
2
by: Compass | last post by:
Hi all, I have an int array in a text file. The file structure is like this: , , , ] How can I easily read them in to three int arrays? Thanks a lot!
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.