Results 1 to 14 of 14
  1. #1
    T.J. is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default EXTREMELY simple question

    ok, so I am trying to write a small program with an output of a 10 by 10 rectangle. I'm using vi and terminal on mac osx 10.7.x (not sure if that matters)

    I can compile my program but cannot run it.

    Here is my program

    import java.io.PrintStream;

    public class Picture {
    public static void main ( String [] args ) {

    CharacterCanvas canvas = new CharacterCanvas (10, 10 );
    canvas.drawLine (0,0,0,10, '|');
    canvas.drawLine (0,0,10,0, '-');
    canvas.drawLine (0,10,10,10, '-');
    canvas.drawLine (10,0,10,10, '|' );
    canvas.outputCanvas ( System.out );
    }
    }


    and here is my error when trying to run it

    % java Picture
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at CharacterCanvas.setChar(CharacterCanvas.java:40)
    at CharacterCanvas.drawLine(CharacterCanvas.java:61)
    at Picture.main(Picture.java:7)


    Now I know this is a simple program, I am currently taking my first cs course at Uni and I have no experience. But I am a quick learner. I have more to write in this but can't until I first run this program.

    Any help is appreciated

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default Re: EXTREMELY simple question

    java.lang.ArrayIndexOutOfBoundsException: 10
    Somewhere you are trying to index an array with less than 11 elements.
    Array indexes start at 0.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: EXTREMELY simple question

    The problem is in how you're using the code that you're not showing us, the CharacterCanvas class. The error message tells us that the problem is occurring on the 40th line of this class:

    % java Picture
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at CharacterCanvas.setChar(CharacterCanvas.java:40)
    at CharacterCanvas.drawLine(CharacterCanvas.java:61)
    at Picture.main(Picture.java:7)

    So you're initiating CharacterCanvas with two int parameters of 10 each, and perhaps you should check what the largest number is that can be used when calling the CharacterCanvas's drawLine method.

  4. #4
    T.J. is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: EXTREMELY simple question

    Ok, So i tried rewriting my picture class to output a rectangle of 11x11, it works. So the previous suggestions were correct. The CharacterCanvas I'm using is this.

    Java Code:
    import java.io.PrintStream;
    
    /**
     * CharacterCanvas provides a character canvas for drawing individual
     * characters and lines.
     *
     * @author Rod Byrne
     * @version 0.01
     */
    public class CharacterCanvas {
    
        private char[][] canvas;
        private int rowSize;
        private int columnSize;
    
        /**
         * CharacterCanvas creates a canvas filled with ' ' characters.
         * @param columns number of columns in the canvas
         * @param rows number of rows in the canvas
         */
        public CharacterCanvas( int columns, int rows ) {
            rowSize = rows;
            columnSize = columns;
            canvas = new char[columns][rows];
    
            for( int r = 0 ; r < rowSize; r += 1 ) {
                for( int c = 0 ; c < columnSize; c += 1 ) {
                    setChar( c, r, ' ' );
                }
            }
        }
    
        /**
         * setChar updates a position on the canvas with a character.
         * @param c specifies the column position (across page)
         * @param r specifies the row position (down page)
         * @param ch specifies the character to update
         */
        public void setChar(int r, int c, char ch ) {
            canvas[c][r] = ch;
        }
    
        /**
         * drawLine draws a line between two position with the provide character.
         * @param c1 specifices column of first position
         * @param r1 specifices row of first position
         * @param c2 specifices column of second position
         * @param r2 specifices row of second position
         * @param ch specifices the character for the line
         */
        public void drawLine( int c1, int r1, int c2, int r2, char ch ) {
            int dc = Math.abs( c1 - c2 );
            int dr = Math.abs( r1 - r2 );
            int sc = ( c1 < c2 ) ? 1 : -1;
            int sr = ( r1 < r2 ) ? 1 : -1;
            int err = dc - dr;
    
            int c = c1;
            int r = r1;
            while ( true ) {
                setChar( c, r, ch );
                if ( c == c2 && r == r2 ) break;
                int err2 = 2 * err;
                if ( err2 > -dr ) {
                    err -= dr;
                    c += sc;
                }
                if ( err2 < dc ) {
                    err += dc;
                    r += sr;
                }
            }
        }
    
        /**
         * outputCanvas sends the canvas to the output
         * @param ps is the print stream for output
         */
        public void outputCanvas( PrintStream ps ) {
            for( int r = 0 ; r < rowSize; r += 1 ) {
                for( int c = 0 ; c < columnSize; c += 1 ) {
                    ps.print( canvas[c][r] );
                }
                ps.println();
            }
        }
    }
    Where exactly should I be looking for my problem?
    Last edited by Norm; 09-23-2011 at 05:48 PM. Reason: added code tags

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: EXTREMELY simple question

    Quote Originally Posted by T.J. View Post
    Where exactly should I be looking for my problem?
    We already told you in my post above. The error message tells you exactly where the error is occurring.
    Last edited by Fubarable; 09-23-2011 at 05:22 PM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default Re: EXTREMELY simple question

    at CharacterCanvas.setChar(CharacterCanvas.java:40)
    At line 40.

  7. #7
    T.J. is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: EXTREMELY simple question

    Yes, maybe I didn't state myself clearly. Sorry, I can read line 40, I however do not know how to change the variables to accept a rectangle of smaller size.

    Public void setChar( int r, int c, char ch ) {

    I assume this is referring somehow back to

    for( int r = 0 ; r < rowSize; r += 1 ) {
    for( int c = 0 ; c < columnSize; c += 1 ) {
    setChar( c, r, ' ' );

    I would not bother asking if I didn't have to hand this assignment in soon, and I've already exhausted my time in the student help centre.

    What would I have to do in order to make my program run?

  8. #8
    T.J. is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: EXTREMELY simple question

    Nevermind, I found it. Thanks for the help and sorry for being a dumbass

  9. #9
    T.J. is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: EXTREMELY simple question

    scratch that, I'm still lost

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: EXTREMELY simple question

    Quote Originally Posted by T.J. View Post
    scratch that, I'm still lost
    Please tell us how you're lost.

  11. #11
    T.J. is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: EXTREMELY simple question

    I do not understand what an Arrayindex is. I only think in layman's terms. What I believe to be happening is that the CharacterCanvas program is mandating that my Picture.java have an output of 11x11 or larger.

    Say I change my
    CharacterCanvas = new CharacterCanvas ( 10, 10 ) ;
    to
    CharacterCanvas = new Charactercanvas (15, 15) ;

    the program runs fine, however this is no good to me as our instructor is making us pass a class test before we can submit the Picture.java. The class test will not pass us unless the boundaries of our output rectangular is 10x10.

    Am i wrong to think that because the canvas I have set is 15 x 15 that I will fail the class test even though the outputted box measures 10 x 10.

    Ideally I would like to make a change to the CharacterCanvas.java to accept a smaller output from Picture.java but I don't know how.


    If none of this is making sense let me know

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default Re: EXTREMELY simple question

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at CharacterCanvas.setChar(CharacterCanvas.java:40)
    at CharacterCanvas.drawLine(CharacterCanvas.java:61)
    at Picture.main(Picture.java:7)
    The trace shows that the main method calls the drawLine method which calls the setChar method where the exception happens.

    What are the arguments in the drawLine method?
    How do they relate to the size of the array?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: EXTREMELY simple question

    The parameters passed into your CharacterCanvas constructor just tells it the maximum size of the character drawing it can make. If you pass in 10 and 10, then it can make anything up to 10 x 10 in size, but no bigger. Note that to make something 10 x 10, you would start at 0 and go to 9 because Java arrays start at 0 (as Norm states above).

  14. #14
    T.J. is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: EXTREMELY simple question

    Ok, I figured it out thanks to that last comment.
    It slipped my mind that java arrays start at 0, so I used the drawLine to draw from coordinate 0 to 10 I was actually trying to step outside of the boundaries I had just set. I thought 0 to 10 would have been 10 instead of 11 spaces. Thank you Norm and Fubarable.

    I know what its like to answer stupid questions and reply to uneducated statements through my job and I appreciate your patience and help!

Similar Threads

  1. Please help. Simple question
    By owencain in forum New To Java
    Replies: 14
    Last Post: 06-16-2011, 01:07 AM
  2. Simple Question
    By stackptr89 in forum New To Java
    Replies: 13
    Last Post: 01-29-2011, 05:35 PM
  3. Digit sum of extremely large numbers
    By sunde887 in forum New To Java
    Replies: 2
    Last Post: 01-26-2011, 10:00 PM
  4. some simple question?
    By jperson in forum New To Java
    Replies: 4
    Last Post: 05-03-2010, 05:32 PM
  5. Extremely fast applet
    By FerretSpy440 in forum Java Applets
    Replies: 0
    Last Post: 07-17-2009, 04:12 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
  •