Results 1 to 14 of 14
Thread: EXTREMELY simple question
- 09-23-2011, 03:16 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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
- 09-23-2011, 03:59 PM #2
Re: EXTREMELY simple question
Somewhere you are trying to index an array with less than 11 elements.java.lang.ArrayIndexOutOfBoundsException: 10
Array indexes start at 0.
-
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.
- 09-23-2011, 05:06 PM #4
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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.
Where exactly should I be looking for my problem?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(); } } }Last edited by Norm; 09-23-2011 at 05:48 PM. Reason: added code tags
-
Re: EXTREMELY simple question
Last edited by Fubarable; 09-23-2011 at 05:22 PM.
- 09-23-2011, 05:17 PM #6
Re: EXTREMELY simple question
At line 40.at CharacterCanvas.setChar(CharacterCanvas.java:40)
- 09-23-2011, 05:23 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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?
- 09-23-2011, 05:25 PM #8
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Re: EXTREMELY simple question
Nevermind, I found it. Thanks for the help and sorry for being a dumbass
- 09-23-2011, 05:28 PM #9
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Re: EXTREMELY simple question
scratch that, I'm still lost
-
- 09-23-2011, 05:41 PM #11
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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
- 09-23-2011, 05:51 PM #12
Re: EXTREMELY simple question
The trace shows that the main method calls the drawLine method which calls the setChar method where the exception happens.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)
What are the arguments in the drawLine method?
How do they relate to the size of the array?
-
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).
- 09-23-2011, 06:02 PM #14
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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
-
Please help. Simple question
By owencain in forum New To JavaReplies: 14Last Post: 06-16-2011, 01:07 AM -
Simple Question
By stackptr89 in forum New To JavaReplies: 13Last Post: 01-29-2011, 05:35 PM -
Digit sum of extremely large numbers
By sunde887 in forum New To JavaReplies: 2Last Post: 01-26-2011, 10:00 PM -
some simple question?
By jperson in forum New To JavaReplies: 4Last Post: 05-03-2010, 05:32 PM -
Extremely fast applet
By FerretSpy440 in forum Java AppletsReplies: 0Last Post: 07-17-2009, 04:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks