Jump to content
Sign in to follow this  
newcoder1010

How to write if else

Recommended Posts

Hello,

My script reads some data from excel cell. Sometimes excel cell has empty but variable returns 0. I am not sure why. 

Use case:

- read data from excel cell.

- If cell is empty, null, or 0, then assign "baddata" value to variable "Cellvalue"

- If cellvalue NOT EQUAL TO "baddata" then print "good data"

String zip2Value;
String cellvalue;

if (zip2Col == null) OR (zip2Col == 0) {
    cellvalue = "baddata";}
}

if (cellvalue != "baddata") {
    System.out.println("good  data ");
}

Is it correct way of writing it?

Share this post


Link to post
Share on other sites

Does it work?  The only issue I see is that you're defining that as a string and then comparing it with 0, which is a number.

Share this post


Link to post
Share on other sites

Are you sure this is Java? You're using the word "OR" in your code and there are comparisons being done between different data types. I can't see where zip2Col is defined so I don't know what type of data it is.

Assuming zip2Col is a string containing the data from the cell, the following Java code would meet your requirements:

String cellValue = "";
if(zip2Col == null || zip2Col.isEmpty() || zip2Col.equals("0")) {
  cellValue = "baddata";
}

if(!cellValue.equals("baddata")) {
  System.out.println("good data");
}

 

Share this post


Link to post
Share on other sites

That's also assuming that if the cell has a 0 it will return the string "0" instead of a number, otherwise you should cast the value to a string.  I don't know enough about the Excel API to know whether you need to do that or not.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...