• Post Reply Bookmark Topic Watch Topic
  • New Topic

Two Dimensional Array Trouble  RSS feed

 
Jordan Ruetz
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
So I am pretty new to Java and am just learning about two dimensional arrays. I think that I understand the concept, but I seem to be having trouble adding stuff to my array. I wanted to make an array to hold both strings and integers, but wasn't sure if I could put integers in a string array. So I thought that I would be able to convert my integers to string and then add them. This however causes an error. I was wondering how to do this to fix my problem and why this problem occurs. This is my code(yes its probably not the best):



Thank you kind folks!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the error message actually tell you what the problem is. It helps us TREMENDOUSLY if you post the exact error you get, since we can't compile this code.

Even without that, I can tell you that this line will be a problem:

students[number][4] = d;

Arrays are 0-base. So if you have a four element array, they are 0 - 3, and not 1-4. But my guess is there are other issues that are currently masking this.
 
Jordan Ruetz
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh, that fixed my problem. For some reason, I thought that the 4 meant that the array would go 0 - 4 and not 0 - 3. Thanks alot for your help!
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jordan Ruetz wrote:I wanted to make an array to hold both strings and integers, but wasn't sure if I could put integers in a string array.


Technically you can have an array that has both Strings and Integers in it, but it's almost certainly a bad idea so you're right not to try and do that.

Jordan Ruetz wrote:So I thought that I would be able to convert my integers to string and then add them.


This will allow you to put the int with the Strings into a String[] array, but you're going to have a problem if you want to use the value as an int (because it's now a String). In general the better solution would be to define a Class that contains the fields you want to store, and then store instances of that class. That way you would not need to use a 'multidimensional' array.

Jordan Ruetz wrote: This however causes an error. I was wondering how to do this to fix my problem and why this problem occurs. This is my code(yes its probably not the best)!


Well done for putting the code into code tags. However when you get errors it is best if you show us exactly what the error was. Did you get a compilation error (in which case print it here). Did you get an error at runtime? (In which case print the full stack trace). And in cases where the code simply doesn't do what you expected then tell us what you did expect, and what actually happened.

In this case however I can see an error that is likely what is causing you problems:



You declared your array to be 4 items long, but you have put 5 items into it. Array indexes begin at 0, so anArray[4] is indexing the 5th element in an array.


Edit: That's what happens if you start typing a reply and then don't submit it immediately! Your response wasn't there when I started Fred
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jordan Ruetz wrote:So I am pretty new to Java and am just learning about two dimensional arrays

Technically speaking Java doesn't have 2 dimensional arrays, it has arrays of arrays. A good explanation of this can be found here.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!