Hi everyone,
I'm trying to make a little applet that reads text files. I was planning to load some data into a class and use it later. However, I seem to having a few problems with it.
// DataSet.java
package com.someone.something;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DataSet {
private String[] header;
private List<String[]> data;
private String[] tempArray;
private int cCounter = 0;
private int rCounter = 0;
private String[][] finishedData;
private boolean finished = false;
public DataSet(int columns){
data = new ArrayList<String[]>();
tempArray = new String[columns];
Arrays.fill(tempArray,"");
}
public void nextRow(){
data.add(rCounter,tempArray); // Seems that this is the problem
Arrays.fill(tempArray,"");
cCounter = 0;
rCounter++;
}
public void add(String k){
tempArray[cCounter] = k;
cCounter++;
finished = false;
}
public String[][] getData(){
if(!finished){
finish();
}
return finishedData;
}
public void finish(){
finishedData = data.toArray(new String[data.size()][]);
finished = true;
}
}
This is the part that uses the DataSet class:
// Inside of the main class
DataSet dataSet;
String[][] data;
ArrayList<String[]> dataList;
dataList = new ArrayList<String[]>();
String[] a = {"aaa","aab","aac","aad","aae"};
String[] b = {"bbb","bba","bbc","bbd","bbe"};
String[] c = {"ccc","cca","ccb","ccd","cce"};
dataSet = new DataSet(5);
for(String value : a){
dataSet.add(value);
}
dataSet.nextRow();
for(String value : b){
dataSet.add(value);
}
dataSet.nextRow();
for(String value : c){
dataSet.add(value);
}
dataSet.nextRow();
data = dataSet.getData();
String bufferedString = "";
data = dataSet.getData();
for(String[] rows : data){
bufferedString = "";
for(int i = 0; rows.length > i; i++){
bufferedString += rows[i];
if(rows.length - 1 != i){
bufferedString += "\t";
}
}
System.out.println(bufferedString);
}
When I run this example, I would get 5 tabs per line for 3 lines. When I tested the data that went into DataSet.Data
in DataSet.nextRow()
, every row seems to be the same instead of writing to the next index.
Can anyone help me? Thanks