| | |
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:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
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.
Last edited by l_03; Jan 22nd, 2009 at 9:51 pm.
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.
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 ?"
"How to ask questions the smart way ?"
![]() |
Similar Threads
- Password Storage System. (Python)
- Basketball timer help needed (Java)
Other Threads in the Java Forum
- Previous Thread: who knows?
- Next Thread: need help here... subject scheduler using topological sorting
Views: 788 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider detection draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle os plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree windows






ahihihi.. 