This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes int [] [] ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "int [] [] ??" Watch "int [] [] ??" New topic
Author

int [] [] ??

billy ghot
Greenhorn

Joined: Nov 22, 2005
Posts: 7
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

Joined: Nov 22, 2005
Posts: 7
and, how would i call to that method??....

TwoDAverage; doesn't work?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
billy ghot
Greenhorn

Joined: Nov 22, 2005
Posts: 7
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 31, 2004
Posts: 11343

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

    Joined: Oct 14, 2002
    Posts: 8815
        
        5
    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


    Spot false dilemmas now, ask me how!
    (If you're not on the edge, you're taking up too much room.)
    billy ghot
    Greenhorn

    Joined: Nov 22, 2005
    Posts: 7
    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
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18754
        
      40

    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


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    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

    Joined: Nov 22, 2005
    Posts: 7
    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
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18754
        
      40

    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

    Joined: Aug 31, 2004
    Posts: 11343

    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.
     
    Consider Paul's rocket mass heater.
     
    subject: int [] [] ??