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:Steve 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?

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);
}

I'm still having no luck

When comparing Strings, almost at 99.99% of the cases, you will need to use the equals method:

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

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.