Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with logic !

 
Manoj Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Guys !~
Before looking at the code below let me explain somethings here.
I am parsing a flat file and storing values for each column(of single line) in a temporary array called "value[]".
Now I have to insert these values in my database depending on the fact that the record exists or not in the database.
So I am comparing the first column value, which is contained in value[0] with the primary key field value of my
relevant table("Constituent_Id" in this case). If it exist it skips that line in flat file and if doesn't than inserts
that record.
Now my problem is very basic here, the code which I am using for finding out whether the key value is already there in the database is given below but it's not working. One reason which I feel is that method 'equals()' can only be used with Object parameters, where as 'value[0]' is String here..I don't know if I am correct but I cannot figure out how to go about. Please give me some clue on how to do it?
The main idea is to get the loop working properly after that I can put in any statement within the if block

while (rsConstituent.next()) {
String num = rsConstituent.getString("Constituent_Id");
if (num.equals(value[0])) {
out.println(num);
} else {
out.println("no");
out.println("This is:" + value[0]);
}
}


LEGEND: value[] -- is a String array


ThanX
Manoj
 
shilpa kulkarni
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manoj Singh:

Now my problem is very basic here, the code which I am using for finding out whether the key value is already there in the database is given below but it's not working. One reason which I feel is that method [b]'equals()'
can only be used with Object parameters, where as 'value[0]' is String here..[/B]

What is not working, I mean, what is the error?
You can use the equals() method with String, String is an object.
 
Manoj Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shilpa kulkarni:
What is not working, I mean, what is the error?
You can use the equals() method with String, String is an object.

The error is that the loop is not working the way it is intended to ...for instance even if the value[0] is same as num it is still going to the else block...???
Thanks Shilpa !

 
Yogen Vadnere
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess following code will work..
while (rsConstituent.next()) {
String num = rsConstituent.getString("Constituent_Id");
int tempInt = java.util.Arrays.binarySearch(value,num)
if(tempInt < 0)
System.out.println("Not found");
else
System.out.println("found at "+tempInt);
}
 
Manoj Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Yogen Vadnere:
I guess following code will work..
while (rsConstituent.next()) {
String num = rsConstituent.getString("Constituent_Id");
int tempInt = java.util.Arrays.binarySearch(value,num)
if(tempInt < 0)
System.out.println("Not found");
else
System.out.println("found at "+tempInt);
}

HI Yogen !
I did try your code but it is just printing Not found for all the rows, where as I am sure I have atleast 10 records which should match.
ThanX
Manoj
 
Yogen Vadnere
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check for case sensitivity also
 
Manoj Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI !
I must mention this that when I am just substiting value[0] with a some harcoded value say "53435" which exists in the table ..it is working perfectly. So that's why I thought there is some problem with the equals only !!!
ThanX
Manoj
 
Manoj Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Yogen Vadnere:
check for case sensitivity also

Yogen !
All the values in this particular column are digits so that issue doesn't apply here.
ThanX
 
Yogen Vadnere
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use trim() method
 
Manoj Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yogen !
Thanks mate !! it worked !
Manoj
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic