I am pretty new with java, I've been reading about it the last month and well I am practicing, so please be patient with me!

So, I have this array matrizAsignaturas and I am trying to ask the user for a number and depending on the int received then print the column information. I am not sure what I am doing wrong here, but I get (I already looked for this error, but not sure if a quite understand what it means):

Hello.
Where is the starting point of execution in your program?
Probably you did not invoke the method that initializes the array.
So before accessing array please initialize it first.

Syed.

August 14th, 2013, 05:31 AM

GregBrannon

Re: Array

What's the difference between String[4][1] and String[4]?

To Syed's point: Is the method matrizAsignaturas() ever called? That is how the matrix is initialized, replacing the null values with the specified strings.

August 14th, 2013, 07:39 AM

syedbhai

Re: Array

Hello.
Q). What's the difference between String[4][1] and String[4]?
A). String[4] refers to 5th element in a one dimensional array.
String[4][1] refers to 2nd element of 5th row in a two dimensional array.

And I wasn't really expecting an expert to answer. I thought thinking about it might help the OP think about and visualize the problem (and maybe see the solution) differently.

August 14th, 2013, 08:55 AM

jps

Re: Array

Quote:

Originally Posted by valam

I am not sure what I am doing wrong here

The method verMatriz returns a value, which is the int verMatriz, which was never initialized. As a side note naming a variable exactly the same as a method may lead to confusion

August 14th, 2013, 04:39 PM

valam

Re: Array

Thank you all for your answers...

I think I see where the problem is... I will be back in a few after I try :)

@jps You are right, I shouldn't be doing that. What exactly is the return anyways, not sure exactly why it is needed. I kinda understand the concept but not exactly what it does.

August 14th, 2013, 04:42 PM

GregBrannon

Re: Array

I like your answer too. One IS 1D and the other 2D, but it's also added complexity for the same storage capacity and no benefit that I can see. I suggest avoiding unnecessary complexity whenever possible.

August 14th, 2013, 05:27 PM

valam

Re: Array

Ok, so I think I am pretty confused here... At the begining I wanted to create a 2D array, now thanks to your comments I realized it was actually 1D, and I fixed that already...

Now, what I trying to do is to request a number from the user and depending on their answer (0-3), then It will show the info that is contained there...

So some questions (I'm sorry if there are dumb and noob, but I'll get there I promise! :) )

1 - preguntaMatriz() all it does is to get a number ( 0-3 ), now since the array is declared as String then can I mix both?, I know that I am not suppose to have in array different types (char, float, int, etc)

2- After requesting the number, do I really need a loop? I mean, at this point we already know the position in the array we need, so not sure if the loop is needed in this case. I think it does not do anything. So I removed it, but still get the same error, so again I am more lost!

3- When I read the error, it point to line 42 and 34, so it does lead to my question # 1, one is String and the other one is int.. is this what is causing the problem?

Here is the code again:

--------------------------------

package Trabajo;

import javax.swing.JOptionPane;

public class Asignaturas {

String matrizAsignaturas[];

public int Programa;
public String a = "";
public int verMatriz;
public int i=0;

We need to see the driving or testing program - the one with the main() method - to see and explain the errors you're seeing. An alternative would be for you to post the errors that you're seeing, copied and pasted just as they appear at your end.

This is what we've been trying to explain to you: the method matrizAsignaturas() is never called, and it's responsible for populating the array matrizAsignaturas[] with values other than null. I suggest you call the method in a constructor:

Code java:

public Asignaturas(){
matrizAsignaturas();}

but there are other ways it could be done. For example, the main() method could call it: