java(beginners)problem undefined twodimensional array
hey i don't now whether i can post java-problems here, i'm also looking on the real java-sites of course but i thought ,
who knows the solution comes falling from the sky ...
If I create an array lets say
anyarray [i] [j]
I have to initialise i and j ? (yes i'm a beginner ! !)
But suppose i don't know how many colums i'll have, what do i do then ?
I tried to set it higher like 1000 but when i ask to give me the list later on, then i get hundreds of empty ones (1000 minus the ones i filled)
Or can i first initialise it high like 1000 and later -before asking the list- re-ajust it and how should i do that?
If necesarry i can copy what i've written (but the text itself is all in french, might be confusing?)
Err. Not sure what the question is here.
if i and j are ints, they need to have a value.
Obviously I need to spend more time programming because I don't know exactly if this is the right thing or not, but to initialize an array I THINK it's
Object[][] array = new Object[x][y];
This will give you an a two dimensional array, of size [x][y]. The rows/columns part has kind of of always confused me though.
x and y in this case could be variables, or just number (like, say 1 and 1. Or if you're feeling existential, make a two dimensional array of size 0,0)
It of course, starts with all elements in the array set to null.
IF you are getting empty results, either your code is not setting the results right, or it is not displaying them correctly
i don't have any problem initialising the arrays but a problem with the list of the coloms that are filled (i think i solved it by separating them, the variable for the list and the array)
but now i still have got a problem with the reading of each colom , it only displays the last one and leaves the rest blank (so if i insert three names i get : first the last and than two blank one (null))
so it does count but doesn't put the names in each colom.
i just copy the code, it might be easier to understand?
Lire.S is a class that reads the value of what is typed ,case 3 isn't finished yet, the problem is in case 1 (the reading of the array) or in case 2 (the writing of the list )
public class Projet
{
public static void main(String [] args)
{
int n,i,j,k,choix,som,duree,h,m,z;
char s='o';
i=0;
j=3;
z=0;
k=1000;
choix=0;
String [][] tab_woord;
tab_woord = new String[j][k];
{
while ((s=='o')||(s=='O'))
{
k=0;
System.out.println ("1 Entrer un titre ");
System.out.println("2 Liste des titres");
System.out.println("3 Taille totale");
System.out.println("4 Quitter");
System.out.print("Voulez-vous entrer un nouveau titre (1), Consulter la liste (2), Connaître la taille totale (3) ou Quitter ?(4) ");
choix =Lire.i();
switch(choix) {
case 1:
j=0;
System.out.print("Entrer le nom de l’artiste: ");
tab_woord[j][k]= Lire.S();
j++;
System.out.print("Entrer le titre de l'album ");
tab_woord[j][k]= Lire.S();
j++;
System.out.print("Entrer la taille ");
tab_woord[j][k]= Lire.S();
z++;
break; do i lose the increased values for z?
case 2:
for (k=0;k<z;k++){ here i plan to re-use the value of z to make sure i don't get the empty values in my list
{
System.out.println("Artiste: "+tab_woord[0][k]);
System.out.println("Titre: "+tab_woord[1][k]);
System.out.println("Taille: "+tab_woord[2][k]);}
}
case 3:
som=0;
while (i<=k){
som=som+
i++;
}
duree=som/1024;
h=duree/60;
m=h/60;//encore à faire
System.out.println("Taille totale: "+som+", durée: "+h+"h "+m+"m ");
case 4:
s='n';
break;
default:
System.out.print("Voulez-vous continuer ? O/N ");
s=Lire.c();
}
}
}
}
}
Similar Topics | |
---|---|
Pride! (and a problem!) |
13 May 2025, 8:52 pm |
Help, what's my problem? Academic failure and not diagnosed. |
29 Jun 2025, 3:08 pm |