| | |
Search Algorithm Dies
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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
3244
teve 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?
voternumber:voternama:havevoted
Ex:
1253:Bill Simmons:false
3244
teve Jobs:trueI 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?
Java Syntax (Toggle Plain Text)
FileInputStream test = new FileInputStream("backupvoters.txt"); Scanner testinput = new Scanner(test); int found = 0; String next [] = new String[3], temp = ""; while(found == 0) { try { temp = testinput.nextLine(); } catch(NoSuchElementException exitloop) { found = 5; } next = temp.split(":"); if(next[0] == ID) { found = 1; } } if(found == 1 && next[3] != "true") { JFrame hello = new JFrame("Confirmed!"); JLabel a = new JLabel("Welcome " + next[2] + "!"); hello.add(a); int r = 0; while(r < allBallots.length) { allBallots[r].enableButtons(); r++; } hello.setBounds(100, 100, 500, 100); hello.setVisible(true); } else if(found == 1) { JFrame hello = new JFrame("Already voted"); JLabel a = new JLabel(next[2] + " you already voted!"); hello.add(a); hello.setBounds(100, 100, 500, 100); hello.setVisible(true); } else { JFrame noname = new JFrame("Not found"); JLabel a = new JLabel("Your ID was not found."); noname.add(a); noname.setBounds(100, 100, 500, 100); noname.setVisible(true); }
When comparing Strings, almost at 99.99% of the cases, you will need to use the equals method:
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
So when you compare Strings:
Java Syntax (Toggle Plain Text)
String s1 = "aa"; String s2 = "aa"; System.out.println( s1.equals(s2) ); //it will print true System.out.println( s1.equals("aa") ); //it will print true System.out.println( "aa".equals(s2) ); //it will print true 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
Java Syntax (Toggle Plain Text)
String s1 = "aa"; String s2 = s1; 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
![]() |
Other Threads in the Java Forum
- Previous Thread: 2D Array: Board Game Help?
- Next Thread: JAVA mobile Application (i need some help)
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel jtextarea julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited webservices windows






