java logic error..

Started by
2 comments, last by XTAL256 15 years, 8 months ago
I realy cant get why this does not work.. been trying for two hours to get it working... its supposed to check if any char is repeated in the string.. It works if I type inn 'abc def' , but eg 'abc def gh' does not work..

import java.util.Scanner;

class repchar
{
	public static void main(String args[])
	{
		Scanner input = new Scanner(System.in);
		
		System.out.println("Pleas enter a string.");
		String str = input.nextLine();
		int sLength = str.length();
		boolean match = false;
		int i = 0;
		while(!match && i < sLength)
		{
			char chkChar = str.charAt(i);
			int j = i + 1;
			while(!match && j < sLength)
			{
				if(str.charAt(j) == chkChar)
					match = true;
				j++;
			}
			i++;
		}
		System.out.println(match);
	}
}

Advertisement
Works for me.
I assume that in your example, the space is the repeated char.
[Window Detective] - Windows UI spy utility for programmers
lol, yeah.. didn't think about that! Had a break from uni and coding.. :)
haha, it's always the simple stupid errors that have you scratching your head [grin].
[Window Detective] - Windows UI spy utility for programmers

This topic is closed to new replies.

Advertisement