943,917 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1287
  • Java RSS
Jan 22nd, 2009
0

How to terminate if string is repeated

Expand Post »
Java Syntax (Toggle Plain Text)
  1.  
  2. import javax.swing.*;
  3. public class A {
  4.  
  5. public static void main (String args[]){
  6. String ask = JOptionPane.showInputDialog("Enter");
  7. if(ask.equals("hello")){
  8. System.out.println("ok.");
  9. main(args);
  10. }else if(isRepeat(ask)){
  11. System.out.println("Invalid,string has been repeated.");
  12. System.exit(0);
  13. }
  14. }
  15. public static boolean isRepeat(String ask){
  16. if(ask.equals("hello")){
  17. return true;
  18. }else{
  19. return false;
  20. }
  21. }
  22. }

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.
Last edited by l_03; Jan 22nd, 2009 at 9:51 pm.
Similar Threads
Reputation Points: 5
Solved Threads: 0
Light Poster
l_03 is offline Offline
37 posts
since Oct 2008
Jan 23rd, 2009
1

Re: How to terminate if string is repeated

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.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Jan 23rd, 2009
0

Re: How to terminate if string is repeated

Click to Expand / Collapse  Quote originally posted by stephen84s ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Jan 23rd, 2009
0

Re: How to terminate if string is repeated

I already solved it, thank you very much. I really love this site. Thank you for the help.
Reputation Points: 5
Solved Threads: 0
Light Poster
l_03 is offline Offline
37 posts
since Oct 2008
Jan 23rd, 2009
-2

Re: How to terminate if string is repeated

i also love this site.. ahihihi..
Reputation Points: 115
Solved Threads: 5
Posting Pro
ahihihi... is offline Offline
594 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: who knows?
Next Thread in Java Forum Timeline: need help here... subject scheduler using topological sorting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC