How to terminate if string is repeated

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 37
Reputation: l_03 has a little shameless behaviour in the past 
Solved Threads: 0
l_03 l_03 is offline Offline
Light Poster

How to terminate if string is repeated

 
0
  #1
Jan 22nd, 2009
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: How to terminate if string is repeated

 
1
  #2
Jan 23rd, 2009
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: How to terminate if string is repeated

 
0
  #3
Jan 23rd, 2009
Originally Posted by stephen84s View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 37
Reputation: l_03 has a little shameless behaviour in the past 
Solved Threads: 0
l_03 l_03 is offline Offline
Light Poster

Re: How to terminate if string is repeated

 
0
  #4
Jan 23rd, 2009
I already solved it, thank you very much. I really love this site. Thank you for the help.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 591
Reputation: ahihihi... is on a distinguished road 
Solved Threads: 4
ahihihi...'s Avatar
ahihihi... ahihihi... is offline Offline
Posting Pro

Re: How to terminate if string is repeated

 
-2
  #5
Jan 23rd, 2009
i also love this site.. ahihihi..
(^.^) Art, Sci. & play had merged into the magical activity of programming (^.^)
-heroes of the c.r.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 788 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC