I am trying to read a simple .CSV file and create a 2D array of Strings. Here is the array:
1,1,1,1,1,1
2,2,2,2,2,2
3,3,3,3,3,3
4,4,4,4,4,4
My code is supposed to find six columns and four rows, but it stops after the third column and moves on to the next row and I cannot figure out why this is happening.
In addition to this, it returns an out of bounds exception even though it quits early.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
Here is the code and it is followed by the output.
public String[][] ascToStringArray(String ascFileIn) {
String directory ="c:\\data\\"; // "\" is an illegal character
String[][] numbers= new String[4][6]; // 4 rows 6 columns
try{
BufferedReader Br = new BufferedReader(new FileReader(directory + ascFileIn));
String line;
int row = 0;
int col = 0;
//read each line of text file
while((line = Br.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(line,",");
//Populating Columns
while (st.hasMoreTokens())
{
//get next token and store it in the array
numbers[row][col] = st.nextToken();
System.out.println(row + " " + col + " = " + st.nextToken());
col++;
}
row++;
}
//close the file
Br.close();
return numbers;
}
catch(IOException exc) {
System.out.println("Error reading file.");
return numbers;
}
}
Here is the output:
0 0 = 1
0 1 = 1
0 2 = 1
1 3 = 2
1 4 = 2
1 5 = 2
If anyone can figure out why it is quitting early and throwing me an out of bounds error no matter how large I make the array I would really appreciate it.
You are using nextToken twice.
numbers[row][col] = st.nextToken();<-1---
System.out.println(row + " " + col + " = " + st.nextToken());<--2--Skips element
But using only one value so in one row only three element of line will be added.
Reason of Exception
You are not resetting the col=0 after the execution of inner while loop which leads to ArrayIndexOutOfBound for col=6 as col size in array is 6 means 0 to 5 so will throw exception when col=6.
Firstly the system.out.println inside the inner while loop consumes a token.. Second on entring the inner while loop you should reset the cols files. to zero.
public String[][] ascToStringArray(String ascFileIn) {
String directory = "c:\\data\\"; // "\" is an illegal character
String[][] numbers = new String[4][6]; // 4 rows 6 columns
try {
BufferedReader Br = new BufferedReader(new FileReader(directory + ascFileIn));
String line;
int row = 0;
int col = 0;
// read each line of text file
while ((line = Br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, ",");
col = 0;
// Populating Columns
while (st.hasMoreTokens()) {
// get next token and store it in the array
numbers[row][col] = st.nextToken();
col++;
}
row++;
}
// close the file
Br.close();
return numbers;
} catch (IOException exc) {
System.out.println("Error reading file.");
return numbers;
}
}
It's because you are calling st.nextToken() twice, swallowing an extra token in your System.out.println.
Instead, first save it into a String:
String token = st.nextToken();
then you can use the same String in your print and for the array.
http://msdn.microsoft.com/fr-fr/library/aa989865(v=vs.80).aspx
StringTokenizer.nextToken () : Gets the next token in the string
during parsing.
numbers[row][col] = st.nextToken();
System.out.println(row + " " + col + " = " + st.nextToken());
You go through your tokens without using them !
Related
I am having this issue with the NumberFormatException in my program. Basically, I am asked to read a .csv file separated by ; and it looks like this:
// Column Explanation (not in .csv file)
id; Summary; Number; Employee1; Employee2; ....... Employee7;
"1";"Sony";"1600";"Markos";"Nikos";"Antonis";"Nikolas";"Vaggelis";"Markos";"Thanasis";
"2";"HP";"1000";"Marios";"Dimitra";"Nikolia";"Spiros";"Thomas";"Kostas";"Manolis";
"3";"Dell";"1100";"Antonis";"Aggelos";"Baggelis";"Nikos";"Kuriakos";"Panagiotis";"Rafail";
"4";"Acer";"2000";"Marina";"Aggelos";"Spiros";"Marinos";"Xristos";"Antreas";"Basilis";
What I have already done is create a String 2-d array or the .csv file called temp_arr and I am asked to write a method that will run a linear search by id and return that company. So here is the thing.
At first, I thought I should convert the input key from int -> String since my temp_arr is a String and compares the strings (which at that time they would be int but read as Strings) using temp_arr[value][value2].equals(string_key). But I had a NullPointerException.
Then I thought I should better convert my Id's from the temp_arr from String -> Int and then compare with the integer key using == operand. This action returned me a NumberFormatException.
The process is this:
System.out.println("Enter id :");
Scanner input = new Scanner(System.in);
int item = input.nextInt(); // read the key which is an Integer
int id_int; // temp_arr is String and item is int, must convert ids from String -> int
for (int i = 0; i < temp_arr.length; i++)
{
id_int = Integer.parseInt(temp_arr[i][0]); // Convert from String to int
if (id_int == item) // If the Array's Id's are == item
{
System.out.println(item+" is present at location " + (i+1) );
break;
}
if (i == temp_arr.length)
System.out.println(item + " does not exist");
}
My error appears at line 7 and I do not know why.
Read File process:
String csvFile = "sam.csv"; // .csv file to be placed in the project file!
BufferedReader br = null; // ini
String line = "",cvsSplitBy = ";"; // columns asked to be split by ";"
String[] arr = null;
String[][] temp_arr = new String[1000][10];
int temp = 0;
try
{
br = new BufferedReader(new FileReader(csvFile)); //start reading the file
while ((line = br.readLine()) != null) // while the line has words
{
arr = line.split(cvsSplitBy); // creating the array
System.out.println(arr[0] + "\t" + arr[1] + "\t" + arr[2] + "\t" + arr[3] + "\t" + arr[4] + "\t" + arr[5] + "\t" + arr[6] + "\t" + arr[7] + "\t" + arr[8] + "\t" + arr[9] );
for (int i = 0; i<=9; i++)
{
temp_arr[temp][i] = arr[i]; // temp_arr represents (is a copy of) the .csv file
}
temp++;
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
if (br != null)
{
try
{
br.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
System.out.println("Done!\n");
Output (Image) :
Line 106 which is causing the issue is :
id_int = Integer.parseInt(temp_arr[i][0]); // Convert from String to int
Your issue is that your Integer.parseInt() is trying to parse a "2" WITH QUOTATION MARKS. That's the problem.
A quick solution would be to replace this line:
temp_arr[temp][i] = arr[i];
To this:
temp_arr[temp][i] = arr[i].replaceAll("\"", "");
Anyway, I'd like to suggest using a different data structure for your case, because I've done something like this before for a client. Have you ever heard of HashMaps? You can do something like a HashMap with an int key and String[] values to store your data in, and the key can be your id_int. Maybe you can try this implementation next time. It's a lot more elegant.
Hope I was able to help!
Cheers,
Justin
Would help if you also posted some of your data file and how you are reading it in.
But, my guess from what is presented is if you add System.out.println(temp_arr[i][0]) prior to the 7th line or run this code through a debugger you will see that temp_arr[i][0] is not an integer value as that is what the error is telling you.
I'm doing a Programming Assignment and basically I need to read from a txt file and sort everything in there in different arrays allowing me to display everything in the cmd prompt neatly and be able to delete stuff.
h Vito 123
d Michael 234 Heart
s Vincent 345 Brain Y
n Sonny 456 6
a Luca 567 Business
r Tom 678 Talking Y
j Anthony 789 Maintenance N
d Nicos 891 Bone
n Vicky 911 7
First column needs to be the employeeRole (employee, doctor). The second column being the employeeName. Third column being the employeeNumber and some of them have have a fourth column (if it's a number it's number of patients. Y is for like sweeping, or answering calls)
So my thought process was put each column into it's own array and then writing it out that way. I was able to put each row into its own array with
public class ReadingFile {
// String test;
// char[] employeeRole = new char[9];
String[] employeeRole = new String[9];
String[] employeeName = new String[9], specialty;
String[] wholeLine = new String[9];
// String word;
int[] employeeNum = new int[9];
int r, n, l, num;
public void Reader()
{
Scanner inputStream = null;
Scanner inputStream2 = null;
Scanner inputStream4 = null;
try
{
BufferedReader inputStream3 =
new BufferedReader(new FileReader("data.txt"));
inputStream = new Scanner(new FileInputStream("data.txt"));
inputStream =
new Scanner(new FileInputStream("data.txt"));
inputStream2 =
new Scanner(new FileInputStream("data.txt"));
inputStream4 =
new Scanner(new FileInputStream("data.txt"));
System.out.println("Yeah");
}
catch(FileNotFoundException e){
System.out.println("File Not found");
System.exit(1);
}
for (l=0; l<9; l++)
{
wholeLine[l] = inputStream2.nextLine();
System.out.println(wholeLine[l]);
}
But I couldn't figure out what to do from there. Doing a split would then put an array into an array? Which means I would put each line into an array and then each word into an array?
So I tried something else, anything with the length not equal to 1 would be the employeeNum, but then they there were the N's and Y's and the number of pateints.
for(r=0; r<9; r++) //role
{
String next = inputStream4.next();
while( next.length() != 1)
{
next = inputStream4.next();
}
employeeRole[r] = next;
System.out.println(employeeRole[r]);
}
I also tried
for (r=0; r<9; r++)
{
employeeRole[r] = wholeLine[r].substring(wholeLine[r].indexOf(1));
//inputStream.nextLine();
System.out.println(employeeRole[r]);
}
I'm just not sure if I'm going the right way about it? If I'm making it more difficult than it really is? Or if there's an easier way to do this. But after everything is done, the output should be able to basically say
Doctors: 2
Name: Michael Employee Number: 234 Specialty: Heart
Name: Nicos Employee Number: 891 Specialty: Bone
Any help would be greatly appreciated, thanks!
You don't have to open 4 streams in order to read the file (I guess you wanted to open "one per column" but you shouldn't do it).
Second, you can split the string on spaces (" ") which will provide you the columns (for every line separately) exactly like you want.
Code example:
BufferedReader br = null;
String[] characters = new String[1024];//just an example - you have to initialize it to be big enough to hold all the lines!
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("data.txt"));
int i=0;
while ((sCurrentLine = br.readLine()) != null) {
String[] arr = sCurrentLine.split(" ");
//for the first line it'll print
System.out.println("arr[0] = " + arr[0]); // h
System.out.println("arr[1] = " + arr[1]); // Vito
System.out.println("arr[2] = " + arr[2]); // 123
if(arr.length == 4){
System.out.println("arr[3] = " + arr[3]);
}
//Now if you want to enter them into separate arrays
characters[i] = arr[0];
// and you can do the same with
// names[1] = arr[1]
//etc
i++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
I have been attempting to save a 9x9 2D array of ints from a .txt file. I have been on this website for a very long time attempting to get this to work and after doing a ton of tweaking, I am very close to getting it. The only problem is that nothing saves to the array!
Here is my code:
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
public class test {
public static void main(String[] args) throws IOException {
int[][] thing = new int[9][9];
int row = 0;
int rows = 9;
int columns = 9;
File fin = new File("C:\\Users\\David\\workspace\\tester\\initial.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(fin));
String line = reader.readLine();
int lineNum = 0;
while (line != null) {
lineNum++;
System.out.println("line " + lineNum + " = " + line);
line = reader.readLine();
String [] tokens = line.split(",");
for (int j=0; j<tokens.length; j++) {
System.out.println("I am filling the row: " + row);
thing[row][j] = Integer.parseInt(tokens[j]);
}
row++;
}
System.out.println("I am printing the array for testing purposes: ");
for (int i=0; i < rows; i++) {
for (int j = 0; j < columns; j++)
System.out.print(thing[i][j]);
System.out.println("");
}
} catch (IOException error) {
} finally {
if (reader != null) reader.close();
}
}
}
I should say that I am doing this as a test for a sudoku game I am trying to create as a mere side project, I am just super frustrated.
This was also my first post on this site so go easy on me for the formatting. Thanks all!
Edit: I made the change codaddict told me too and now I get the output:
line 1 = 5 3 0 0 7 0 0 0 0
I am filling the row: 0
Exception in thread "main" java.lang.NumberFormatException: For input string: "6 0 0 1 9 5 0 0 0"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at test.main(test.java:36)
You are doing:
while (line != null) {
lineNum++;
System.out.println("line " + lineNum + " = " + line);
line = reader.readLine();
}
// At this point you've already reached the end of the file
// and line is null, so you never go inside the next while.
while (line != null) {
// you need to split on space not comma
String [] tokens = line.split(" ");
for (int j=0; j<tokens.length; j++) {
System.out.println("I am filling the row: " + row);
thing[row][j] = Integer.parseInt(tokens[j]);
}
row++;
}
To fix this you need to process each line in the outer while loop:
while (line != null) {
lineNum++;
System.out.println("line " + lineNum + " = " + line);
line = reader.readLine();
String [] tokens = line.split(",");
for (int j=0; j<tokens.length; j++) {
System.out.println("I am filling the row: " + row);
thing[row][j] = Integer.parseInt(tokens[j]);
}
row++;
}
The string line is already null after exiting the first while loop and hence the second while loop is never executed, and hence no values get assigned to the array cells, the default value of int array cells being zero which gets printed. Insert the assigning part in the first loop to solve the problem.
you have read the whole file in the first while loop itself.
So, you are unable to print the values in the second loop.
Make sure you are storing the values from the text file in the first while loop.
while (line != null) {
lineNum++;
System.out.println("line " + lineNum + " = " + line);
line = reader.readLine();
String [] tokens = line.split(",");
for (int j=0; j<tokens.length; j++) {
System.out.println("I am filling the row: " + row);
thing[row][j] = Integer.parseInt(tokens[j]);
}
row++;
}
In my application I need to read a specific column of tab separated csv file using jsp. But I can read the data of full row not a specific column.
I need help this regard. Please help me
Thanks
mycode:
<%# page import="java.io.*"%>
<html>
<body>
<%
String fName = "c:\\csv\\myfile.csv";
String thisLine;
int count=0;
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
int i=0;
%>
<table>
<%
while ((thisLine = myInput.readLine()) != null)
{
String strar[] = thisLine.split(",");
for(int j=0;j<strar.length;j++)
{
if(i!=0)
{
out.print(" " +strar[j]+ " ");
}
else
{
out.print(" <b>" +strar[j]+ "</b> ");
}
}
out.println("<br>");
i++;
}
%>
</table>
</body>
</html>
I don't think you can read specific column.Better to read entire row using CSVParser or you can read CSV line by line and split it and get String array then you can get specific column but yes you need to read whole row gain.
Try it.
String fName = "C:\\Amit\\abc.csv";
String thisLine;
int count = 0;
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
int i = 0;
while ((thisLine = myInput.readLine()) != null) {
String strar[] = thisLine.split(",");
System.out.println(strar[3]);
// Here column 2
}
}
By this way you can read specific column.
I had a similar problem in Objective C the other day, but this is how I solved it.
This method assumes you know the column number of the data you want. (I.E. if you want column 1 of 6)
Read all the rows into strings and append them into one.
Data sample: (columns 1 to 6)
1,2,3,4,5,6
13,45,63,29,10,8
11,62,5,20,13,2
String 1 = 1,2,3,4,5,6
String 2 = 13,45,63,29,10,8
String 3 = 11,62,5,20,13,2
Then you should get this:
String combined = 1,2,3,4,5,6,13,45,63,29,10,8,11,62,5,20,13,2 //add in the missing "," when you concatenate strings
Next you need to split the string into an array of all values.
Use code somewhat like this: (written off the top of my head so may be off.)
String[] values = combined.split(",");
Now you should have something like this:
Values = `"1", "2", "3", ... etc`
The last step is to loop through the entire array and modulo for whatever column you need:
//Remember that java numbers arrays starting with 0.
//The key here is that all remainder 0 items fall into the first column. All remainder 1 items fall into the second column. And so on.
for(int i = 0; i < values.length(); i++)
{
//Column1 - Column6 -> array lists of size values.length/number of columns
//In this case they need to be size values.length/6
if(i % 6 == 0)
column1.add(values[i]);
else if(i % 6 == 1)
column2.add(values[i]);
else if(i % 6 == 2)
column3.add(values[i]);
else if(i % 6 == 3)
column4.add(values[i]);
else if(i % 6 == 4)
column5.add(values[i]);
else if(i % 6 == 5)
column6.add(values[i]);
}
~~~~~~~~~~~~~~~~
Edit:
You added code to your question. Above I was saving them into memory. You just loop through and print them out. In your while loop, split each line separately into an array and then either hardcode the column number or modulo the length of the array as the index.
public class ParseCSVs {
public static void main(String[] args) {
try {
// csv file containing data
String strFile = "./input//SIMNumbers.csv";
String line = "";
System.out.println("Enter line number to configure");
Scanner sc = new Scanner(System.in);
int lineNumber = sc.nextInt();
BufferedReader br = new BufferedReader(new FileReader(strFile));
if ((line = br.readLine()) != null) {
String cvsSplitBy = ",";
String blankCell = null;
// use comma as separator
String[] cols = line.split(cvsSplitBy);
for (int i = 0; i < cols.length; i++)
System.out.println("Coulmns = " + cols[i]);
// System.exit(0);
} else
System.out.println("No data found in csv");
} catch (IOException e) {
e.printStackTrace();
}
}
I'm trying to write a method to take a multiline tab-delimited file and return the contents of that file as an arraylist of String arrays (each line is a String[], and each such String[] is an element of an arraylist). My problem is, I can't tell if the output is correct or not. I've printed each arraylist element and String[] element as they are saved to the arraylist, and those printings look correct. But after the arraylist is returned and I print the String[] in it, they appear to only have the contents of the very last line of the file. I'm suspecting it might be something about FileReader or BufferedReader that I don't know. Anyhoo, here's the code:
public class DataParsingTest {
static File AAPLDailyFile = new File("./textFilesForMethodTests/dataParsingPractice2.tsv");
public static void main(String[] args) throws FileNotFoundException, IOException {
ArrayList<String[]> stringArrayList = fileToStringArray(AAPLDailyFile);
System.out.println("stringArray.size() = " + stringArrayList.size());
System.out.println(stringArrayList.get(0)[0]);
for (int i = 0; i < stringArrayList.size(); i++) {
for (int j = 0; j < stringArrayList.get(i).length; j++) {
System.out.println("index of arraylist is " + i + " and element at index " + j + " of that array is " + stringArrayList.get(i)[j]);
}
}
}
public static ArrayList<String[]> fileToStringArray(File file) throws FileNotFoundException, IOException {
ArrayList<String[]> arrayListOfStringArrays = new ArrayList<String[]>();
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
int nextChar = 0;
int noOfTokens = 1; // because the first token doesn't have a tab or newline before it
int startIndex = 0, endIndex = 0, tokenIndex = 0;
String toRead = "";
toRead = bufferedReader.readLine();
for (int i = 0; i < toRead.length(); i++) {
if (toRead.charAt(i) == '\t') {
noOfTokens++;
}
}
System.out.println("noOfTokens = " + noOfTokens);
bufferedReader.close();
fileReader.close();
String[] productString = new String[noOfTokens];
startIndex = 0;
endIndex = 0;
tokenIndex = 0;
FileReader fileReader2 = new FileReader(file);
BufferedReader bufferedReader2 = new BufferedReader(fileReader2);
tokenIndex = 0;
int count = 1;
while ((toRead = bufferedReader2.readLine()) != null) {
System.out.println("toRead = " + toRead);
startIndex = -1; // [L - so that the first time an array element is assigned, it's upped to 0]
endIndex = 0;
tokenIndex = 0;
while (true) {
endIndex = toRead.indexOf("\t", startIndex + 1);
if (endIndex == -1) {
productString[tokenIndex] = toRead.substring(startIndex + 1);
System.out.println("tokenIndex = " + tokenIndex);
System.out.println("productString[" + tokenIndex + "] = " + productString[tokenIndex]);
tokenIndex++;
count++;
arrayListOfStringArrays.add(productString);
System.out.println("just added an array to the list. the first element is " + productString[0]);
break;
}
productString[tokenIndex] = toRead.substring(startIndex + 1, endIndex);
System.out.println("tokenIndex = " + tokenIndex);
System.out.println("productString[" + tokenIndex + "] = " + productString[tokenIndex]);
startIndex = endIndex;
tokenIndex++;
count++;
}
}
fileReader2.close();
bufferedReader2.close();
return arrayListOfStringArrays;
}
}
The input file is:
1 2
3 4
5 6
The output is:
noOfTokens = 2
toRead = 1 2
tokenIndex = 0
productString[0] = 1
tokenIndex = 1
productString[1] = 2
just added an array to the list. the first element is 1
toRead = 3 4
tokenIndex = 0
productString[0] = 3
tokenIndex = 1
productString[1] = 4
just added an array to the list. the first element is 3
toRead = 5 6
tokenIndex = 0
productString[0] = 5
tokenIndex = 1
productString[1] = 6
just added an array to the list. the first element is 5
stringArray.size() = 3
5 // from here on up, it looks like the method works correctly
index of arraylist is 0 and element at index 0 of that array is 5
index of arraylist is 0 and element at index 1 of that array is 6
index of arraylist is 1 and element at index 0 of that array is 5
index of arraylist is 1 and element at index 1 of that array is 6
index of arraylist is 2 and element at index 0 of that array is 5
index of arraylist is 2 and element at index 1 of that array is 6 //these 6 lines only reflect the last line of the input file.
Thanks a mil!
You're only creating a single string array, and reusing that for all lines. So your ArrayList just contains multiple references to the same object. You need to understand that when you call arrayListOfStringArrays.add(productString); that's not adding a copy of the array to the ArrayList - it's just adding a reference. (The value of productString is just a reference, not the array itself.)
Just move this:
String[] productString = new String[noOfTokens];
into the while loop, and all should be well. (In this respect, anyway. You should also be closing your file handles in finally blocks.)
That looks like too much code for me to process. Try this altered fileToStringArray method.
public static ArrayList<String[]> fileToStringArray(File file) throws FileNotFoundException, IOException {
ArrayList<String[]> returnVal = new ArrayList<String[]>();
// Scanner is a nifty utility for reading Files
Scanner fIn = new Scanner(file);
// keep reading while the Scanner has lines to process
while (fIn.hasNextLine()) {
// take the next line of the file, and split it up by each tab
// and add that String[] to the list
returnVal.add(fIn.nextLine().split("\t", -1));
}
return returnVal;
}