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.
7 4510
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.
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 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
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>
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.
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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;
|
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...
|
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...
|
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)
...
|
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...
|
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!
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |