Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to deal with the string value of "null" in a data file

966239Feb 24 2015 — edited Feb 24 2015

HI, I have table data I'm trying to read . However one column is populated with the word "null".

I guess java does not like dealing with the word null and throws errors.

when I try a block like

if ( rsrow.getString(insCol) = "null") {

I get and error .

If I try :

cmp = rsrow.getString(insCol);

If I try:If I try:

if (cmp = "null") {

I get an error

If I try to use a replace function like:

System.out.println(cmp.replace("null", "0"));

just to see if replace works I get and error.

What id the BEST way to replace this string value with something like "0"  when I come across it with out jave throwing an error?

I want it such that every time I come across the word "null" I can repalce it with "0".

Something like:

if (rsrow.getString(insCol) = "null") {
rowofdata = rowofdata + "'0',";
  } else {
rowofdata = rowofdata + "'" + rsrow.getString(insCol) + "',";
}

Thanks,

Comments

TPD-Opitz

966239 wrote:

HI, I have table data I'm trying to read . However one column is populated with the word "null".

I guess java does not like dealing with the word null and throws errors.

Your guess is wrong.

Jjava has no problem dealing with the String "null". Java will only throw a NullPointerException if a reference ponts to null (no value).

when I try a block like

  1. if ( rsrow.getString(insCol) = "null") { 


I get and error .

Of cause. There are 2 errors in this line.

The first error is the assignment of a value to a method. That is not possible in Java.

However, in Java assingments also have a value. Eg.: you can wrte

int a ,b;

a = 3;

b= a=4;

what results in a and b having 4.

But your assignment (if it would work) resolves to an int and unlike C in Java ints cannot be abused as boolean.

What you wanted was no assignment but a compare which is don in Java using ==.

If I try :

cmp = rsrow.getString(insCol);

If I try:If I try:

  1. if (cmp = "null") { 

I get an error

for the very same reason as above.

If I try to use a replace function like:

  1. System.out.println(cmp.replace("null", "0")); 

just to see if replace works I get and error.

What id the BEST way to replace this string value with something like "0"  when I come across it with out jave throwing an error?

In the first place you schould start to read the error messages of the java compiler carefully. They are surprisingly helpfull compared to those in other programming languages like C or C++.

The next thing to learn is to read the Java API.

You already found the method replace() of class String. In the java API of that class there is a similar method replaceAll(). Maybe you have a look at that...

bye

TPD

unknown-7404

Strings are OBJECTS. You don't compare strings like you are trying.

You would use the 'equals' or 'equalsIgnoreCase' methods of the String class.

See The Java Tutorials trails for 'Comparing Strings and Portions of Strings'.

Comparing Strings and Portions of Strings (The Java™ Tutorials > Learning the Java Language > Numbers…


1 - 2
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 24 2015
Added on Feb 24 2015
2 comments
888 views