import javax.swing.*;
public class A {
	
	public static void main (String args[]){
		String ask = JOptionPane.showInputDialog("Enter");
		if(ask.equals("hello")){
			System.out.println("ok.");
			main(args);
		}else if(isRepeat(ask)){
			System.out.println("Invalid,string has been repeated.");
			System.exit(0);
		}
	}
	public static boolean isRepeat(String ask){
		if(ask.equals("hello")){
			return true;
		}else{
			return false;
		}
	}
}

Hello, I am having problems with my code. IF i input again the "hello", it still prints out "ok". How can I terminate the program, if i input again "hello"?.Please help me. Thank you for the help in advance.

Recommended Answers

All 4 Replies

Insert all the Strings that the user enters in to the args[] string array, since this array is being passed to each successive call of main, So you will have a list of all entered keywords in this array.

In order to check for repetitions just check if the entered word is present in the args[] array.

But instead of calling the main recursively I recommend you delegate this responsibility to some other method and let that method call itself recursively.

commented: Good point on the recursive calls. +11

But instead of calling the main recursively I recommend you delegate this responsibility to some other method and let that method call itself recursively.

I agree.

I already solved it, thank you very much. I really love this site. Thank you for the help.

i also love this site..:$ ahihihi..

commented: Please do not make absolutely useless comments on technical threads. -3
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.