There is something wrong with this class (about a 2D array) i made in java












-2















This code bellow declares and initialized and prints a 2D array in console :



    package tp_poo_v1_build1;
public class UI {
public static void main(String args) {
int carte = new int[5][5];


for(int i=0;i<carte.length;i++){
for(int j=0;j<carte[i].length;j++){
carte[i][j]='X';
}
}
for (int i = 0; i < carte.length; i++) {
System.out.print(" | ");
for (int j = 0; j < carte[i].length; j++) {

System.out.print((char)carte[i][j] +" | ");
}
System.out.println();
}
}
}


it outputs :



run:
| X | X | X | X | X |
| X | X | X | X | X |
| X | X | X | X | X |
| X | X | X | X | X |
| X | X | X | X | X |
BUILD SUCCESSFUL (total time: 0 seconds)


So, the problem is that, i wanted to use this code using a class called :




Grille.jave




and so i tried this code below for the Class "Grille"



package tp_poo_v1_build1;
public class Grille {
int nbrL,nbrC;
int carte= new int[nbrL][nbrC];
public Grille(int pNbrL,int pNbrC){ //constructor
nbrL=pNbrL;
nbrC=pNbrC;
/*for(int i:carte)// enhanced for loop ( for each )
for(int j:i)
i[j]='X';*/

for(int i=0;i<carte.length;i++){ // syntax not optmised !
for(int j=0;j<carte[i].length;j++){
carte[i][j]='X';//Char in int 'X' == 88
}
}
}
boolean estLibre(int x,int y) {
return (carte[x][y] == 'X');
}
public void liberer(int x, int y){
carte[x][y]='X';
}
public void occupe(int x,int y,char nom){
//...!
}
public void afficher(){//nbrL=carte.length and nbrC=carte[i].length ?
System.out.println("THE Grill: ");
for (int i = 0; i < carte.length; i++) {
System.out.print(" | ");
for (int j = 0; j < carte[i].length; j++) {
System.out.print(carte[i][j] +" | ");
}
System.out.println();
}
}
}


and for the class that has the main method called :




UI.java




it's code is bellow :



package tp_poo_v1_build1;
public class UI {
public static void main(String args) {
Grille g=new Grille(5,5);
g.afficher();
}
}


it outputs :



run:
THE Grill:
BUILD SUCCESSFUL (total time: 0 seconds)


which is not what i want so if somebody knows what wrong please help.



why is it that when i try to fill the array and print it print using the main method directly, it works fine but when i fill and declare the array using the constructor "Grille" and the method "afficher" to print it, by creating the object "G" of the class "Grille" and then calling the method "afficher" in the main method, it does print the message "The Grill" but not the array like in the previous code ?
i just can't find out why so please help !!



and thank you in advance. (sorry if it's a stupid question im new to java and programming in general 😋 )
i'm new to this site so if my post is wrong please forgive me and help me correct it .










share|improve this question

























  • Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

    – Joe C
    Nov 25 '18 at 20:38











  • ok thanks ill try

    – B15
    Nov 25 '18 at 20:41
















-2















This code bellow declares and initialized and prints a 2D array in console :



    package tp_poo_v1_build1;
public class UI {
public static void main(String args) {
int carte = new int[5][5];


for(int i=0;i<carte.length;i++){
for(int j=0;j<carte[i].length;j++){
carte[i][j]='X';
}
}
for (int i = 0; i < carte.length; i++) {
System.out.print(" | ");
for (int j = 0; j < carte[i].length; j++) {

System.out.print((char)carte[i][j] +" | ");
}
System.out.println();
}
}
}


it outputs :



run:
| X | X | X | X | X |
| X | X | X | X | X |
| X | X | X | X | X |
| X | X | X | X | X |
| X | X | X | X | X |
BUILD SUCCESSFUL (total time: 0 seconds)


So, the problem is that, i wanted to use this code using a class called :




Grille.jave




and so i tried this code below for the Class "Grille"



package tp_poo_v1_build1;
public class Grille {
int nbrL,nbrC;
int carte= new int[nbrL][nbrC];
public Grille(int pNbrL,int pNbrC){ //constructor
nbrL=pNbrL;
nbrC=pNbrC;
/*for(int i:carte)// enhanced for loop ( for each )
for(int j:i)
i[j]='X';*/

for(int i=0;i<carte.length;i++){ // syntax not optmised !
for(int j=0;j<carte[i].length;j++){
carte[i][j]='X';//Char in int 'X' == 88
}
}
}
boolean estLibre(int x,int y) {
return (carte[x][y] == 'X');
}
public void liberer(int x, int y){
carte[x][y]='X';
}
public void occupe(int x,int y,char nom){
//...!
}
public void afficher(){//nbrL=carte.length and nbrC=carte[i].length ?
System.out.println("THE Grill: ");
for (int i = 0; i < carte.length; i++) {
System.out.print(" | ");
for (int j = 0; j < carte[i].length; j++) {
System.out.print(carte[i][j] +" | ");
}
System.out.println();
}
}
}


and for the class that has the main method called :




UI.java




it's code is bellow :



package tp_poo_v1_build1;
public class UI {
public static void main(String args) {
Grille g=new Grille(5,5);
g.afficher();
}
}


it outputs :



run:
THE Grill:
BUILD SUCCESSFUL (total time: 0 seconds)


which is not what i want so if somebody knows what wrong please help.



why is it that when i try to fill the array and print it print using the main method directly, it works fine but when i fill and declare the array using the constructor "Grille" and the method "afficher" to print it, by creating the object "G" of the class "Grille" and then calling the method "afficher" in the main method, it does print the message "The Grill" but not the array like in the previous code ?
i just can't find out why so please help !!



and thank you in advance. (sorry if it's a stupid question im new to java and programming in general 😋 )
i'm new to this site so if my post is wrong please forgive me and help me correct it .










share|improve this question

























  • Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

    – Joe C
    Nov 25 '18 at 20:38











  • ok thanks ill try

    – B15
    Nov 25 '18 at 20:41














-2












-2








-2








This code bellow declares and initialized and prints a 2D array in console :



    package tp_poo_v1_build1;
public class UI {
public static void main(String args) {
int carte = new int[5][5];


for(int i=0;i<carte.length;i++){
for(int j=0;j<carte[i].length;j++){
carte[i][j]='X';
}
}
for (int i = 0; i < carte.length; i++) {
System.out.print(" | ");
for (int j = 0; j < carte[i].length; j++) {

System.out.print((char)carte[i][j] +" | ");
}
System.out.println();
}
}
}


it outputs :



run:
| X | X | X | X | X |
| X | X | X | X | X |
| X | X | X | X | X |
| X | X | X | X | X |
| X | X | X | X | X |
BUILD SUCCESSFUL (total time: 0 seconds)


So, the problem is that, i wanted to use this code using a class called :




Grille.jave




and so i tried this code below for the Class "Grille"



package tp_poo_v1_build1;
public class Grille {
int nbrL,nbrC;
int carte= new int[nbrL][nbrC];
public Grille(int pNbrL,int pNbrC){ //constructor
nbrL=pNbrL;
nbrC=pNbrC;
/*for(int i:carte)// enhanced for loop ( for each )
for(int j:i)
i[j]='X';*/

for(int i=0;i<carte.length;i++){ // syntax not optmised !
for(int j=0;j<carte[i].length;j++){
carte[i][j]='X';//Char in int 'X' == 88
}
}
}
boolean estLibre(int x,int y) {
return (carte[x][y] == 'X');
}
public void liberer(int x, int y){
carte[x][y]='X';
}
public void occupe(int x,int y,char nom){
//...!
}
public void afficher(){//nbrL=carte.length and nbrC=carte[i].length ?
System.out.println("THE Grill: ");
for (int i = 0; i < carte.length; i++) {
System.out.print(" | ");
for (int j = 0; j < carte[i].length; j++) {
System.out.print(carte[i][j] +" | ");
}
System.out.println();
}
}
}


and for the class that has the main method called :




UI.java




it's code is bellow :



package tp_poo_v1_build1;
public class UI {
public static void main(String args) {
Grille g=new Grille(5,5);
g.afficher();
}
}


it outputs :



run:
THE Grill:
BUILD SUCCESSFUL (total time: 0 seconds)


which is not what i want so if somebody knows what wrong please help.



why is it that when i try to fill the array and print it print using the main method directly, it works fine but when i fill and declare the array using the constructor "Grille" and the method "afficher" to print it, by creating the object "G" of the class "Grille" and then calling the method "afficher" in the main method, it does print the message "The Grill" but not the array like in the previous code ?
i just can't find out why so please help !!



and thank you in advance. (sorry if it's a stupid question im new to java and programming in general 😋 )
i'm new to this site so if my post is wrong please forgive me and help me correct it .










share|improve this question
















This code bellow declares and initialized and prints a 2D array in console :



    package tp_poo_v1_build1;
public class UI {
public static void main(String args) {
int carte = new int[5][5];


for(int i=0;i<carte.length;i++){
for(int j=0;j<carte[i].length;j++){
carte[i][j]='X';
}
}
for (int i = 0; i < carte.length; i++) {
System.out.print(" | ");
for (int j = 0; j < carte[i].length; j++) {

System.out.print((char)carte[i][j] +" | ");
}
System.out.println();
}
}
}


it outputs :



run:
| X | X | X | X | X |
| X | X | X | X | X |
| X | X | X | X | X |
| X | X | X | X | X |
| X | X | X | X | X |
BUILD SUCCESSFUL (total time: 0 seconds)


So, the problem is that, i wanted to use this code using a class called :




Grille.jave




and so i tried this code below for the Class "Grille"



package tp_poo_v1_build1;
public class Grille {
int nbrL,nbrC;
int carte= new int[nbrL][nbrC];
public Grille(int pNbrL,int pNbrC){ //constructor
nbrL=pNbrL;
nbrC=pNbrC;
/*for(int i:carte)// enhanced for loop ( for each )
for(int j:i)
i[j]='X';*/

for(int i=0;i<carte.length;i++){ // syntax not optmised !
for(int j=0;j<carte[i].length;j++){
carte[i][j]='X';//Char in int 'X' == 88
}
}
}
boolean estLibre(int x,int y) {
return (carte[x][y] == 'X');
}
public void liberer(int x, int y){
carte[x][y]='X';
}
public void occupe(int x,int y,char nom){
//...!
}
public void afficher(){//nbrL=carte.length and nbrC=carte[i].length ?
System.out.println("THE Grill: ");
for (int i = 0; i < carte.length; i++) {
System.out.print(" | ");
for (int j = 0; j < carte[i].length; j++) {
System.out.print(carte[i][j] +" | ");
}
System.out.println();
}
}
}


and for the class that has the main method called :




UI.java




it's code is bellow :



package tp_poo_v1_build1;
public class UI {
public static void main(String args) {
Grille g=new Grille(5,5);
g.afficher();
}
}


it outputs :



run:
THE Grill:
BUILD SUCCESSFUL (total time: 0 seconds)


which is not what i want so if somebody knows what wrong please help.



why is it that when i try to fill the array and print it print using the main method directly, it works fine but when i fill and declare the array using the constructor "Grille" and the method "afficher" to print it, by creating the object "G" of the class "Grille" and then calling the method "afficher" in the main method, it does print the message "The Grill" but not the array like in the previous code ?
i just can't find out why so please help !!



and thank you in advance. (sorry if it's a stupid question im new to java and programming in general 😋 )
i'm new to this site so if my post is wrong please forgive me and help me correct it .







java multidimensional-array 2d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 20:56







B15

















asked Nov 25 '18 at 20:37









B15B15

123




123













  • Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

    – Joe C
    Nov 25 '18 at 20:38











  • ok thanks ill try

    – B15
    Nov 25 '18 at 20:41



















  • Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

    – Joe C
    Nov 25 '18 at 20:38











  • ok thanks ill try

    – B15
    Nov 25 '18 at 20:41

















Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

– Joe C
Nov 25 '18 at 20:38





Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

– Joe C
Nov 25 '18 at 20:38













ok thanks ill try

– B15
Nov 25 '18 at 20:41





ok thanks ill try

– B15
Nov 25 '18 at 20:41












1 Answer
1






active

oldest

votes


















0














Initialize your carte array in the constructor!



Before the constructor:



 int  carte;


In the constructor:



 public Grille(int pNbrL,int pNbrC){      //constructor 
nbrL=pNbrL;
nbrC=pNbrC;

carte = new int[nbrL][nbrC];

//rest of the constructor
}





share|improve this answer
























  • Damn that was stupid of me thanks a bunch

    – B15
    Nov 25 '18 at 20:59











  • No problem! It did not work becouse when you initialized it before both nbrL and nbrC where 0 (before the contructor) so you created an empty array basically.

    – Gtomika
    Nov 25 '18 at 21:08











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471719%2fthere-is-something-wrong-with-this-class-about-a-2d-array-i-made-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Initialize your carte array in the constructor!



Before the constructor:



 int  carte;


In the constructor:



 public Grille(int pNbrL,int pNbrC){      //constructor 
nbrL=pNbrL;
nbrC=pNbrC;

carte = new int[nbrL][nbrC];

//rest of the constructor
}





share|improve this answer
























  • Damn that was stupid of me thanks a bunch

    – B15
    Nov 25 '18 at 20:59











  • No problem! It did not work becouse when you initialized it before both nbrL and nbrC where 0 (before the contructor) so you created an empty array basically.

    – Gtomika
    Nov 25 '18 at 21:08
















0














Initialize your carte array in the constructor!



Before the constructor:



 int  carte;


In the constructor:



 public Grille(int pNbrL,int pNbrC){      //constructor 
nbrL=pNbrL;
nbrC=pNbrC;

carte = new int[nbrL][nbrC];

//rest of the constructor
}





share|improve this answer
























  • Damn that was stupid of me thanks a bunch

    – B15
    Nov 25 '18 at 20:59











  • No problem! It did not work becouse when you initialized it before both nbrL and nbrC where 0 (before the contructor) so you created an empty array basically.

    – Gtomika
    Nov 25 '18 at 21:08














0












0








0







Initialize your carte array in the constructor!



Before the constructor:



 int  carte;


In the constructor:



 public Grille(int pNbrL,int pNbrC){      //constructor 
nbrL=pNbrL;
nbrC=pNbrC;

carte = new int[nbrL][nbrC];

//rest of the constructor
}





share|improve this answer













Initialize your carte array in the constructor!



Before the constructor:



 int  carte;


In the constructor:



 public Grille(int pNbrL,int pNbrC){      //constructor 
nbrL=pNbrL;
nbrC=pNbrC;

carte = new int[nbrL][nbrC];

//rest of the constructor
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 20:48









GtomikaGtomika

381212




381212













  • Damn that was stupid of me thanks a bunch

    – B15
    Nov 25 '18 at 20:59











  • No problem! It did not work becouse when you initialized it before both nbrL and nbrC where 0 (before the contructor) so you created an empty array basically.

    – Gtomika
    Nov 25 '18 at 21:08



















  • Damn that was stupid of me thanks a bunch

    – B15
    Nov 25 '18 at 20:59











  • No problem! It did not work becouse when you initialized it before both nbrL and nbrC where 0 (before the contructor) so you created an empty array basically.

    – Gtomika
    Nov 25 '18 at 21:08

















Damn that was stupid of me thanks a bunch

– B15
Nov 25 '18 at 20:59





Damn that was stupid of me thanks a bunch

– B15
Nov 25 '18 at 20:59













No problem! It did not work becouse when you initialized it before both nbrL and nbrC where 0 (before the contructor) so you created an empty array basically.

– Gtomika
Nov 25 '18 at 21:08





No problem! It did not work becouse when you initialized it before both nbrL and nbrC where 0 (before the contructor) so you created an empty array basically.

– Gtomika
Nov 25 '18 at 21:08




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471719%2fthere-is-something-wrong-with-this-class-about-a-2d-array-i-made-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin