• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

output problem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and welcome to the Ranch!
 
Joseph Fiddler
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then why won't it run 1 and 5? It gives the same error message.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic