ArrayIndexOutOfBoundsException generated when br.readLine() = "," - java

The code below has several functions which allow for things such as writing data to a document, reading it and putting the data in an array for a JTable later down the line.
package tabletest.populatetable;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class GetData {
DataClass[] data = new DataClass[500];
int nextPosition = 0;
public GetData() {
readData();
}
public void writeData()
{
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("resources/tabledata.txt")));
for(int i=0; i < nextPosition; i++) {
bw.write(data[i].toString());
bw.newLine();
}
bw.close();
} catch(Exception e) {
System.out.println("Invalid Data input");
}
}
public void readData()
{
try {
BufferedReader br = new BufferedReader(new FileReader(new File("resources/tabledata.txt")));
String nextData = br.readLine();
String[] arrayStringData;
while (nextData != null) {
try {
arrayStringData = nextData.split(",");
} catch(NullPointerException nPE) {
arrayStringData = new String[] {" ", " "};
}
for(int i = 0; i < arrayStringData.length - 1; i++) {
if(arrayStringData[i] == null || arrayStringData[i] == "") {
arrayStringData[i] = " ";
}
}
DataClass getData = new DataClass();
getData.col1 = arrayStringData[0].trim();
getData.col2 = arrayStringData[1].trim();
data[nextPosition] = getData;
nextPosition++;
nextData = br.readLine();
}
br.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public String[][] dataInTableForm() {
final int colCount = 2;
String[][] temp = new String[nextPosition][colCount];
for(int i = 0; i < nextPosition; i++) {
temp[i][0] = data[i].col1;
temp[i][1] = data[i].col2;
}
return temp;
}
private class DataClass {
String col1;
String col2;
public String toString() {
return col1 + ", " + col2;
}
}
}
The document which it is reading, resources/tabledata.txt, is 12 lines long and it looks like this.
asfias, adsnj
aw,aerfae
aw,aewaa
,tre
asfd,
okfas,af
e,ds
sw,f
,
asfias, adsnj
aw,aerfae
aw,aewaa
The problem is on line 9 of the text document. This is where it is just a , on its own. When there is something before or after the comma this seems to work fine and I checked by removing the line to see if it definitely was the comma causing the problem.
When I looked at the console I discovered the problem was a ArrayIndexOutOfBoundsException and the stack trace is below.
java.lang.ArrayIndexOutOfBoundsException: 0
at tabletest.populatetable.GetData.readData(GetData.java:55)
at tabletest.populatetable.GetData.<init>(GetData.java:14)
at tabletest.Table.createTablePanel(Table.java:76)
at tabletest.Table.createPanels(Table.java:34)
at tabletest.Table.runGUI(Table.java:24)
at tabletest.Table.main(Table.java:150)
Line 55 of the code is getData.col1 = arrayStringData[0].trim();
As you can see in the code I attempted several things to prevent this occurring but I have had no luck. I also tried removing the .trim() from the end of the line; however, exactly the same problem occurs.
I would appreciate any help in fixing this problem.

Javadoc of split(String regex) says:
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
So, ",".split(",") will return an empty array, i.e. new String[0].
If you want to keep trailing empty strings, use ",".split(",", -1), which will return new String[] { "", "" }.

The split function will cut out the "," delimiter and return an empty array.
Your try-catch block is catching a null pointer exception if the nextData array is null, not if the output array is empty.
The for loop would only work if the output array contains null strings (which it doesn't).

String.split yanks the delimiter out of the resulting array. So, for a string with just "," your array would be of length 0 (the element at index 0 does not exist).
What you need to do is check whether the length is greater than 0. You can do this using the code arrayStringData.length > 0 in an if statement before trying to access arrayStringData

Related

Read a csv that has double quotes with comma inside

UPDATE: WORKING SOLUTION POSTED BELOW
I'm trying to process a csv file and I'm splitting it by comma. However, there are couple places with quotes that has comma embedded.
Example: "# 29. Toxic substances properly identified, stored, used"
Every quote that has a comma in there is wrapped around with " ", is there a way to detect this double quotes and work around the commas?
Thanks!
Original code:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileWriter;
import java.io.PrintWriter;
public class csvFileReader {
public static void main(String[] args) {
String csvFile = "/Users/zzmle/Desktop/data.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
int count=0;
try {
br = new BufferedReader(new FileReader(csvFile));
String firstline = br.readLine();
String[] header = firstline.split(",");
while ((line = br.readLine()) != null && count<10) {
//comma is the separator
String[] Restaurant = line.split(cvsSplitBy);
for (int i=0; i<header.length; i++) {
System.out.println(header[i]+": "+Restaurant[i]+" ");
}
System.out.println("-------------------");
count++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Working Solution:
// #author Zhiming Zhao
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileWriter;
import java.io.PrintWriter;
public class csvFileReader {
public static void main(String[] args) {
String csvFile = "data.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
int count=0;
try {
br = new BufferedReader(new FileReader(csvFile));
String firstline = br.readLine();
String[] header = firstline.split(cvsSplitBy);
while ((line = br.readLine()) != null && count<10) { //count<10 is for testing purposes
String[] Restaurant = line.split(cvsSplitBy); //comma is the separator
process(Restaurant); //this is to deal with the commas within quotation marks (which split the elements and shifts them into the wrong places)
//this part prints the header + restaurant for the first ten lines
for (int i=0; i<header.length; i++) {
System.out.println(header[i]+": "+Restaurant[i]+" ");
}
System.out.println("-------------------");
count++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("The file cannot be found, check if the file is under root directory");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Input & Output operations error");
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// #brief This function specifically deal with the issue of commas within the quotation marks
// #detail it gets the index number of the 2 elements containing the quotation marks, then concats them all. It works with multiple quotation marks on the same line
public static void process(String[] data) {
int index1 = -1; //initialize the index of the first ", -1 for empty
int index2 = 0; //initialize the index of the second ", 0 for empty
for (int i=0; i<data.length; i++) {
if (String.valueOf(data[i].charAt(0)).equals("\"") && index1 == -1) { //if index1 is not empty and the first char of current element is "
index1 = i; //set index1 to current index number
}
if (String.valueOf(data[i].charAt(data[i].length()-1)).equals("\"") && index1 != -1) { //if index1 is not empty and the last char of current element is "
index2 = i; //set index2 to current index number
multiconcat(index1, index2, data); //concat the elements between index1 and index2
data = multidelet(index1+1, index2, data); //delete the elements that were copied (index1+1:index2)
i -= (index2-index1); //this is to reset the cursor back to index1 (could be replaced with i = index1)
index1 = -1; //set index1 to empty
}
}
}
// #brief Copy all elements between index1 and index2 to index1, doesn't return anything
public static void multiconcat(int index1, int index2, String[] data) {
for (int i=index1+1; i<=index2; i++) {
data[index1] += data[i];
}
}
// #brief Deletes the elements between index1+1 and index2
public static String[] multidelet(int index1, int index2, String[] data) {
String[] newarr = new String[data.length-(index2-index1+1)];
int n = 0;
for (int i=0; i<data.length; i++) {
if (index1 <= i && i <= index2) continue;
newarr[n] = data[i];
n++;
}
return newarr;
}
}
The csv file
Output (one of the lines with quotation mark and comma embedded), although it's not perfect (the comma within quotation mark got eaten), it's a minor issue and I'm too lazy to fix it lol :
serial_number: DA08R0TCU
activity_date: 03/30/2018 12:00:00 AM
facility_name: KRUANG TEDD
violation_code: F035
violation_description: "# 35. Equipment/Utensils - approved; installed; clean; good repair capacity"
violation_status: capacity"
points: OUT OF COMPLIANCE
grade: 1
facility_address: A
facility_city: 5151 HOLLYWOOD BLVD
facility_id: LOS ANGELES
facility_state: FA0064949
facility_zip: CA
employee_id: 90027
owner_id: EE0000857
owner_name: OW0001034
pe_description: 5151 HOLLYWOOD LLC
program_element_pe: RESTAURANT (31-60) SEATS HIGH RISK
program_name: 1635
program_status: KRUANG TEDD
record_id: ACTIVE
score: PR0031205
service_code: 92
service_description: 1
row_id: ROUTINE INSPECTION ```
My own solution:
Read the first character of each element, if the first character is a double quote, concat this and the next ones (will need to use recursion for this) until there's an element with a double quote as the last character.
This will run considerably faster than reading char by char, as suggested by JGFMK.
And I am not allowed to use external libraries for this project.
STILL IMPLEMENTING THIS, I will update if it works
EDIT: Working solution posted in original post
don't reinvent the wheel: there are libs to read csv around e.g. http://commons.apache.org/proper/commons-csv/
http://opencsv.sourceforge.net/
https://code.google.com/archive/p/jcsv/

I want to read a file and also check a word whether the word is present in the file or not. If the word is present one of my method will return +1

This is my code. I want to read a file called "write.txt" and then once it reads. Compare it with a word, here I use "target variable(of string type) once the comparison is done inside the method called findTarget it will return 1 after the condition is true. I try to call the method but I keep getting an error. test.java:88: error: cannot find symbol
String testing = findTarget(target1, source1);
^
symbol: variable target1
location: class test
1 error
can someone correct my mistake. I am quite new to programming.
import java.util.*;
import java.io.*;
public class test {
public static int findTarget( String target, String source )
{
int target_len = target.length();
int source_len = source.length();
int add = 0;
for(int i = 0;i < source_len; ++i) // i is an varialbe used to count upto
source_len.
{
int j = 0; // take another variable to count loops
while(add == 0)
{
if( j >= target_len ) // count upto target length
{
break;
}
else if( target.charAt( j ) != source.charAt( i + j ) )
{
break;
}
else
{
++j;
if( j == target_len )
{
add++; // this will return 1: true
}
}
}
}
return add;
//System.out.println(""+add);
}
public static void main ( String ... args )
{
//String target = "for";
// function 1
try
{
// read the file
File file = new File("write.txt"); //establising a file object
BufferedReader br = new BufferedReader(new FileReader(file));
//reading the files from the file object "file"
String target1;
while ((target1 = br.readLine()) != null) //as long the condition is not null it will keep printing.
System.out.println(target1);
//target.close();
}
catch (IOException e)
{
System.out.println("file error!");
}
String source1 = "Searching for a string within a string the hard way.";
// function 2
test ob = new test();
String testing = findTarget(target1, source1);
// end
//System.out.println(findTarget(target, source));
System.out.println("the answer is: "+testing);
}
}
The error is because findTarget is a class function.
So, where you have this:
test ob = new test();
String testing = findTarget(target1, source1);
...should be changed to call the function from a static context:
//test ob = new test(); not needed, the function is static
int testing = test.findTarget(target1, source1);
// also changed the testing type from String to int, as int IS findTarget's return type.
I don't have your file contents to give a trial run, but that should at least help get past the error.
=====
UPDATE:
You are close!
Inside main, change the code at your loop so that it looks like this:
String target1;
int testing = 0; // move and initialize testing here
while ((target1 = br.readLine()) != null) //as long the condition is not null it will keep printing.
{
//System.out.println(target1);
testing += test.findTarget(target1, source1);
//target1 = br.readLine();
}
System.out.println("answer is: "+testing);
I have finally been able to solve my problem. but extending the functionalities. I want to increment the add by 1. but in my programming, it keeps giving me output as
answer is: 1 answer is: 1
instead I want my program to print not two 1's rather 1+1 = 2
can someone fix this incrementing problem?
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class test {
public static int findTarget(String target, String source) {
int target_len = target.length();
int source_len = source.length();
int add = 0;
// this function checks the character whether it is present.
for (int i = 0; i < source_len; ++i) // i is a varialbe used to count upto source_len.
{
int j = 0; // take another variable to count loops
while (add == 0)
{
if (j >= target_len) // count upto target length
{
break;
}
else if (target.charAt(j) != source.charAt(i + j))
{
break;
}
else
{
++j;
if (j == target_len)
{
add++; // this will return 1: true
}
}
}
}
return add;
//System.out.println(""+add);
}
public static void main(String... args) {
//String target = "for";
// function 1
try {
// read the file
Scanner sc = new Scanner(System.in);
System.out.println("Enter your review: ");
String source1 = sc.nextLine();
//String source1 = "Searching for a string within a string the hard way.";
File file = new File("write.txt"); //establising a file object
BufferedReader br = new BufferedReader(new FileReader(file)); //reading the files from the file object "file"
String target1;
while ((target1 = br.readLine()) != null) //as long the condition is not null it will keep printing.
{
//System.out.println(target1);
int testing = test.findTarget(target1, source1);
System.out.println("answer is: "+testing);
//target1 = br.readLine();
}
br.close();
}
catch (IOException e)
{
System.out.println("file error!");
}
}
}

Splitting an arraylist and textfile

What I'm trying to do is add numbers from a .txt file and split it by ; into my ArrayList listR2. As of now it semi works, however the result is that only the last 2 persons score is added, the first persons score is just getting null.
Is it some problem with my split?
Any ideas how i get the program to write all the scores?
It is skipping lines (from your file) in your code because you have used
for (int i = 3; i < itemStudent.length; i++) {
String test = studin.readLine(); //<--- this is the error
listR2.add(test);
}
Instead use
String test = itemStudent[i]; // to add the scores into the listR2
First, your code:
BufferedReader studin = new BufferedReader(new FileReader(studentFile));
grader.Student student;
student = new Student();
String line, eNamn, fNamn, eMail;
ArrayList<String> listR = new ArrayList<String>();
ArrayList<String> listR2 = new ArrayList<String>();
//loop for the file and setters for first, lastname and email
while ((line = studin.readLine()) != null) {
if (line.contains(";")) {
//# you don't need regex to split on a single specific character
String[] itemStudent = line.split("[;]");
eNamn = itemStudent[0];
fNamn = itemStudent[1];
eMail = itemStudent[2];
//#why are you using the Student object if you never use it in any way ?
//#also you are always updating the same "Student". if you expect to add it to say an ArrayList,
//#you need to declare a new student at the beginning of the loop (not outside of it)
student.setFirstName(fNamn);
student.setLastName(eNamn);
student.setEmail(eMail);
//Loop for the sum of the tests
Integer sum = 0; //# why Interger, the "int" primitive is more than sufficient
for (int index = 3; index < itemStudent.length; index++) {
try {
sum += Integer.parseInt(itemStudent[index]);
listR.add(itemStudent[index]);
} catch (Exception ex) {} //very bad practice, nerver silently drop exceptions.
}
//# that part is just wrong in many ways, I guess it's some left over debug/testing code
//# this also makes you skip lines as you will read as many lines as you have elements (minus 3) in itemStudent
/*
for (int i = 3; i < itemStudent.length; i++) {
String test = studin.readLine();
listR2.add(test);
}
*/
System.out.println(eNamn + " " + fNamn + " " + eMail + " SUMMA:" + sum + " " );
//# you'll get a nice pointer address, but not it's values, you need to itterate the list to view it's content
System.out.println(listR2);
}
}
The //# mark my comments
and here a quick example displaying the object approach:
(may contains misspells/missing imports but otherwise should be fine the compiler should will you). to run it:
java Main "your_file"
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
class Student{
String fname;
String lname;
String mail;
int sum;
Student(String fn,String ln,String ml){
fname=fn;
lname=ln;
mail=ml;
sum=0;
}
void addScore(int n){
sum += n;
}
public String toString() {
return "Student: "+fname+" "+lname+", "+mail+" sum: "+sum;
}
}
try {
BufferedReader br = new BufferedReader(new FileReader(args[0]));
ArrayList<Student> stdnts = new ArrayList<Student>();
String line = br.readLine();
while (line != null) {
if (line.contains(";")) {
String[] stdnt_arr = line.split(";");
Student stdnt = new Student(stdnt_arr[0],stdnt_arr[1],stdnt_arr[2]);
for (int i = 3;i<stdnt_arr.length;i++){
try {
stdnt.addScore(Integer.parseInt(stdnt_arr[i]));
} catch (NumberFormatException e) {
//not a number
e.printStackTrace();
}
}
stdnts.add(stdnt);
System.out.println(stdnt.toString());
}
line = br.readLine();
}
} catch(IOException e){
//things went wrong reading the file
e.printStackTrace();
}
}
}

Using a method to call individual strings from an array (looping)

This is the question from my assignment that I am unsure of:
The class is to contain a public method nextWord(). When a new line is read, use the String method .split("\s+") to create an array of the words that are on the line. Each call to the nextWord() method is to return the next word in the array. When all of the words in the array have been processed, read the next line in the file. The nextWord()method returns the value null when the end of the file is reached.
I have read the file, and stored each individual string in an array called tokenz.
I'm not sure how I can have a method called "nextWord" which returns each individual word from tokenz one at a time. Maybe I don't understand the question?
The last part of the question is:
In your main class, write a method named processWords() which instantiates the MyReader class (using the String "A2Q2in.txt"). Then write a loop that obtains one word at a time from the MyReader class using the nextWord() method and prints each word on a new line.
I've thought of ways to do this but I'm not sure how to return each word from the nextWord method i'm supposed to write. I can't increase a count because after the String is returned, anything after the return statement cannot be reached because the method is done processing.
Any help would be appreciated, maybe I'm going about this the wrong way?
Can't use array lists or anything like that.
Here is my code.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class A2Q2
{
public static void main (String [] args)
{
processWords();
}
public static void processWords()
{
MyReader reader = new MyReader("A2Q2.txt");
String[] words = new String[174];
words[0] = reader.nextWord();
System.out.println(words[0]);
}
}
class MyReader
{
static String name;
static BufferedReader fileIn;
static String inputLine;
static int tokensLength = 0;
static String[] tokens;
static int counter = 0;
// constructor.
public MyReader(String name)
{
this.name = name;
}
public static String[] readFile()
{
String[] tokenz = new String[174];
int tokensLength = 0;
try
{
fileIn = new BufferedReader (new FileReader(name));
inputLine = fileIn.readLine();
while(inputLine !=null)
{
tokens = inputLine.split("\\s+");
for (int i = 0 ; i < tokens.length; i++)
{
int j = i + tokensLength;
tokenz[j] = tokens[i];
}
tokensLength = tokensLength + tokens.length;
inputLine = fileIn.readLine();
}
fileIn.close();
}
catch (IOException ioe)
{
System.out.println(ioe.getMessage());
ioe.printStackTrace();
}
//FULL ARRAY OF STRINGS IN TOKENZ
return tokenz;
}
public static String nextWord()
{
String[] tokenzz = readFile();
//????
return tokenzz[0];
}
}
Here's a conceptual model for you.
Keep track of your MyReader's state to know which value to return next.
the following example uses tokenIndex to decide where to read at next.
class MyReader
{
String[] tokens;
int tokenIndex = 0;
public String nextWord()
{
if(tokens == null || tokens.length <= tokenIndex)
{
// feel free to replace this line with whatever logic you want to
// use to fill in a new line.
tokens = readNextLine();
tokenIndex = 0;
}
String retVal = tokens[tokenIndex];
tokenIndex++;
return retval;
}
}
Mind you, this isn't a complete solution(it doesn't check for the end of file for instance), only a demonstration of the concept. You might have to elaborate a bit.
Use a loop and process each element in the array, printing them one at a time?

java.lang.StringIndexOutOfBoundsException: String index out of range

Hi I wrote a java code to find longest word made of other words. My logic is to read the list of words from the text file and add each word into an array (In the text the words are sorted and there will be only one word in each line) After that we check if each element in the array has other elemnts as substrings. If so we count the number of substrings. The element with maximum number of substrings will be the result
The code is running when I give a text file wih only two words. But when there are more than two words I am getting following error
java.lang.StringIndexOutOfBoundsException: String index out of range: 3
I feel the error is occuring in this line if(s.charAt(i1)==w.charAt(j1))
import java.util.*;
import java.io.*;
import java.lang.reflect.Array;
public class Parser
{
public static void main (String[] args) throws IOException
{
String [] addyArray = null;
FileReader inFile = new FileReader ("sample.txt");
BufferedReader in = new BufferedReader (inFile);
String line = "";
int a = 0;
int size=0;
String smallestelement = "";
while(in.ready())
{
line=in.readLine();
while (line != null && line != "\n")
{
size++;
System.out.println(size);
line = in.readLine();
if (line == null) line = "\n";
}
}
addyArray = new String[size];
FileReader inFile2 = new FileReader ("sample.txt");
BufferedReader in2 = new BufferedReader (inFile2);
String line2 = "";
while(in2.ready())
{
line2 = in2.readLine();
while (line2 != null && line2 != "\n")
{
addyArray[a] = line2;
System.out.println("Array"+addyArray[a]);
line2 = in.readLine();
a++;
if (line2 == null) line2 = "\n";
}
}
int numberofsubstrings=0;
int[] substringarray= new int[size];
int count=0,no=0;
for(int i=0;i<size;i++)
{
System.out.println("sentence "+addyArray[i]);
for(int j=0;j<size;j++)
{
System.out.println("word "+addyArray[j]);
String w,s;
s=addyArray[i].trim();
w=addyArray[j].trim();
try{
for(int i1=0;i1<s.length();i1++)
{
if(s.equals(w)&& s.indexOf(addyArray[j-1].trim()) == -1)
{}
else
{
if(s.charAt(i1)==w.charAt(0))
{
for(int j1=0;j1<w.length();j1++,i1++)
{
if(s.charAt(i1)==w.charAt(j1)) //I feel the error is occuring here
{ count=count+1;}
if(count==w.length())
{no=no+1;count=0;};
}
}
}
}
System.out.println(no);
}
catch(Exception e){System.out.println(e);}
substringarray[i]=no;
no=0;
}
}
for(int i=0;i<size;i++)
{
System.out.println("Substring array"+substringarray[i]);
}
Arrays.sort(substringarray);
int max=substringarray[0];
System.out.println("Final result is"+addyArray[max]+size);
}
}
This is the problem:
for(int j1=0;j1<w.length();j1++,i1++)
On each iteration through the loop, you're incrementing i1 as well as j1. i1 could already be at the end of s, so after you've incremented it, s.charAt(i1) is going to be invalid.
Two asides:
You should look at String.regionMatches
Using consistent indentation and sensible whitespace can make your code much easier to read.
A few tips:
First, always include the full stack trace when you are asking for debugging help. It should point to the exact line number the issue is happening on.
Second, your issue is likely in your most inner loop for(int j1=0;j1<w.length();j1++,i1++) you are incrementing i1 in addition to j1 this will cause i1 to eventually go beyond the size of String s
Finally, you should consider using the String.contains() method for Strings or even a Regular Expression.
When you use string.charAt(x), you must check that it is not beyond string length.
Documentation shows that you will get "IndexOutOfBoundsException if the index argument is negative or not less than the length of this string". And in your particular case, you are only validating in the loop that you are under w length, so it will fail.
As SO already said, the loop runs only taking into account w length, so in case you have a shorter s, it will raise that exception. Check the condition so it goes up to the shorter string or rethink the process.

Categories