|
-
Dec 18th, 2001, 11:59 AM
#1
Thread Starter
Hyperactive Member
plz look at this code
i get the error [B]undefined label: newHost[\B]
but not a similar error for label intError
Code:
import java.io.*;
import java.net.*;
public class Client {
public static void main (String args []) throws IOException {
BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in));
[I]newHost:[\I] System.out.print ("connect to IP: ");
String host = keyboard.readLine();
int port= 0;
[I]intError: [\I]try {
port = Math.abs (Integer.parseInt (keyboard.readLine()));
}
catch (NumberFormatException e) {
System.out.println ("plz enter a valid port num");
break intError;
}
try {Socket connection = new Socket (host, port);}
catch (UnknownHostException e){
System.out.println ("Host can not be found\n");
break newHost;
}
}//end main
}//end Client
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Dec 18th, 2001, 01:30 PM
#2
Addicted Member
That's interesting code. I can't help too much, except to parrot an excerpt from Bruce Eckel's very good book (available online) "Thinking in Java". It looks pertinent tho:
A label is an identifier followed by a colon, like this:
label1:
The only place a label is useful in Java is right before an iteration statement. And that means right before—it does no good to put any other statement between the label and the iteration. And the sole reason to put a label before an iteration is if you’re going to nest another iteration or a switch inside it. That’s because the break and continue keywords will normally interrupt only the current loop, but when used with a label they’ll interrupt the loops up to where the label exists:
label1:
outer-iteration {
inner-iteration {
//...
break; // 1
//...
continue; // 2
//...
continue label1; // 3
//...
break label1; // 4
}
}
In case 1, the break breaks out of the inner iteration and you end up in the outer iteration. In case 2, the continue moves back to the beginning of the inner iteration. But in case 3, the continue label1 breaks out of the inner iteration and the outer iteration, all the way back to label1. Then it does in fact continue the iteration, but starting at the outer iteration. In case 4, the break label1 also breaks all the way out to label1, but it does not re-enter the iteration. It actually does break out of both iterations.
-
Dec 18th, 2001, 02:38 PM
#3
Dazed Member
I see what you are trying to do. It looks like if an invalid host or port is detected then you want an exception to be thrown and program execution to continue at the given label. If this is your first draft i would just scrap it and start over with a new program structure. I do this all of the time.
-
Dec 18th, 2001, 03:09 PM
#4
Dazed Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|