Forum Stats

  • 3,853,884 Users
  • 2,264,289 Discussions
  • 7,905,494 Comments

Discussions

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

966239
966239 Member Posts: 27
edited Feb 24, 2015 5:23PM in New To Java

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,

Answers

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Feb 24, 2015 5:19PM
    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") { 
    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") { 
    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")); 
    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
    edited Feb 24, 2015 5:23PM

    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…


This discussion has been closed.