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

[712 byte] By [juniorprogrammera] at [2007-11-27 6:28:30]
# 1

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...");

}

calvino_inda at 2007-7-12 17:51:46 > top of Java-index,Java Essentials,Java Programming...
# 2
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 > top of Java-index,Java Essentials,Java Programming...
# 3

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

juniorprogrammera at 2007-7-12 17:51:46 > top of Java-index,Java Essentials,Java Programming...
# 4
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 > top of Java-index,Java Essentials,Java Programming...
# 5
java.lang.NoSuchMethodError: mainException in thread "main" this was what i got from the output after compiling...
juniorprogrammera at 2007-7-12 17:51:46 > top of Java-index,Java Essentials,Java Programming...
# 6
How are you trying to run it?
jverda at 2007-7-12 17:51:46 > top of Java-index,Java Essentials,Java Programming...
# 7
OP: make both methods (method1 & 2) static, as well as the boolean "complete"then, in your public static void main method, just call: "method1()"
calvino_inda at 2007-7-12 17:51:46 > top of Java-index,Java Essentials,Java Programming...
# 8

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 :(

juniorprogrammera at 2007-7-12 17:51:46 > top of Java-index,Java Essentials,Java Programming...
# 9
what error do you ahve?please use [ code ] tags when you paste code!
calvino_inda at 2007-7-12 17:51:46 > top of Java-index,Java Essentials,Java Programming...
# 10

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....

juniorprogrammera at 2007-7-12 17:51:46 > top of Java-index,Java Essentials,Java Programming...
# 11
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'.
keeskista at 2007-7-12 17:51:46 > top of Java-index,Java Essentials,Java Programming...
# 12

> 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

calvino_inda at 2007-7-12 17:51:46 > top of Java-index,Java Essentials,Java Programming...
# 13

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

juniorprogrammera at 2007-7-12 17:51:46 > top of Java-index,Java Essentials,Java Programming...
# 14
your modifications make the code look a bit weird, but in absolute, it should do what you're trying to do
calvino_inda at 2007-7-12 17:51:46 > top of Java-index,Java Essentials,Java Programming...
# 15

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

juniorprogrammera at 2007-7-21 21:55:04 > top of Java-index,Java Essentials,Java Programming...
# 16
name
prob.not.sola at 2007-7-21 21:55:05 > top of Java-index,Java Essentials,Java Programming...
# 17
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
floundera at 2007-7-21 21:55:05 > top of Java-index,Java Essentials,Java Programming...
# 18

> 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?

keeskista at 2007-7-21 21:55:05 > top of Java-index,Java Essentials,Java Programming...
# 19

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

juniorprogrammera at 2007-7-21 21:55:05 > top of Java-index,Java Essentials,Java Programming...
# 20

> - - - > 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 > top of Java-index,Java Essentials,Java Programming...
# 21

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

juniorprogrammera at 2007-7-21 21:55:05 > top of Java-index,Java Essentials,Java Programming...
# 22

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());

}

kevjavaa at 2007-7-21 21:55:05 > top of Java-index,Java Essentials,Java Programming...
# 23
hi.. thanks alot for all the help.. i managed to find the solution :)jp
juniorprogrammera at 2007-7-21 21:55:05 > top of Java-index,Java Essentials,Java Programming...