Search Algorithm Dies

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Search Algorithm Dies

 
0
  #1
Nov 15th, 2008
I'm working on a project for school where we have to read from a voters database which is set up in the following manner:

voternumber:voternama:havevoted
Ex:

1253:Bill Simmons:false
3244teve Jobs:true

I made this algorithm to search for a voter by ID number but for some reason it keeps coming up as not found. I've been looking at it for awhile.. maybe fresh eyes can spot the problem?
  1. FileInputStream test = new FileInputStream("backupvoters.txt");
  2. Scanner testinput = new Scanner(test);
  3. int found = 0;
  4. String next [] = new String[3], temp = "";
  5.  
  6. while(found == 0)
  7. {
  8. try
  9. {
  10. temp = testinput.nextLine();
  11. }
  12. catch(NoSuchElementException exitloop)
  13. {
  14. found = 5;
  15. }
  16. next = temp.split(":");
  17. if(next[0] == ID)
  18. {
  19. found = 1;
  20. }
  21. }
  22.  
  23. if(found == 1 && next[3] != "true")
  24. {
  25. JFrame hello = new JFrame("Confirmed!");
  26. JLabel a = new JLabel("Welcome " + next[2] + "!");
  27. hello.add(a);
  28. int r = 0;
  29. while(r < allBallots.length)
  30. {
  31. allBallots[r].enableButtons();
  32. r++;
  33. }
  34. hello.setBounds(100, 100, 500, 100);
  35. hello.setVisible(true);
  36. }
  37. else if(found == 1)
  38. {
  39. JFrame hello = new JFrame("Already voted");
  40. JLabel a = new JLabel(next[2] + " you already voted!");
  41. hello.add(a);
  42. hello.setBounds(100, 100, 500, 100);
  43. hello.setVisible(true);
  44. }
  45. else
  46. {
  47. JFrame noname = new JFrame("Not found");
  48. JLabel a = new JLabel("Your ID was not found.");
  49. noname.add(a);
  50. noname.setBounds(100, 100, 500, 100);
  51. noname.setVisible(true);
  52. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: Search Algorithm Dies

 
0
  #2
Nov 16th, 2008
I'm still having no luck
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,711
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: Search Algorithm Dies

 
0
  #3
Nov 16th, 2008
When comparing Strings, almost at 99.99% of the cases, you will need to use the equals method:

  1. String s1 = "aa";
  2. String s2 = "aa";
  3.  
  4. System.out.println( s1.equals(s2) ); //it will print true
  5. System.out.println( s1.equals("aa") ); //it will print true
  6. System.out.println( "aa".equals(s2) ); //it will print true
  7.  
  8. System.out.println( s1==s2); //it will print false

The equals compares the values of the objects. 's1', 's2' are not the same objects but they have the same value. So use the equals.
When you use the '==' you compare the objects themselves. So like I said:'s1', 's2' are not the same objects so '==' will return false

BUT
  1. String s1 = "aa";
  2. String s2 = s1;
  3. System.out.println( s1==s2); //it will print true

So when you compare Strings: next[0] == ID use :
next[0].equals(ID)
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC