Write columns vertically instead of horizontally - java

I am using opencsv for getting values out of multiple csv sheets to write them into one csv sheets. This is what I am doing:
//reading all entries in a huge list
for (int j = 0; j < (fileList.size() - 740); j++) {
String csvFile = "C:\\" + fileList.get(j);
reader = new CSVReader(new FileReader(csvFile), ';');
hugeList = reader.readAll();
List<String[]> data = new ArrayList<String[]>();
List<String> tmp= new ArrayList<String>();
for(int m = 0; m < hugeList.size(); m++) {
String[] values = hugeList.get(m);
tmp.add(values[0]);
}
data.add(tmp.toArray(new String[0]));
writer.writeAll(data);
}
As you can see I am getting the file and write its content into a list(hugeList) and then mapping each value on a new data array which I am writing into my new sheet. The problem is I am getting the data in a row and not in a column:
How to write my data column-by-column? What is wrong in my algorithm?
I appreciate your reply!

What is wrong here is very simple: you must write a every entry as new line
E.g.: writer.writeNext(data);
See example below for more details
My Approach for a result in Column
for (int j = 0; j < fileList.size(); j++) {
String csvFile = readPath + fileList.get(j);
System.out.println("Read: " + csvFile);
reader = new CSVReader(new FileReader(csvFile), ';');
hugeList = reader.readAll();
String[] data = new String[1];
for (int m = 0; m < hugeList.size(); m++) {
String[] values = hugeList.get(m);
data[0] = values[0];
writer.writeNext(data);
}
}

Related

How can i make my turn my text file to 40x40 matrix [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
In my code i need to read my text file and create a 40x40 matrix however my array only reads the first line Here is my code;
String worldData = "world.txt";
File worldFile = new File(worldData);
int[][] worldArray = new int[40][40];
Scanner scanner = new Scanner(worldFile);
while (scanner.hasNextLine()) {
String allText = scanner.nextLine();
String[] allLines = allText.split(";");
for (int i = 0; i < worldArray.length; i++) {
for (int j = 0; j < worldArray[0].length; j++) {
worldArray[i][j] = Integer.parseInt(allLines[0]);
}
}
I hope the in-line comment may give you some hints:
while (scanner.hasNextLine()) {
// you read a single line once in the while loop
String allText = scanner.nextLine();
String[] allLines = allText.split(";");
// here, for every new coming line, the for loop starts
// from array[0][0], therefore, it overwrites all the existing
// data. Thus, finally, you have only the last line in your array.
for (int i = 0; i < worldArray.length; i++) {
for (int j = 0; j < worldArray[0].length; j++) {
worldArray[i][j] = Integer.parseInt(allLines[0]);
}
}
Assuming that every line contains a row of the world matrix, the for-i loop should read exactly one line.
Scanner scanner = new Scanner(worldFile);
for (int i = 0; i < worldArray.length; i++) {
if (!scanner.hasNextLine()) {
throw new IllegalArgumentException("There are only " + i
+ " lines of the 40 needed.");
}
String line = scanner.nextLine();
String[] cells = line.split(";");
if (cells.length != 40) {
throw new IllegalArgumentException("There are " + i
+ " cells instead of the 40 needed.");
}
for (int j = 0; j < worldArray[0].length; j++) {
worldArray[i][j] = Integer.parseInt(cells[j]);
}
}
Alternatively you can do without a Scanner:
String worldData = "world.txt";
Path worldFile = Paths.get(worldData);
List<String> lines = Files.readAllLines(worldFile, StandardCharsets.UTF_8);
if (lines.size() < 40) {
throw new IllegalArgumentException("There are only "
+ lines.size()
+ " lines of the 40 needed.");
}
for (int i = 0; i < worldArray.length; i++) {
String line = lines.get(i);
String[] cells = line.split(";");
if (cells.length != 40) {
throw new IllegalArgumentException("There are " + i
+ " cells instead of the 40 needed.");
}
for (int j = 0; j < worldArray[0].length; j++) {
worldArray[i][j] = Integer.parseInt(cells[j]);
}
}
Other answers are good. Here you can try this to reduce loop operations and easily get integers from file without parsing String to Integer. Use delimiter with scanner object.
String worldData = "world.txt";
File worldFile = new File(worldData);
int[][] worldArray = new int[40][40];
int i = 0; // For index of worldArray
Scanner scanner = new Scanner(worldFile).useDelimiter("[\\n;]");
while(sc.hasNextInt()) {
wA[i/40][i%40] = sc.nextInt();
i++;
/* Since worldArray is a square matrix, you can keep
incrementing i and divide i by 40 to get rows index and i%40 to get column index thus simplifying the code.*/
}
you can use BufferedReader to read line by line from the file
BufferedReader br = new BufferedReader(new FileReader("world.txt"));
String line = null;
while((line=br.readLine()) != null) {
// process your line
}
br.close();

How can I store columns of a text in an array with which I can then make calculations?

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.

Keep the delimiter at third position and rest of other split

I am trying to split , (Comma) delimiter file where I need to skip , (Comma) at position three and rest of , (Comma) I can split.
My code:
String st;
BufferedReader Br = null;
FileOutputStream outFile1 = new FileOutputStream(
new File("C:\\DATA\\data.xls"));
Workbook book = new HSSFWorkbook();
File objFile = new File(
"C:\\DATA\\user.txt");
Br = new BufferedReader(new FileReader(objFile));
LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(objFile));
lineNumberReader.skip(Long.MAX_VALUE);
int lines = lineNumberReader.getLineNumber();
Sheet sheet = book.createSheet("UserData");
System.out.println("Total Rows in the File : " +lines);
int line = 0;
while ((st = Br.readLine()) != null) {
String value = st.replace("\"", "");
arraylist = value.split(",");
Row row = null;
Cell cell = null;
row = sheet.createRow(line);
for (int i = 0; i < arraylist.length; i++) {
// System.out.println(arraylist[i]);
cell = row.createCell(i);
cell.setCellValue(arraylist[i]);
}
line++;
// System.out.println("Line: " + line);
}
book.write(outFile1);
outFile1.close();
Br.close();
How my txt file look:
"userid","Subscriberid ","HeadhouseFullname",
"167fgfg611","5904fds02","ABC, XYZ C"
"200fhskdhf","876fsgj25","ACD, NNP C"
"3893fjs956","502sgfgg3","ADC, KIO C"
"918shdfd71","1029gsg57","AED, JUI C"
Currently, when the code has been executed then it prints this file value:
userid Subscriberid HeadhouseFullname
167fgfg611 5904fds02 ABC XYZ C
200fhskdhf 876fsgj25 ACD NNP C
3893fjs956 502sgfgg3 ADC KIO C
918shdfd71 1029gsg57 AED JUI C
How it should be printed:
userid Subscriberid HeadhouseFullname
167fgfg611 5904fds02 ABC, XYZ C
200fhskdhf 876fsgj25 ACD, NNP C
3893fjs956 502sgfgg3 ADC, KIO C
918shdfd71 1029gsg57 AED, JUI C
Where you can notice that HeadhouseFullname column value is full name. For example "ABC, XYZ C" where I don't want to split full name by , (Comma) delimiter throughout the file. I want to keep it as it is "ABC, XYZ C".
Currently, it's splitting wherever it see , (Comma) delimiter.
I agree that you should be using a CSV lib as commented above, but if you want to keep going down your current path, try updating your split logic to be:
while ((st = Br.readLine()) != null) {
arraylist = st.split(",");
Row row = null;
Cell cell = null;
row = sheet.createRow(line);
for (int i = 0; i < arraylist.length; i++) {
// System.out.println(arraylist[i]);
cell = row.createCell(i);
cell.setCellValue(arraylist[i].replace("\"", ""));
}
line++;
//System.out.println("Line: " + line);
}
You could start splitting the line on " characters, i.e. st.split("\""). At this point, the resulting array would contain your entries of interest plus two additional kind of strings: empty and , character only.
String[] values = str.split("\"");
Once done that, you could iterate over the resulting array only considering and processing your entries as follows:
for (int valueIndex = 0; valueIndex < values.length; valueIndex++) {
if (values[valueIndex].length() > 0 && !values[valueIndex].equals(",")) {
// DO SOMETHING WITH values[valueIndex]...
}
}
So, considering the source code you posted, the while loop would change as follows:
while ((st = Br.readLine()) != null) {
String[] values = st.split("\"");
Row row = sheet.createRow(line++);
for (int valueIndex = 0, cellIndex = 0; valueIndex < values.length; valueIndex++) {
if (values[valueIndex].length() > 0 && !values[valueIndex].equals(",")) {
Cell cell = row.createCell(cellIndex++);
cell.setCellValue(values[valueIndex]);
}
}
}
Hope this helps!
Lorenzo
I added one additional loop with following updated code and now third column is populating combine with First name, last name and middle initial:
Here below is my updated code:
String st;
BufferedReader Br = null;
FileOutputStream outFile1 = new FileOutputStream(
new File("C:\\DATA\\data.xls"));
Workbook book = new HSSFWorkbook();
File objFile = new File(
"C:\\DATA\\user.txt");
Br = new BufferedReader(new FileReader(objFile));
LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(objFile));
lineNumberReader.skip(Long.MAX_VALUE);
int lines = lineNumberReader.getLineNumber();
Sheet sheet = book.createSheet("UserData");
System.out.println("Total Rows in the File : " +lines);
int line = 0;
while ((st = Br.readLine()) != null) {
arraylist = st.split("," + "\"");
for (int i = 0; i < arraylist.length; i++) {
arraylist[i] = arraylist[i].replace("\"", "");
}
Row row = null;
Cell cell = null;
row = sheet.createRow(line);
for (int i = 0; i < arraylist.length; i++) {
// System.out.println(arraylist[i]);
cell = row.createCell(i);
cell.setCellValue(arraylist[i]);
}
line++;
// System.out.println("Line: " + line);
}
book.write(outFile1);
outFile1.close();
Br.close();
I have tried using regex and it helped for example
String txt = "0, 2, 23131312,\"This, is a message\", 1212312"; System.out.println(Arrays.toString(txt.split(",(?=(?:[^\"]\"[^\"]\")[^\"]$)")));

