aspose file tools*
The moose likes JDBC and the fly likes problem with logic ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "problem with logic !" Watch "problem with logic !" New topic
Author

problem with logic !

Manoj Singh
Ranch Hand

Joined: Aug 10, 2000
Posts: 41
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


Sun Certified Java 2 Programmer<BR>" Only those who will risk going too far can possibly find out how far one can go "
shilpa kulkarni
Ranch Hand

Joined: Jun 07, 2000
Posts: 87
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

Joined: Aug 10, 2000
Posts: 41
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

Joined: Sep 20, 2001
Posts: 58
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);
}

Yogen Vadnere
Manoj Singh
Ranch Hand

Joined: Aug 10, 2000
Posts: 41
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

Joined: Sep 20, 2001
Posts: 58
check for case sensitivity also
Manoj Singh
Ranch Hand

Joined: Aug 10, 2000
Posts: 41
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

Joined: Aug 10, 2000
Posts: 41
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

Joined: Sep 20, 2001
Posts: 58
use trim() method
Manoj Singh
Ranch Hand

Joined: Aug 10, 2000
Posts: 41
Hi Yogen !
Thanks mate !! it worked !
Manoj
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with logic !
 
Similar Threads
findByCriteria
Help with Data Array for Hash Table creation
suncertify.db.Data
multi dimension array
Why doesn't find("3") work?????