Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Norm

Thread: What to do here(cant compile)?

  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default What to do here(cant compile)?

    import java.util.*;
    import java.io.*;

    Java Code:
    class mainprogram
    {
    	//These are available for the whole class
    	static InputStreamReader osw = new InputStreamReader (System.in, "Cp850");
    
    ...
    }

    Java Code:
    C:\Kryptator\mainprogram.java:9: unreported exception java.io.UnsupportedEncodingException; must be caught or declared to be thrown
    	static InputStreamReader osw = new InputStreamReader (System.in, "Cp850");
    	                               ^
    1 error
    
    Tool completed with exit code 1
    They want me declare the exception, but how? I know how to do it if it was inside a method, but outside a method?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

  3. #3
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    New "cant compile" issue. This one dont make sense, it seems like the compiler is blind.

    PHP Code:
    		Loop1:
    		while (jtemp <= bigFile.length ())
    		{
    			FLoop:
      			while (false)
    			{
    				finalString = finalString.append (temp);
    				itemp += 12;
    				jtemp += 12;
    				continue Loop1;
    			}
    
    			temp = bigFile.toString ().substring (itemp, jtemp);
    
    			//DEBUG
    			System.out.println (temp);
    
    			System.gc ();
    
    			for (int j = 0; j < theChars.length + 2; j++)
    			{
    				Loop2:
    				for (int l = 0; l < 100; l++)
    				{
    					if (temp.equals (key[j][l]))
    					{
    						if (j == theChars.length)
    						{
    							temp = temp.replace (temp, "[unsupported character]");
    							continue FLoop;
    						}
    
    						if (j == theChars.length + 1)
    						{
    							temp = temp.replace (temp, "\n");
    							continue FLoop;
    						}
    						tempChar = theChars[j] + "";
    						temp = temp.replace (temp, tempChar);
    						continue FLoop;
    					}
    
    					else
    						continue Loop2;
    				}
    			}
    		}

    Java Code:
    C:\Program\Java\jdk1.6.0_23\jre\classes\pjjava\crypt\cryptMethods.java:255: undefined label: FLoop
    							continue FLoop;
    							^
    C:\Program\Java\jdk1.6.0_23\jre\classes\pjjava\crypt\cryptMethods.java:261: undefined label: FLoop
    							continue FLoop;
    							^
    C:\Program\Java\jdk1.6.0_23\jre\classes\pjjava\crypt\cryptMethods.java:265: undefined label: FLoop
    						continue FLoop;
    						^
    3 errors
    
    Tool completed with exit code 1

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,585
    Rep Power
    23

    Default

    As good an example of goto code as I've seen in years.
    The continue FLoop is not within a loop where FLoop: is defined. FLoop is just floating around inside of the outer loop. FLoop needs to be on an outer loop that contains the continue.
    JeffGrigg likes this.

  5. #5
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Quote Originally Posted by Pojahn_M View Post
    Java Code:
    class mainprogram
    {
    	//These are available for the whole class
    	static InputStreamReader osw = new InputStreamReader (System.in, "Cp850");
    	...
    }
    You need to put this initialization code in the constructor, where you can deal with the possible exception.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Quote Originally Posted by JeffGrigg View Post
    You need to put this initialization code in the constructor,
    Not necessarily.

    Quote Originally Posted by JeffGrigg View Post
    where you can deal with the possible exception.
    That part is correct.

    Now why was I dumb enough to search through the JLS and post a link to the section that accurately describes how to deal with checked exceptions from an instance initializer, hoping someone would actually click the link and learn something in the process.

    db

  7. #7
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    As good an example of goto code as I've seen in years.
    The continue FLoop is not within a loop where FLoop: is defined. FLoop is just floating around inside of the outer loop. FLoop needs to be on an outer loop that contains the continue.
    goto code, what is that?
    btw, I didnt quite get understand you. Can you please reexplain?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,585
    Rep Power
    23

    Default

    Basically you need to move the FLoop label to be at the beginning of a loop that contains the continue FLoop statement. The loop where it is now does not contain the continue FLoop statement.
    You are trying to 'goto' the FLoop statement.

    For more on goto, ask Google

  9. #9
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Quote Originally Posted by DarrylBurke View Post
    ... post a link to the section that accurately describes how to deal with checked exceptions from an instance initializer, ...
    You are correct, good sir.

    For the readers, see "8.6 Instance Initializers" by Following This Link (and scroll up just one click.)

    Still, to throw the checked exception with Instance Initializers, one would have to have at least one constructor, and to declare the checked exception in all of them.

    To not throw the checked exception, the instance initializer block would have to have a try-catch block that did "something appropriate" with the exception -- whatever that might be. (Print stack trace and eat it? Wrap it in a RuntimeException?)

    Personally, I avoid static and instance initializer blocks, except in extreme circumstances. And when they are the best solution to the problem, I usually find that the code really needs to be refactored so as to not have that kind of problem anyway.

    Given the original example, this is what an initializer block would look like:
    Java Code:
    import java.io.*;
    
    class mainprogram {
    
    	static InputStreamReader osw;
    
    	static {
    		try {
    			osw = new InputStreamReader(System.in, "Cp850");
    		} catch (UnsupportedEncodingException ex) {
    			ex.printStackTrace();
    		}
    	}
    }
    Last edited by JeffGrigg; 08-06-2011 at 02:01 AM. Reason: Fix [CODE] markup, and URL link.

Similar Threads

  1. why i can not compile? please help
    By zypchun in forum New To Java
    Replies: 4
    Last Post: 09-29-2013, 10:11 PM
  2. would this compile?
    By stringkilla in forum New To Java
    Replies: 10
    Last Post: 10-24-2010, 03:27 PM
  3. Can't Compile
    By sidk47 in forum JavaServer Pages (JSP) and JSTL
    Replies: 7
    Last Post: 06-15-2010, 04:43 PM
  4. Help with compile
    By mr_anderson in forum NetBeans
    Replies: 7
    Last Post: 06-10-2010, 04:03 AM
  5. Not able to compile
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-09-2008, 10:13 PM

Posting Permissions

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