Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int [] [] ??

 
billy ghot
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a method to write, when the method is called in the main program, it is supposed to prompt the user for how many rows in the array?... and if they say three, the it sets up a "int [] [] a A = new int [3][?];" i suppose. then its suppose to prompt them for the values of each row.
and then return the average.

now this is my code so far.


 
billy ghot
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and, how would i call to that method??....

TwoDAverage; doesn't work?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a number of things preventing this from working -- some of which the compiler will point out when you try to compile.

One of the bigger issues is that before you can use an array, you need to instantiate it. In this case, you have a 2-d array -- which in Java is really just an array of arrays. You've set the first dimension as A, but you never set the second. Therefore, you have an array (of length A) that contains null references instead of other arrays.

Take a shot at fixing the array issue (taking a careful look at the rest of your code along the way), and then see where you are.
 
billy ghot
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what you're saying is

int [][] A = {{3,4,5},
{4,5,6},
{7,8,}};

each row is an individual array?....
if that is so, how do i store values in each one?
like A[i][j];

thanks, justin
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by billy ghot:
so what you're saying is

int [][] A = {{3,4,5},
{4,5,6},
{7,8,}};

each row is an individual array?....
if that is so, how do i store values in each one?
like A[i][j];

thanks, justin

In this example, A represents an array, and A[i] represents an array that's an element of A. For example, A[0] = {3, 4, 5}.

Then A[i][j] represents element j in A[i]. For example, A[0][2] = 5.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some other things to consider:
  • Variables (like C) need to be declared before you can assign anything to them.
  • Methods with a non-void return type need to have a return statement.
  • What exactly is "while(A != 0)" doing? (Hint: When you get trapped in an infinite loop, press Ctrl+C to get out.)
  • What exactly are you doing with C?
  • Once compiled, how are you running this? (Do you have a main method anywhere? Do you have an instance of this class?)
  •  
    Bert Bates
    author
    Sheriff
    Posts: 8900
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey Billy,

    Welcome to the ranch, we're glad you're here, and we're glad you're having a good discussion.

    I'd like to ask you to update your display name! We've found that if people use their real names things stay a lot friendlier

    Thanks,

    Bert
     
    billy ghot
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i understand that....

    when i put A[i][j] = Integer.parseInt(C);
    will that work?...
    or is there an insert method in the array class
    and how do i get my method (TwoDArray) to where i can call it from the main.

    and yes i have a main method, im just a newb and i cant figure out how to call it from the main.
    if i put a break after the last for loop, it would only do it once...

    thanks, justin

    [ November 22, 2005: Message edited by: billy ghot ]
    [ November 22, 2005: Message edited by: billy ghot ]
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 21510
    84
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The problem with the original source is that you allocated one of the "dimensions" of the array, but not the other. You need to allocate both "dimensions" of the 2 dimensional array.

    As for using the { } syntax of arrays, that only work if you know what the value is at compile time. You can't use that syntax if you don't know the value or the size of the array.

    Henry
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here's a link to Sun's tutorial page...

    http://java.sun.com/docs/books/tutorial/

    In particular, you might find the arrays section helpful...

    http://java.sun.com/docs/books/tutorial/java/data/arrays.html
     
    billy ghot
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ok, so how am i supposed to set the value to the A[3][?] when i dont know how many rows there are going to be, thats up to the user...

    would i set it to int i, and then put it in the for loop?

    i need help, not directions, so if you could i would be very grateful

    thx, Justin Billy Ghot..
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 21510
    84
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You actually have it almost correct... you just didn't finish it.

    To allocate one dimension of a two dimensional array:

    int[][] A = new A[3][];

    Unfortunately, if you do it this way, you will need to allocate the other dimension for each member:

    A[0] = new A[7];
    A[1] = new A[6];
    A[2] = new A[14];

    Hope this helps,
    Henry
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by billy ghot:
    ok, so how am i supposed to set the value to the A[3][?] when i dont know how many rows there are going to be, thats up to the user...

    It depends, in part, on how exactly the user specifies this. This is what I meant above by "what exactly are you doing with C?"

    For example, I'm guessing that C is a String like "4, 12, 9, 11." If that's the case, then you're going to need to figure out how to break it up into parts. (Integer.parse(C) won't do it.) And because an array's size is set at the time of instantiation, you're going to need to know how many pieces there are before you put any of them in the array. On the other hand, maybe my guess is wrong, and that's not what you have in mind.

    I think it would help you -- as well as anyone helping you -- if you could describe in words what each step should be. That is, exactly what form the user would enter input, and exactly what your program should do with it.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic