Results 1 to 4 of 4

Thread: Java 2D Array

  1. #1
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Java 2D Array

    I need to make a 2D array out of the 0's and 1's in the file. The file is 25X25 in total so contains 625 characters in all, either 0 or 1.

    This is the code I have so far:


    import london.*;
    public class Picture {
    public static void main (String[] arg) {
    EasyReader fileInput = new EasyReader("picture.txt");
    EasyGraphics g = new EasyGraphics(30,30);

    int cols = 30; // How many columns the array should have
    int rows = 30; // How many rows the array should have

    int[][] array2d = new int[rows][cols];

    String strLine;
    int xCounter = 0;
    // Reading lines from the file using EasyReader
    while ((strLine = fileInput.readLine()) != null) // fileInput.readLine() fills the variable strLine with a String value.
    {
    char[] stringArray= new char[42];
    stringArray = strLine.toCharArray();
    //display the array
    for(int index=0; index < stringArray.length; index++) {
    array2d[xCounter][index] = (int) stringArray[index] - 0x30;
    }
    xCounter++;
    }
    }
    }


    The EasyReader is just a class for input that inherits the BufferedReader class and is part of the "london" package. There is 1 error in compilation which says:


    Picture.java:15: unreported exception java.io.IOException; must be caught or declared to be thrown
    while ((strLine = fileInput.readLine()) != null) // fileInput.readLine() fills the variable strLine with a String value.
    ^

    Please can someone help me, this is driving me mental!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Java 2D Array

    You might want to look at Fubarable's "rep power" and consider whether thanking him for his help in your previous thread (or even letting him know how you got on) would be productive.

    unreported exception java.io.IOException; must be caught or declared to be thrown
    Evidently this EasyReader thingee can throw an IOException when you call the readLine() method. That's not too surprising and it's there to try and deal with all the strange things that can happen when you read files. (someone pulls the cable out of the drive etc. They happen and your program has to do something.)

    What to do is described in detail in the Exceptions section of Oracle's Tutorial.

  3. #3
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: Java 2D Array

    I cant find the bit that is relevent to my code's error?! And I did I sent him a pm!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Java 2D Array

    Ah, sorry. I've kicked myself in the backside for being pompous...

    Your error is dealt with on the Catch or Specify Requirement page.

    Basically, readLine() can throw an IOException. So you must either put that line inside a try block and catch the exception. Something like this:

    Java Code:
    try {
        // ...
        foo.methodThatMayThrowException();
        // ...
    } catch(SomeException se) {
        se.printStackTrace();
        //System.out.println("We got an exception: " + se);
        //System.exit(1);
    }
    In this case it's up to you what you do when you catch the exception: just report it as shown or end the program (commented out) or anything else.

    The alternative is to add a "throws" to the method that contains readLine(). If readLine() causes an IOException then the method will pass that exception back to whoever called it and they can deal with it. In your case an IOException is pretty fatal (basically there is nothing you can do about it) and if you declare main() to throw IOException then if one occurs the program will end and a message will be printed on the console. Probably the best thing to do.

    Java Code:
    public static void main(String arg[]) throws IOException {
        // now calling readLine() is fine
    }
    You will have to figure out where the IOException class is (what package) and import it, of course.

Similar Threads

  1. Java Array
    By lary in forum New To Java
    Replies: 8
    Last Post: 05-10-2011, 11:44 AM
  2. Java Array Help
    By md69holla in forum New To Java
    Replies: 14
    Last Post: 02-15-2010, 07:02 PM
  3. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 07:24 PM
  4. how to convert a Java array to a java stack?
    By pompeez in forum New To Java
    Replies: 2
    Last Post: 08-13-2007, 03:41 PM
  5. Help with array in java
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:03 AM

Posting Permissions

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