Empty String Error While reading and writing CSV file - java

I am reading the csv file and writing it to Database ussing prepare statement but while inserting the number column ,i am getting this error
java.lang.NumberFormatException: empty String
i tried to handle by this way
if(csvLength > 43 && (null != csvRead[43] && !csvRead[43].trim().equals("\"\""))){
pstmt.setFloat(45,Float.parseFloat((csvRead[43].replac("\"","")).toString()));
}else{
pstmt.setNull(45,Types.INTEGER);
but still giving the same error .Please help
the value at csvRead[43] - """"

Because !csvRead[43].trim().equals("\"\"") is returning true, it is going inside if condition and trying yo parse "" and you are getting NumberFormatException. It should be !csvRead[43].trim().equals("").
Also I would like to suggest, create a variable for csvRead[43], you are reading thrice a value from a array. That will improve performance a bit.
if(csvLength > 43 && (null != csvRead[43] && !csvRead[43].trim().equals(""))){
pstmt.setFloat(45,Float.parseFloat((csvRead[43].replace("\"\"","0")).toString()));
}else{
pstmt.setNull(45,Types.INTEGER);
}

This does not check for an empty string:
trim().equals("\"\"")
That checks for a string whose value is two quotes. You need to check for equals("")

Related

StringBuilder.append outofmemory

I'm using StringBuilder.append() to parse and process a file as following :
StringBuilder csvString = new StringBuilder();
bufferedReader.lines().filter(line -> !line.startsWith(HASH) && !line.isEmpty()).map(line -> line.trim())
.forEachOrdered(line -> csvString.append(line).append(System.lineSeparator()));
int startOfFileTagIndex = csvString.indexOf(START_OF_FILE_TAG);
int startOfFieldsTagIndex = csvString.indexOf(START_OF_FIELDS_TAG, startOfFileTagIndex);
int endOfFieldsTagIndex = csvString.indexOf(END_OF_FIELDS_TAG, startOfFieldsTagIndex);
int startOfDataTagIndex = csvString.indexOf(START_OF_DATA_TAG, endOfFieldsTagIndex);
int endOfDataTagIndex = csvString.indexOf(END_OF_DATA_TAG, startOfDataTagIndex);
int endOfFileTagIndex = csvString.indexOf(END_OF_FILE_TAG, endOfDataTagIndex);
int timeStartedIndex = csvString.indexOf("TIMESTARTED", endOfFieldsTagIndex);
int dataRecordsIndex = csvString.indexOf("DATARECORDS", endOfDataTagIndex);
int timeFinishedIndex = csvString.indexOf("TIMEFINISHED", endOfDataTagIndex);
if (startOfFileTagIndex != 0 || startOfFieldsTagIndex == -1 || endOfFieldsTagIndex == -1
|| startOfDataTagIndex == -1 || endOfDataTagIndex == -1 || endOfFileTagIndex == -1) {
log.error("not in correct format");
throw new Exception("not in correct format.");
}
The problem is that when the file is quite large i get an outofmemoryexception.
Can you help me transform my code to avoid that exception with large files?
Edit:
As I can understand charging a huge file into a string Builder is not a good idea and won't work.
So the question is which structure in Java is the more appropriate to use to parse my huge file, delete some lines , find the index of some lines and seperate the file into parts (where to store those parts thaht can be huge) according to the found indexes then creating an output file in the end?
The OOM seems to be due to the fact that you are storing all lines in the StringBuilder. When the file has too many lines, it will take up a huge amount of memory and may lead to OOM.
The strategy to avoid this depends upon what you are doing with appended strings.
As I can see in your code, you are only trying to verify the structure of the input file. In that case, you don't need to store all the lines in a StringBuilder instance. Instead,
Have multiple ints to hold each index you are interested in, (or have an array of ints)
Instead of adding the line to the StringBuilder, detect the presence of the "tag" or "index" you are looking for and save it in its designated int variable.
Finally, the check you are already doing may need to undergo a change to test not as -1 but relative to other indices. (This you are currently achieving using a start index in the indexOf() call.)
If there is a risk of a tag spanning across lines, then you may not be able to use streams, but will have to use a simple for loop in which to save some previous lines, append them and check. (Just one idea; you may have a better one.)

Reading excel and set String value to Integer using Java POI

I tried to read excel data using java poi and attempt to store in the database.
I read the values from the excel like
Integer.parseInt(formatter.formatCellValue(row.getCell(columnNum)))
in that, the excel may contain null values in the column. So It throws an error
java.lang.NumberFormatException: For input string: ""
when try to set excel values to Integer like
riskVo.setProbability(Integer.parseInt(formatter.formatCellValue(row.getCell(columnNum))));
Note: Probability is an Integer data type.
But It accepts the values if the column is not null.
This is the excel file.
The error occurs in 2nd row 2nd column which contains empty column.If the column has value it works perfectly.
Please note, there is difference between "" and NULL. What you are getting exception is, because of empty string "", which is different than NULL.
For example :
String s = "";
s.length ()// it will return 0
But,
String s = null;
s.length ()// throw NPE.
So, you could probably first check, if String is not null and then check if it not empty.
if(str != null && !str.isEmpty()) {

Java delete string from file if zero returned

I have a script which visits links from a text file. I am trying to delete the string if value returned is null
Example:
1. some link (returned value 'hi')
2. some link (returned null value) //DELETE STRING FROM FILE BECAUSE NULL VALUE RETURNED
3. some link (returned value 'hello')
Some code:
while ((input = in.readLine()) != null) {
System.out.println(input);
if ((input = in.readLine())=="0"){
System.out.println("1 String deleted from file because null value returned ");
}
I'm aware that I'm checking for String "0" instead of an integer 0 because the server stores it as a string i suppose.
I think, rather than trying to remove to the file mid-read (and I don't even really know how you'd do that, and if you could it'd be a horrible idea) you might have an easier time of this by just reading the entire file in and storing each value in an index of an ArrayList<string>:
ArrayList<string> lines = new ArrayList<string>();
while ((input = in.readLine()) != null) {
lines.add(input);
}
Then write the file again after you've finished reading it, skipping any index of lines that's equal to "0":
for (String line : lines)
{
// skip "0"
if (line.equals("0")) {
continue;
}
// write to file if not
writer.write(line);
writer.newLine();
}
Note that == compares reference equality in Java, and .equals compares value equality, so for almost all cases you want to use .equals.
Granted, if as your comment states above, you have another file constantly writing to this one, you're better off looking for an entirely new idea. For that matter, if you've got a script writing these, why not change the script so that it just doesn't write lines for null values in the first place? Unless you have literally no way at all of changing the script, spinning another one up to constantly rewrite parts of its work (on the same constantly-accessed file!) is going to be a. ineffective and b. extremely problematic.

Whats wrong with my syntax on if statement using LineReader?

Can anyone see why I'm getting an error on this:
r = new LineNumberReader(new FileReader(txtFile));
for (int i = 1; i < txtFile.length(); i++){
if (r.getLineNumber() = (6*i)+1 || r.equals(1)) {
//code here
}
}
Error is:
Multiple markers at this line
- The left-hand side of an assignment must be a
variable
- The left-hand side of an assignment must be a
variable
- Syntax error on token ")", delete this token
But i can't see whats the issue is. Error is on the 3rd line
EDIT: I love you all, you've saved me once again! Guess I've spent to long with VB.net....
if (r.getLineNumber() = (6*i)+1 || r.equals(1))
should be
if (r.getLineNumber() == (6*i)+1 || r.equals(1))
Not sure what do you want to check with this r.equals(1) but you will most probably get false all the time, since you are compartir equallity between a LineNumberReader and an Integer.
In JAVA you compare two objects with two equals == , but in the if statement you put only one =.
So replace the = in the second line with the ==.
And please, write down this error somwhere you can remember it. Becouse every time you'll see it, then you'll know that it is caused by this same problem (mostly).
My wild guess would be:
if (r.getLineNumber() == (6*i)+1 || r.getLineNumber == 1)
should be == in stead of =
r.getLineNumber() == (6*i)

form validation not working correctly

I am writing a simple GUI using netbeans and Java SE, i cannot seem to get input validation to work correctly. I have a JTextField and 2 JPasswordField objects, I want to check that any of these fields arent empty when the user hits the submit button. It works properly for the password fields if they are left empty, however if i input something into the username field, it prints data has been submitted, when the other form elements have not been filled with data. Here is the code below, any assistance would be greatly appreciated....
// test that none of the fields are empty before submitting form data
if (!username_input.getText().isEmpty() && !password_input1.getPassword().toString().isEmpty()
&& !password_input2.getPassword().toString().isEmpty())
{
System.out.println("Data has been submitted!");
}
else
{
System.out.println("Form has not been filled in correctly!\nPlease try again");
}
use something like
!username_input.getText().toString().equals("")&&
!new String(password_input1.getPassword()).equals("") &&
!new String(password_input2.getPassword()).equals("")
Change the boolean expression to:
!username_input.getText().isEmpty() &&
password_input1.getPassword().lenght == 0 &&
password_input2.getPassword().lenght == 0
The method getPassword() returns a char array and you need check the lenght == 0 for empty values in the JPasswordField.

Categories