Sign in to follow this  
Nanook

java logic error..

Recommended Posts

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this