Results 1 to 4 of 4

Thread: plz look at this code

  1. #1

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    Exclamation 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

  2. #2
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    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.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Try using the crab claw ie..... for(; I perfer lobster myself but.... The code below loops until either an "x" or an "X" is entered. If a MalformedURLException is thrown the method just propagates back to it's caller which is contained within the endless for(; loop.
    Code:
      import java.io.*; 
      import java.net.*; 
    
      public class URLReader{
       public static void main(String[] args){
     
      System.out.println("Welcome to the URL Reader Program \n");
      System.out.println("Please Enter a URL.Note~* Url must be in http:// format!\n");
      System.out.println("To exit the program just press x or X\n");
      
      try{
      BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
     
      for(;;){
       String url = buff.readLine();
        if(url.equals("x") || url.equals("X")){
          System.exit(0);
        }
       startReader(url);
           
      } 
       }catch(IOException e){System.out.println(e);}
      }  
    
      public static void startReader(String url){
       try{
         URL u = new URL(url);
         InputStream in = u.openStream();
         StreamCopier.copy(in,System.out);
         in.close();
         }
         catch(MalformedURLException e){System.err.println(e);
         } 
         catch(IOException e){System.err.println(e);}  
      }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width