Clustering many sentence using weka lib in java

I have 5 files text.
I merge these files into 1 file. That file contain about 60 sentences.
I want to clustering that file to 5 cluster.
I am using weka to clustering.
public static void doClustering(String pathSentences, int numberCluster) throws IOException {
Helper.deleteAllFileInFolder("results");
//so cum bang so cau trong file / so cau trung binh trong 1 file
HashMap<Integer, String> sentences = new HashMap<>();
HashMap<Integer, Integer> clustering = new HashMap<>();
try {
StringToWordVector filter = new StringToWordVector();
SimpleKMeans kmeans = new SimpleKMeans();
FastVector atts = new FastVector(5);
atts.addElement(new Attribute("text", (FastVector) null));
Instances docs = new Instances("text_files", atts, 0);
Scanner sc = new Scanner(new File(pathSentences));
int count = 0;
while (sc.hasNextLine()) {
String content = sc.nextLine();
double[] newInst = new double[1];
newInst[0] = (double) docs.attribute(0).addStringValue(content);
docs.add(new SparseInstance(1.0, newInst));
sentences.put(sentences.size(), content);
clustering.put(clustering.size(), -1);
}
NGramTokenizer tokenizer = new NGramTokenizer();
tokenizer.setNGramMinSize(10);
tokenizer.setNGramMaxSize(10);
tokenizer.setDelimiters("\\W");
filter.setTokenizer(tokenizer);
filter.setInputFormat(docs);
filter.setLowerCaseTokens(true);
filter.setWordsToKeep(1);
Instances filteredData = Filter.useFilter(docs, filter);
kmeans.setPreserveInstancesOrder(true);
kmeans.setNumClusters(numberCluster);
kmeans.buildClusterer(filteredData);
int[] assignments = kmeans.getAssignments();
int i = 0;
for (int clusterNum : assignments) {
clustering.put(i, clusterNum);
i++;
}
PrintWriter[] pw = new PrintWriter[numberCluster];
for (int j = 0; j < numberCluster; j++) {
pw[j] = new PrintWriter(new File("results/result" + j + ".txt"));
}
sentences.entrySet().stream().forEach((entry) -> {
Integer key = entry.getKey();
String value = entry.getValue();
Integer cluster = clustering.get(key);
pw[cluster].println(value);
});
for (int j = 0; j < numberCluster; j++) {
pw[j].close();
}
} catch (Exception e) {
System.out.println("Error K means " + e);
}
}
When I change the order of the input file, the clustering results also vary.
Can you help me fix it. Thanks you so much.
k-means is a randomized algorithm.
It picks some instances as initial seeds, then searches for a local optimum.
So of course it will produce different results!
If they vary a lot, this indicates it did not work well. If your data is good for k-means, then most runs will produce very similar results (except for permutation of labels).

List<String[]> method Adding always same values

In my Java Project, i want to read values from txt file to List method.Values seems like;
1 kjhjhhkj 788
4 klkkld3 732
89 jksdsdsd 23
Number of row changable. I have tried this codes and getting same values in all indexes.
What can i do?
String[] dizi = new String[3];
List<String[]> listOfLists = new ArrayList<String[]>();
File f = new File("input.txt");
try {
Scanner s = new Scanner(f);
while (s.hasNextLine()) {
int i = 0;
while (s.hasNext() && i < 3) {
dizi[i] = s.next();
i++;
}
listOfLists.add(dizi);
}
} catch (FileNotFoundException e) {
System.out.println("Dosyaya ba?lanmaya çal???l?rken hata olu?tu");
}
int q = listOfLists.size();
for (int z = 0; z < q; z++) {
for (int k = 0; k < 3; k++) {
System.out.print(listOfLists.get(z)[k] + " ");
}
}
String [] dizi = new String [3];
dizi is a global variable getting overridden eveytime in the loop. Thats why you are getting same values at all indexes
Make a new instance everytime before adding to the list.
You put the same reference to the list, create a new array in while loop.
while (s.hasNextLine()){
String[] dizi = new String[3]; //new array
int i = 0;
while (s.hasNext() && i < 3)
{
dizi[i] = s.next();
i++;
}
listOfLists.add(dizi);
}

Categories

Resources