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()) {
Related
I want to compare two cell value, therefore, I added an if statement that helps me to compare it, here's the following code:
if (searchTheColumn != null && searchTheColumn2 != null) { // if that cell is present
System.out.println(wb.getSheetAt(0).getRow(rowNumber).getCell(10) + "\t" + wb.getSheetAt(0).getRow(rowNumber).getCell(11));
if (wb.getSheetAt(0).getRow(rowNumber).getCell(10) || wb.getSheetAt(0).getRow(rowNumber).getCell(11) > 300) // error here {
}
//int cellValue = dataFormatter.formatCellValue(searchTheColumn, formulaEvaluator); // get string cell value
}
At the second if statement condition, I got an error that said The operator > is undefined for the argument type(s) XSSFCell, int, I wonder how do I fix it? I tried to use the data formatter, but it seems like it does not support it.
You cannot compare an instance of an object to a number. You need the value of that XSSFCell in numerical form so that you can compare it.
Taking a quick look at the docs, getNumericCellValue() looks like it should do the trick. So use
wb.getSheetAt(0).getRow(rowNumber).getCell(11).getNumericCellValue() > 300
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.
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("")
I have the following block of code in my Java program:
Filter.sitesToBeFiltered.add(eid.getSite());
System.out.println("Entity Site added to ArrayList. ");
Filter.applicationsToBeFiltered.add(eid.getApplication());
System.out.println("Entity Application added to ArrayList. ");
Filter.IDsToBeFiltered.add(eid.getEntity());
System.out.println("Entity ID added to ArrayList");
Filter.positionsToBeFilteredX.add(position.getX());
System.out.println("Entity X position added to ArrayList. ");
Filter.positionsToBeFilteredY.add(position.getY());
System.out.println("Entity Y position added to ArrayList. ");
Filter.positionsToBeFilteredZ.add(position.getZ());
System.out.println("Entity Z position added to ArrayList. ");
Currently, it will read the values of a set of JTextFields, and add each value to an associated ArrayList. (The ArrayLists are either ArrayLists of Integers or Doubles). However, it will read their values no matter whether the value is a String, int/ double or null...
I want to add some error checking code, so that it will only add the value to the associated ArrayList if it is of the correct data type. I've tried doing this by surrounding each of the two-line blocks in the code displayed above with an 'if' statement, such as:
if(eid.getSite() != null){
Filter.sitesToBeFiltered.add(eid.getSite());
System.out.println("Entity Site added to ArrayList. ");
}
But if I do this, I get a compile error on the if statement that says "The operator != is undefined for the argument type(s) int, null"... Why is this? What should I use to check that the value of the JTextFields are not equal to null instead?
try with:
if(eid != null && eid.getSite()) {
doSomething();
}
If it is int (primitive type) you will always get 0 if no value is set.
If for 0 nothing is assigned at UI side, then try to ignore zero if (!eid.getSite() == 0) but please make sure that 0 dont have any meaning.
With string you can cast return object to string if it's Object class, and check for not blank
I managed to solve this by adding the error checking code to where I was reading the values in from the JTextFields. So instead of:
filter1Type = String.valueOf(Gui.filter1.getSelectedItem());
filter1Value = Integer.parseInt(Gui.filter1Text.getText());
I now have:
filter1Type = String.valueOf(Gui.filter1.getSelectedItem());
if(isNumeric(Gui.filter1Text.getText()){
filter1Value = Integer.parseInt(Gui.filter1Text.getText());
} else {
filter1Value = 0;
//Display some error message to the user here.
}
So now, any value entered into the filter1Text JTextField that is not numeric will automatically be converted to 0, and a message will be displayed to the user telling them to enter a valid value.
First of all,you can control the empty TextFields using the trim method. This control must be used before you assign textField value to the arraylist.
Before control textField value is empty or not,then assign to the arraylist.
String controlledText = eid.getText().trim();//read contents of text area into 'TextField'
if(!controlledText.equals("")){
//doSometing();
}
In my Oracle table there are columns with different type.
I want to read all columns as number.
String str = resultSet.getString("col1");
The problem is that if column in database is defined as number, and value is
0.5
the returned string will be
.5
I can not use any other getter like getDecimal() and etc.
If I use:
String str = resultSet.getObject("col1").toString();
I'll get an exception if the value is null.
You could use
String str = String.valueOf(resultSet.getObject("col1"));
as a simple workaround to avoid any exceptions. (Not sure why you can't use resultSet.getDouble("col1") though.)
If you don't want to see an empty string rather than the literal "null" for a null value (which is what String.valueOf()) will produce, you can use:
Object value = resultSet.getObject("col1")
String str = value == null ? "" : value.toString();