• Post Reply Bookmark Topic Watch Topic
  • New Topic

output problem  RSS feed

 
Joseph Fiddler
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My program is supposed to print (from another class called SquareAndCubeApp) a range of numbers with their square and cube (this must also be done on the command line/terminal). When I enter 0 and 5, it works fine. 0 and 6, no problem, but 1 and 6 causes a problem. Can anyone figure out why?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think there's a problem?

(Read our FAQ entry TellTheDetails -- follow that link -- which explains why you should tell us what's going on.)
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and welcome to the Ranch!
 
Joseph Fiddler
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well.. when I run it with 1 and 5, it gives me an array error. I am in a java short course by the way, and this is day two. I might withdraw from the class and take it later. I only have a year of c++ under my belt and I dont feel too prepared for java. I still want to try to finish my homework, its kinda the determining factor lol.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An "array error"?

Come on, tell us the details. You got a stack trace, right? It tells you what line of code threw the exception, which is useful information. You should be looking at this information to figure out your problem, but anyway why don't you start by pasting it here?
 
Joseph Fiddler
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at SquareAndCube.squareNCube(SquareAndCube.java:12)
at SquareAndCubeApp.main(SquareAndCubeApp.java:16)


This is what I get. I am gonna post SquareAndCubeApp, which is where my main method is located

 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, that's better. So it's saying that you are using array index 6, and that's out of range for the array. The key line of the stack trace is

at SquareAndCube.squareNCube(SquareAndCube.java:12)


which says that line 12 was where the exception was thrown. Line 12 is this one:



So that means that x is 6, but the array only accepts indexes from 0 to 5, or less than 5. Check how the array is declared to find out how big it is. Your code is pretty C-like, so hopefully you can take it from there.
 
Joseph Fiddler
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then why won't it run 1 and 5? It gives the same error message.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!