loop function problem
need help....how can i add a continuous loop to the code such that if there's no USB detected after few counts it will call back the thread function... the code is stated below :
System.out.println("Insert USB disk drive...");
File f = new File("F://");
int slpCount = 0;
while(!f.exists() && slpCount<10)
try {
slpCount++;
Thread.sleep(1000);
}
catch (Exception e) {
}
if(!f.exists()) {
System.out.println("Detection failed!\n" +"\nInsert USB disk drive...");
}
- - - - - - -> i want to call back the thread function after here since detection has failed .....how do i do it?
thanks
jp
boolean complete = false;
public void method1() {
while (!complete)
method2();
}
public void method2() {
System.out.println("Insert USB disk drive...");
File f = new File("F://");
int slpCount = 0;
while(!f.exists() && slpCount<10) {
try {
slpCount++;
Thread.sleep(1000);
}
catch (Exception e) {}
}
if (f.exists())
complete=true;
else
System.out.println("Detection failed!\n" +"\nInsert USB disk drive...");
}
But I get one question anaswered on my part; what if the USB disk is relocated to dfive J:\ That is just a "what-if" question that maybe you can use to device a better way to solve the problem.
Jamwaa at 2007-7-12 17:51:46 >

i still can't manage to call back the function...
my code look like this....
public static void main(String[] args) {
System.out.println("Insert USB disk drive...");
File f = new File("F://");
int slpCount = 0;
while(!f.exists() && slpCount<5)
try {
slpCount++;
Thread.sleep(1000);
}
catch (Exception e) {
}
if(f.exists()) {
System.out.println("Drive inserted!");
}
else {
System.out.println("Detection failed!\n" +"\nInsert USB disk drive...");
} - - - > i want to call back the thread function here (still unable to do it...) how do i do it?
}
thanks
jp
What do you mean "unable to do it"? What specific problem are you having? How did the first answer not help?
jverda at 2007-7-12 17:51:46 >

java.lang.NoSuchMethodError: mainException in thread "main" this was what i got from the output after compiling...
How are you trying to run it?
jverda at 2007-7-12 17:51:46 >

OP: make both methods (method1 & 2) static, as well as the boolean "complete"then, in your public static void main method, just call: "method1()"
import java.io.File;
public class FileLatest {
boolean complete = false;
public static void method1() {
while(!complete)
method2();
}
public static void method2() {
System.out.println("Insert USB disk drive...");
File f = new File("F://");
int slpCount = 0;
while(!f.exists() && slpCount<10) {
try {
slpCount++;
Thread.sleep(1000);
}
catch (Exception e) {
}
}
if (f.exists())
complete=true;
else
System.out.println("Detection failed!\n" +"\nInsert USB disk drive...");
}
public static void main(String[] args) {
method1();
}
}
i'm sorry...still having error on the code above, what else is wrong here...thanks alot
jp :(
what error do you ahve?please use [ code ] tags when you paste code!
public class abcde {
boolean complete = false;
public method1() {
while (!complete)
method2();
}
public method2() {
System.out.println("Insert USB disk drive...");
File f = new File("F://");
int slpCount = 0;
while(!f.exists() && slpCount<10) {
try {
slpCount++;
Thread.sleep(1000);
}
catch (Exception e) {
}
}
if (f.exists())
complete=true;
else
System.out.println("Detection failed!\n" +"\nInsert USB disk drive...");
}
}
public static void main(String[] args) {
new method1(); - - - ->> is this the way if i wanna call back the thread function.....i mean recall the method2()?
}
also can you check wether the code is correct from the beginning? thanks....
Please use code tags.In your main method you create a new method1(), but that is a method and not a class. The name of the class is 'abcde', so the constructor should also be 'abcde' and not 'method1'.
> Please use code tags.
>
> In your main method you create a new method1(), but
> that is a method and not a class. The
> name of the class is 'abcde', so the constructor
> should also be 'abcde' and not 'method1'.
in the code OP pasted earlier, there was only "method1()" and not "new" ; i don't really get why he added the "new" statement, but you're right, it got nothing to do here ^^
OP: get rid of the "new" before "method1()", and if there is an error, tell us what it is
and please use code tags XD
public class abcde {
boolean complete = false;
public abcde() {
while (!complete)
method2();
}
public method2() {
System.out.println("Insert USB disk drive...");
File f = new File("F://");
int slpCount = 0;
while(!f.exists() && slpCount<10) {
try {
slpCount++;
Thread.sleep(1000);
}
catch (Exception e) {
}
}
if (f.exists())
complete=true;
else
System.out.println("Detection failed!\n" +"\nInsert USB disk drive...");
}
}
public static void main(String[] args) {
new abcde();
}
is the code modification above correct?
so am i able to recall back the thread function since the USB is not detected after the no. of counts?
thanks
jp
your modifications make the code look a bit weird, but in absolute, it should do what you're trying to do
basically my program should run this way...
the program will count for 10sec afterwhich when USB is not detected after the 10sec it will prompt again for the user to insert the USB...so the program will keep prompting again and again for 10sec till the user insert the USB and the loop will end..
my problem is only the loop part really need help if there's a need to modify the code hope you can correct it
thanks alot
Juniorprogrammer if you don't know what "please use code tags" means (it ha been mentioned a dozen time already) then ask.Paste your codeHighlight itClick code buttonNot rocket science
> basically my program should run this way...
>
> the program will count for 10sec afterwhich when USB
> is not detected after the 10sec it will prompt again
> for the user to insert the USB...so the program will
> keep prompting again and again for 10sec till the
> user insert the USB and the loop will end..
> my problem is only the loop part really need help if
> there's a need to modify the code hope you can
> correct it
>
> thanks alot
What happens now?
Sorry actually i'm not sure what is code tag thanks anyway...
public class abcde {
boolean complete = false;
public abcde() {
while (!complete)
method2();
}
public method2() {
System.out.println("Insert USB disk drive...");
File f = new File("F://");
int slpCount = 0;
while(!f.exists() && slpCount<10) {
try {
slpCount++;
Thread.sleep(1000);
}
catch (Exception e) {
}
}
if (f.exists())
complete=true;
else
System.out.println("Detection failed!\n" +"\nInsert USB disk drive...");
- - - > the problem is from here how do i call back the method2() again and again until the USB drive is detected, then the loop ends?
}
}
public static void main(String[] args) {
new abcde();
}
really hope if you can modify the code above to meet the requirement (in bold) thanks.
jp
> - - - > the problem is from here how do i call
> back the method2() again and again until the USB
> drive is detected, then the loop ends?
Put a loop around the body of method2(). Or call method2() inside a loop.
> really hope if you can modify the code above to meet
> the requirement (in bold) thanks.
Can but won't. You have to do that yourself.
jverda at 2007-7-21 21:55:05 >

public static void main(String[] args) {
System.out.println("Insert USB disk drive...");
File f = new File("F://");
int counter = 0;
do {
try {
counter++;
Thread.sleep(1000);
}
catch (Exception e) {
}
}while(counter<5);
if(!f.exists()) {
System.out.println("\nDetection failed!" +"\nInsert USB disk drive...");
}
}
how do i want to call back the "do" "while" in order to call back the "thread.sleep()" process when detection is failed?
thanks
jp
public static void main(String[] args) {
System.out.println("Insert USB disk drive...");
File f = new File("F://");
int counter = 0;
do {
do {
try {
counter++;
Thread.sleep(1000);
}
catch (Exception e) {}
}while(counter<5);
if(!f.exists()) {
System.out.println("\nDetection failed!" +"\nInsert USB disk drive...");
}
} while (!f.exists());
}
hi.. thanks alot for all the help.. i managed to find the solution :)jp