community,
I am currently stuck on a homework assignment which requires me to read a text file and then make further calculations with it, my questions are now.
How do I read a text file and store the columns in several arrays to make further calculations?
I have written a piece of code which already reads the file, but doesn't seem to store the columns in arrays as I keep getting "ArrayOutOfBoundsException".
I have already tried the given code below, which doesn't give me what I need.
Unfortunately, I haven't found anything that was able to solve the problem.
The columns are separated by Tabs.
This is what the table looks like
Date Open High Low Close Volume
29.12.2017 12.980,09 12980.74 12.911,773 12.917.64 1.905.806.208
....
....
....
and so on
try {
ArrayList < String > KursDaten = new ArrayList<String>();
String Zeile = null;
while ((Zeile = in.readLine()) != null) {
Zeile = Zeile.replaceAll("\u0000", "");
Zeile = Zeile.replaceAll("�", "");
String[] columns = in.readLine().split(" ");
columns = in.readLine().split("\t");
String data = columns[columns.length - 1];
KursDaten.add(data);
out.println(Zeile);
}
int size = KursDaten.size();
out.println(KursDaten.size());
String[] arr1 = new String[size];
String[] arr2 = new String[size];
String[] arr3 = new String[size];
String[] arr4 = new String[size];
String[] arr5 = new String[size];
String[] arr6 = new String[size];
for (int i = 0; i < size; i++) {
String[] temp = KursDaten.get(i).split("\t\t");
temp = KursDaten.get(i).split("\t");
arr1[i] = temp[0];
arr2[i] = temp[1];
arr3[i] = temp[2];
arr4[i] = temp[3];
arr5[i] = temp[4];
arr6[i] = temp[5];
System.out.println(Arrays.toString(arr1));
}
}
catch (NullPointerException e) {
}
I expect the columns of the list to be stored in the different arrays, with which I then can make further calculations as needed.
I have a problem with my code, because it throws NullPointerException when I try to assign string from arraylist to array.
String[][] data = new String[idList.size()][];
for(int i = 1; i<=idList.size(); i++) {
data[i][0] = idList.get(i);
data[i][1] = nameList.get(i);
data[i][2] = hList.get(i);
data[i][3] = sList.get(i);
data[i][4] = fList.get(i);
data[i][5] = mList.get(i);
data[i][6] = osList.get(i);
data[i][7] = tsList.get(i);
data[i][8] = podList.get(i);
data[i][9] = pacList.get(i);
}
Can someone please tell me how do I fix that?
data[i] is null, since you initialized data to contain idList.size() null references of type String[].
Change
String[][] data = new String[idList.size()][];
to
String[][] data = new String[idList.size()][10];
which will initialize data to contain idList.size() references to a String array of length 10 (String[10]).
I suggest to define the array properly (in both dimensions )
replace this
new String[idList.size()][];
for something like
new String[idList.size()][10]; // you have 10 different lists there...
and iterate in the for loop with zero base...
Example:
String[][] data = new String[idList.size()][10];
for (int i = 0; i < idList.size(); i++) {
data[i][0] = idList.get(i);
data[i][1] = idList.get(i);
data[i][2] = idList.get(i);
data[i][3] = idList.get(i);
}
I don't really understand why this is happening. This is, by far, the weirdest error I've never come across.
The thing is I'm retrieving some info from a Server using Retrofit, and for that, I needed to create a POJO called POJO_PATIENTINFO. Inside this pojo, I have a BigDataset_PatientInfo variable called data. Inside this variable, I am storing a List<Dataset_RegVar> called registro_variable, inside of which I have these strings:
registro_var_id
registro_var_fecha
registro_var_descripcion
registro_var_variable
registro_var_medida
registro_var_comentario
No problems with that. The idea is that I want to recover these Strings from another Fragment.class, easy task I thought.
This is my piece of code for doing such an easy task:
int size = POJOS.getPojo_patientInfo().data.registro_variable.size();
int i = 0;
strVariable = strId = strFecha = strMedida = strDescripcion = strComentarios = new String[size];
for (POJO_PATIENTINFO.Dataset_RegVar dataset : POJOS.getPojo_patientInfo().data.registro_variable) {
strVariable[i] = dataset.registro_var_variable;
strId[i] = dataset.registro_var_id;
strFecha[i] = dataset.registro_var_fecha;
strMedida[i] = dataset.registro_var_medida;
strDescripcion[i] = dataset.registro_var_descripcion;
strComentarios[i] = dataset.registro_var_comentario;
i++;
}
After the first iteration, just before the "i++;" line executes, the String arrays are all storing exactly the same value, that is, the same value as dataset.registro_var_comentario. In a nutshell, the aforementioned piece of code is execute as if I was writing:
int size = POJOS.getPojo_patientInfo().data.registro_variable.size();
int i = 0;
strVariable = strId = strFecha = strMedida = strDescripcion = strComentarios = new String[size];
for (POJO_PATIENTINFO.Dataset_RegVar dataset : POJOS.getPojo_patientInfo().data.registro_variable) {
strVariable[i] = dataset.registro_var_comentario;
strId[i] = dataset.registro_var_comentario;
strFecha[i] = dataset.registro_var_comentario;
strMedida[i] = dataset.registro_var_comentario;
strDescripcion[i] = dataset.registro_var_comentario;
strComentarios[i] = dataset.registro_var_comentario;
i++;
}
Obviously, the first thing that came to my mind is that maybe all of these strings were holding the same value. No, they are not. I know this because I did some debugging work on my own. Let me paste some results (note that these images are for i=3): http://postimg.org/gallery/2gf1lj7qa/
I will provide more details if you think they are necessary. Thanks.
You are assigning one and the same String array to all those variables. So the last write to any of these variables overwrites the corresponding index in all the other variables aswell. And this happens to be this assignment:
strComentarios[i] = dataset.registro_var_comentario;
So instead of
strVariable = strId = strFecha = strMedida = strDescripcion = strComentarios = new String[size];
you need to write
strVariable = new String[size];
strId = new String[size];
strFecha = new String[size];
strMedida = new String[size];
strDescripcion = new String[size];
strComentarios = new String[size];
Funny reason: You are using same memory location for all the string array.
Why you are getting values of strComentarios in every array: Because a new memory location is assigned to it and you are using same memory location for other array. So whatever is updated in strComentarios, all other array get that value.
strVariable = strId = strFecha = strMedida = strDescripcion = strComentarios = new String[size];
Split it like this
strVariable = new String[size];
strId = new String[size];
strFecha = new String[size];
strMedida = new String[size];
strDescripcion = new String[size];
strComentarios = new String[size];
So I have a text file with items like look like this:
350279 1 11:54 107.15
350280 3 11:55 81.27
350281 2 11:57 82.11
350282 0 11:58 92.43
350283 3 11:59 86.11
I'm trying to create arrays from those values, in which the first values of each line are in an array, the second values of each line are in an array, and so on.
This is all the code I have right now, and I can't seem to figure out how to do it.
package sales;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Sales {
public static void main (String[] args) throws FileNotFoundException {
Scanner reader = new Scanner(new File("sales.txt"));
int[] transID = new int[reader.nextInt()];
int[] transCode = new int[reader.nextInt()];
String[] time = new String[reader.next()];
double[] trasAmount = new double[reader.hasNextDouble()];
}
}
It's difficult to build an array this way, because Arrays have fixed size... you need to know how many elements they have. If you use a List instead, you don't have to worry about knowing the number of elements in advance. Try this (note: there is no error checking here!):
public static void main (String[] args) throws FileNotFoundException {
Scanner reader = new Scanner(new File("sales.txt"));
List<Integer> ids = new LinkedList<>();
List<Integer> codes = new LinkedList<>();
List<String> times = new LinkedList<>();
List<Double> amounts = new LinkedList<>();
// Load elements into Lists. Note: you can just use the lists if you want
while(reader.hasNext()) {
ids.add(reader.nextInt());
codes.add(reader.nextInt());
times.add(reader.next());
amounts.add(reader.nextDouble());
}
// Create arrays
int[] idArray = new int[ids.size()];
int[] codesArray = new int[codes.size()];
String[] timesArray = new String[times.size()];
double[] amountsArray = new double[amounts.size()];
// Load elements into arrays
int index = 0;
for(Integer i : ids) {
idArray[index++] = i;
}
index = 0;
for(Integer i : codes) {
codesArray[index++] = i;
}
index = 0;
for(String i : times) {
timesArray[index++] = i;
}
index = 0;
for(Double i : ids) {
amountsArray[index++] = i;
}
}
Use Array list because Arrays have fixed size and using Arraylist you add the elements dynamically
Scanner reader = new Scanner(new File("test.txt"));
List<Integer> transID = new ArrayList<Integer>();
List<Integer> transCode = new ArrayList<Integer>();
List<String> time= new ArrayList<String>();
List<Double> trasAmount = new ArrayList<Double>();
while(reader.hasNext() )
{
transID.add(reader.nextInt());
transCode.add(reader.nextInt());
time.add(reader.next());
trasAmount.add(reader.nextDouble());
}
System.out.println(transID.toString());
System.out.println(transCode.toString());
System.out.println(time.toString());
System.out.println(trasAmount.toString());
Output of the above code
transID [350279, 350280, 350281, 350282, 350283]
transCode [1, 3, 2, 0, 3]
time [11:54, 11:55, 11:57, 11:58, 11:59]
trasAmount [107.15, 81.27, 82.11, 92.43, 86.11]
You'll need a while loop to check for input. Since not all inputs are integers you might do something like:
while(reader.hasNextLine()){ //checks to make sure there's still a line to be read in the file
String line=reader.nextLine(); //record that next line
String[] values=line.split(" "); //split on spaces
if(values.length==4){
int val1=Integer.parseInt(values[0]); //parse values
int val2=Integer.parseInt(values[1]);
String val3=values[2];
double val4=Double.parseDouble(values[3]);
//add these values to your arrays. Might have to "count" the number of lines on a first pass and then run through a second time... I've been using the collections framework for too long to remember exactly how to work with arrays in java when you don't know the size right off the bat.
}
}
In addition to my comment here are 3 ways how you cant do it
read into single arrays
int size = 2;
// first allocate some memory for each of your arrays
int[] transID = new int[size];
int[] transCode = new int[size];
String[] time = new String[size];
double[] trasAmount = new double[size];
Scanner reader = new Scanner(new File("sales.txt"));
// keep track of how many elements you have read
int i = 0;
// start reading and continue untill there is no more left to read
while(reader.hasNext()) {
// since array size is fixed and you don't know how many line your file will have
// you have to reallocate your arrays when they have reached their maximum capacity
if(i == size) {
// increase capacity by 5
size += 5;
// reallocate temp arrays
int[] tmp1 = new int[size];
int[] tmp2 = new int[size];
String[] tmp3 = new String[size];
double[] tmp4 = new double[size];
// copy content to new allocated memory
System.arraycopy(transID, 0, tmp1, 0, transID.length);
System.arraycopy(transCode, 0, tmp2, 0, transCode.length);
System.arraycopy(time, 0, tmp3, 0, time.length);
System.arraycopy(trasAmount, 0, tmp4, 0, trasAmount.length);
// reference to the new memory by your old old arrays
transID = tmp1;
transCode = tmp2;
time = tmp3;
trasAmount = tmp4;
}
// read
transID[i] = Integer.parseInt(reader.next());
transCode[i] = Integer.parseInt(reader.next());
time[i] = reader.next();
trasAmount[i] = Double.parseDouble(reader.next());
// increment for next line
i++;
}
reader.close();
for(int j = 0; j < i; j++) {
System.out.println("" + j + ": " + transIDList.get(j) + ", " + transCodeList.get(j) + ", " + timeList.get(j) + ", " + trasAmountList.get(j));
}
as you see this is a lot of code.
Better you use lists so get rid of the overhead of reallocation and copying (at leas in your own code)
read into single lists
// instanciate your lists
List<Integer> transIDList = new ArrayList<>();
List<Integer> transCodeList = new ArrayList<>();
List<String> timeList = new ArrayList<>();
List<Double> trasAmountList = new ArrayList<>();
reader = new Scanner(new File("sales.txt"));
int i = 0;
while(reader.hasNext()) {
// read
transIDList.add(Integer.parseInt(reader.next()));
transCodeList.add(Integer.parseInt(reader.next()));
timeList.add(reader.next());
trasAmountList.add(Double.parseDouble(reader.next()));
i++;
}
reader.close();
for(int j = 0; j < i; j++) {
System.out.println("" + j + ": " + transIDList.get(j) + ", " + transCodeList.get(j) + ", " + timeList.get(j) + ", " + trasAmountList.get(j));
}
You see here how small the code went? But but it still can get better...
A line in the sales.txt file seem to constitute data elements of some entity, why not put them in an object ? for that you may write a class named Trans, some think like this:
class Trans {
public int transID;
public int transCode;
public String time;
public double trasAmount;
#Override
public String toString() {
return transID + ", " + transCode + ", " + time + ", " + trasAmount;
}
}
Then you can use this class to hold the data you read from your file and put each object of that class in a list.
reading into a list of objects
reader = new Scanner(new File("sales.txt"));
List<Trans> transList = new ArrayList<>();
int i = 0;
while(reader.hasNext()) {
Trans trans = new Trans();
trans.transID = Integer.parseInt(reader.next());
trans.transCode = Integer.parseInt(reader.next());
trans.time = reader.next();
trans.trasAmount = Double.parseDouble(reader.next());
transList.add(trans);
i++;
}
reader.close();
for(Trans trans : transList) {
System.out.println("" + i++ + ": " + trans);
}
Output of all 3 methods
0: 350279, 1, 11:54, 107.15
1: 350280, 3, 11:55, 81.27
2: 350281, 2, 11:57, 82.11
3: 350282, 0, 11:58, 92.43
4: 350283, 3, 11:59, 86.11
Here is a sample code to read the values from the file and write into an array. Sample code has logic for int array, you can replicate it for other array types as well.
package sales;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Sales {
public static void main (String[] args) throws IOException {
FileInputStream fstream = new FileInputStream("sales.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] tokens = strLine.split(" ");
int[] transID = convertStringToIntArray(tokens[0]);
for(int i = 0 ; i < transID.length ; i++ )
System.out.print(transID[i]);
}
}
/** function to convert a string to integer array
* #param str
* #return
*/
private static int[] convertStringToIntArray(String str) {
int intArray[] = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
intArray[i] = Character.digit(str.charAt(i), 10);
}
return intArray;
}
}
I have an array that is from .split command and want to put it into an array called String[][] datatabvars, I do not know how to turn datatabvars into a two dimensional array and put the data into it.
public String[] getList() {
String file_name = "path";
String[] links = null;
String[][] datatabvars = null; // this var
int numberOfDatatabs = 0;
try {
ReadFile file = new ReadFile(file_name);
String[] aryLines = file.OpenFile();
int i;
for(i=0; i < aryLines.length; i++) { //aryLines.length
if (aryLines[i].substring(0, 7).equals("datatab")) {
aryLines[i] = aryLines[i].replace("datatab["+Integer.toString(numberOfDatatabs)+"] = new Array(", "");
aryLines[i] = aryLines[i].replace(");", "");
datatabvars = aryLines[i].split(","); // this split array
numberOfDatatabs++;
}
}
System.out.println(datatabvars[0]);
}catch (IOException e) {
System.out.println( e.getMessage() );
}
return links;
}
Update the two lines(I added comment) as below: (I am assuming that rest of your code is working)
String[][] datatabvars = null; // this var
int numberOfDatatabs = 0;
try {
ReadFile file = new ReadFile(file_name);
String[] aryLines = file.OpenFile();
datatabvars = new String[aryLines.length][]; // INITIALIZED
int i;
for(i=0; i < aryLines.length; i++) { //aryLines.length
if (aryLines[i].substring(0, 7).equals("datatab")) {
aryLines[i] = aryLines[i].
replace("datatab["+Integer.toString(numberOfDatatabs)+"] =
new Array(", "");
aryLines[i] = aryLines[i].replace(");", "");
datatabvars[i] = aryLines[i].split(","); // this split array: ASSIGNED
numberOfDatatabs++;
}
}
System.out.println(datatabvars[0]);
In general, arrays are to avoided like the plague - use collections if possible:. In this case, split() returns a String[], so use that, but use List<String[]> to store multiple String[]:
List<String[]> datatabvars = new ArrayList<String[]>();
...
String[] array = input.split(",");
datatabvars.add(array);
You find life is much easier using collections than arrays.