Java - A 2D Array Multiplication Table with user input has too many rows and columns





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am not understanding a particular part of the following assignment and why my loops with a 2D matrix of this assignment is not generating the proper output.



I am getting too many rows and columns in the output. I also need help with the formatting of the output to make it readable.



The assignment is to do the following:



public void getMultiplicationTable()


This method will use a while loop to ask the user to enter two integer numbers between 1 and 10. The while loop will stop if the numbers provided by the user were in the specified range; otherwise, an error message will be printed and the user will be asked to enter another two numbers.



The first number given by the user will be incremented by one then used as the rows number and the second one will be incremented by 1 and used as the column number of a two-dimensional array. The method will calculate the multiplication table and store it in the array and then display it.



For example, if the user entered 5 & 7 your code should create an int array with 6 rows and 8 columns and then creates and load the array with the multiplication table. The printed result should be like so:



0  0  0  0  0  0  0  0
0 1 2 3 4 5 6 7
0 2 4 6 8 10 12 14
0 3 6 9 12 15 18 21
0 4 8 12 16 20 24 28
0 5 10 15 20 25 30 35


Where I am getting stuck is the part where it says:



The first number given by the user will be incremented by one then used as the rows number and the second one will be incremented by 1 and used as the column number of a two-dimensional array. The method will calculate the multiplication table and store it in the array and then display it.



I cannot seem to get the numbers that I have inputted to be used as the row and column length, and the print out will print multiples of up to ten (although I guess technically nine, since I have not used + 1). Any help that can be provided would be greatly appreciated. Here is what I have so far:



public void getMultiplicationTable(){

Scanner numsEnter = new Scanner(System.in);

int table = new int[10][10];

int numRow = 0;
int numCol = 0;

System.out.println("Please enter in two numbers: ");

while(true){
numRow = numsEnter.nextInt();
numCol = numsEnter.nextInt();
if((numRow >= 1 && numRow <= 10) && (numCol >= 1 && numCol <= 10)){
break;
}else{
System.out.println("These numbers are invalid, please enter two numbers between 0 & 10");
}
}

numRow++;
numCol++;

for(int row=0; row < table.length; row++){
for(int column = 0; column < table[row].length; column++){
System.out.print(table [row] [column] = (row) * (column) );
}
System.out.println();
}
}


Here is the output that I am getting with this, using 5 & 7 as the numbers inputted:



Please enter in two numbers: 
5
7

0000000000
0123456789
024681012141618
0369121518212427
04812162024283236
051015202530354045
061218243036424854
071421283542495663
081624324048566472
091827364554637281


Thank you!










share|improve this question

























  • The row loop should go from 1 to numRow and the column loop should go from 1 to numCol. The table size should be one more than the expected largest size so it should be new int[11][11] and not new int[10][10]. The values entered can be 1 to 10 but then the value is incremented so the max range would be 11 and not 10.

    – Richard Chambers
    Nov 22 '18 at 4:47




















0















I am not understanding a particular part of the following assignment and why my loops with a 2D matrix of this assignment is not generating the proper output.



I am getting too many rows and columns in the output. I also need help with the formatting of the output to make it readable.



The assignment is to do the following:



public void getMultiplicationTable()


This method will use a while loop to ask the user to enter two integer numbers between 1 and 10. The while loop will stop if the numbers provided by the user were in the specified range; otherwise, an error message will be printed and the user will be asked to enter another two numbers.



The first number given by the user will be incremented by one then used as the rows number and the second one will be incremented by 1 and used as the column number of a two-dimensional array. The method will calculate the multiplication table and store it in the array and then display it.



For example, if the user entered 5 & 7 your code should create an int array with 6 rows and 8 columns and then creates and load the array with the multiplication table. The printed result should be like so:



0  0  0  0  0  0  0  0
0 1 2 3 4 5 6 7
0 2 4 6 8 10 12 14
0 3 6 9 12 15 18 21
0 4 8 12 16 20 24 28
0 5 10 15 20 25 30 35


Where I am getting stuck is the part where it says:



The first number given by the user will be incremented by one then used as the rows number and the second one will be incremented by 1 and used as the column number of a two-dimensional array. The method will calculate the multiplication table and store it in the array and then display it.



I cannot seem to get the numbers that I have inputted to be used as the row and column length, and the print out will print multiples of up to ten (although I guess technically nine, since I have not used + 1). Any help that can be provided would be greatly appreciated. Here is what I have so far:



public void getMultiplicationTable(){

Scanner numsEnter = new Scanner(System.in);

int table = new int[10][10];

int numRow = 0;
int numCol = 0;

System.out.println("Please enter in two numbers: ");

while(true){
numRow = numsEnter.nextInt();
numCol = numsEnter.nextInt();
if((numRow >= 1 && numRow <= 10) && (numCol >= 1 && numCol <= 10)){
break;
}else{
System.out.println("These numbers are invalid, please enter two numbers between 0 & 10");
}
}

numRow++;
numCol++;

for(int row=0; row < table.length; row++){
for(int column = 0; column < table[row].length; column++){
System.out.print(table [row] [column] = (row) * (column) );
}
System.out.println();
}
}


Here is the output that I am getting with this, using 5 & 7 as the numbers inputted:



Please enter in two numbers: 
5
7

0000000000
0123456789
024681012141618
0369121518212427
04812162024283236
051015202530354045
061218243036424854
071421283542495663
081624324048566472
091827364554637281


Thank you!










share|improve this question

























  • The row loop should go from 1 to numRow and the column loop should go from 1 to numCol. The table size should be one more than the expected largest size so it should be new int[11][11] and not new int[10][10]. The values entered can be 1 to 10 but then the value is incremented so the max range would be 11 and not 10.

    – Richard Chambers
    Nov 22 '18 at 4:47
















0












0








0








I am not understanding a particular part of the following assignment and why my loops with a 2D matrix of this assignment is not generating the proper output.



I am getting too many rows and columns in the output. I also need help with the formatting of the output to make it readable.



The assignment is to do the following:



public void getMultiplicationTable()


This method will use a while loop to ask the user to enter two integer numbers between 1 and 10. The while loop will stop if the numbers provided by the user were in the specified range; otherwise, an error message will be printed and the user will be asked to enter another two numbers.



The first number given by the user will be incremented by one then used as the rows number and the second one will be incremented by 1 and used as the column number of a two-dimensional array. The method will calculate the multiplication table and store it in the array and then display it.



For example, if the user entered 5 & 7 your code should create an int array with 6 rows and 8 columns and then creates and load the array with the multiplication table. The printed result should be like so:



0  0  0  0  0  0  0  0
0 1 2 3 4 5 6 7
0 2 4 6 8 10 12 14
0 3 6 9 12 15 18 21
0 4 8 12 16 20 24 28
0 5 10 15 20 25 30 35


Where I am getting stuck is the part where it says:



The first number given by the user will be incremented by one then used as the rows number and the second one will be incremented by 1 and used as the column number of a two-dimensional array. The method will calculate the multiplication table and store it in the array and then display it.



I cannot seem to get the numbers that I have inputted to be used as the row and column length, and the print out will print multiples of up to ten (although I guess technically nine, since I have not used + 1). Any help that can be provided would be greatly appreciated. Here is what I have so far:



public void getMultiplicationTable(){

Scanner numsEnter = new Scanner(System.in);

int table = new int[10][10];

int numRow = 0;
int numCol = 0;

System.out.println("Please enter in two numbers: ");

while(true){
numRow = numsEnter.nextInt();
numCol = numsEnter.nextInt();
if((numRow >= 1 && numRow <= 10) && (numCol >= 1 && numCol <= 10)){
break;
}else{
System.out.println("These numbers are invalid, please enter two numbers between 0 & 10");
}
}

numRow++;
numCol++;

for(int row=0; row < table.length; row++){
for(int column = 0; column < table[row].length; column++){
System.out.print(table [row] [column] = (row) * (column) );
}
System.out.println();
}
}


Here is the output that I am getting with this, using 5 & 7 as the numbers inputted:



Please enter in two numbers: 
5
7

0000000000
0123456789
024681012141618
0369121518212427
04812162024283236
051015202530354045
061218243036424854
071421283542495663
081624324048566472
091827364554637281


Thank you!










share|improve this question
















I am not understanding a particular part of the following assignment and why my loops with a 2D matrix of this assignment is not generating the proper output.



I am getting too many rows and columns in the output. I also need help with the formatting of the output to make it readable.



The assignment is to do the following:



public void getMultiplicationTable()


This method will use a while loop to ask the user to enter two integer numbers between 1 and 10. The while loop will stop if the numbers provided by the user were in the specified range; otherwise, an error message will be printed and the user will be asked to enter another two numbers.



The first number given by the user will be incremented by one then used as the rows number and the second one will be incremented by 1 and used as the column number of a two-dimensional array. The method will calculate the multiplication table and store it in the array and then display it.



For example, if the user entered 5 & 7 your code should create an int array with 6 rows and 8 columns and then creates and load the array with the multiplication table. The printed result should be like so:



0  0  0  0  0  0  0  0
0 1 2 3 4 5 6 7
0 2 4 6 8 10 12 14
0 3 6 9 12 15 18 21
0 4 8 12 16 20 24 28
0 5 10 15 20 25 30 35


Where I am getting stuck is the part where it says:



The first number given by the user will be incremented by one then used as the rows number and the second one will be incremented by 1 and used as the column number of a two-dimensional array. The method will calculate the multiplication table and store it in the array and then display it.



I cannot seem to get the numbers that I have inputted to be used as the row and column length, and the print out will print multiples of up to ten (although I guess technically nine, since I have not used + 1). Any help that can be provided would be greatly appreciated. Here is what I have so far:



public void getMultiplicationTable(){

Scanner numsEnter = new Scanner(System.in);

int table = new int[10][10];

int numRow = 0;
int numCol = 0;

System.out.println("Please enter in two numbers: ");

while(true){
numRow = numsEnter.nextInt();
numCol = numsEnter.nextInt();
if((numRow >= 1 && numRow <= 10) && (numCol >= 1 && numCol <= 10)){
break;
}else{
System.out.println("These numbers are invalid, please enter two numbers between 0 & 10");
}
}

numRow++;
numCol++;

for(int row=0; row < table.length; row++){
for(int column = 0; column < table[row].length; column++){
System.out.print(table [row] [column] = (row) * (column) );
}
System.out.println();
}
}


Here is the output that I am getting with this, using 5 & 7 as the numbers inputted:



Please enter in two numbers: 
5
7

0000000000
0123456789
024681012141618
0369121518212427
04812162024283236
051015202530354045
061218243036424854
071421283542495663
081624324048566472
091827364554637281


Thank you!







java arrays multidimensional-array 2d user-input






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 5:27









Richard Chambers

10.2k24268




10.2k24268










asked Nov 22 '18 at 4:36









Andrew ShearerAndrew Shearer

204




204













  • The row loop should go from 1 to numRow and the column loop should go from 1 to numCol. The table size should be one more than the expected largest size so it should be new int[11][11] and not new int[10][10]. The values entered can be 1 to 10 but then the value is incremented so the max range would be 11 and not 10.

    – Richard Chambers
    Nov 22 '18 at 4:47





















  • The row loop should go from 1 to numRow and the column loop should go from 1 to numCol. The table size should be one more than the expected largest size so it should be new int[11][11] and not new int[10][10]. The values entered can be 1 to 10 but then the value is incremented so the max range would be 11 and not 10.

    – Richard Chambers
    Nov 22 '18 at 4:47



















The row loop should go from 1 to numRow and the column loop should go from 1 to numCol. The table size should be one more than the expected largest size so it should be new int[11][11] and not new int[10][10]. The values entered can be 1 to 10 but then the value is incremented so the max range would be 11 and not 10.

– Richard Chambers
Nov 22 '18 at 4:47







The row loop should go from 1 to numRow and the column loop should go from 1 to numCol. The table size should be one more than the expected largest size so it should be new int[11][11] and not new int[10][10]. The values entered can be 1 to 10 but then the value is incremented so the max range would be 11 and not 10.

– Richard Chambers
Nov 22 '18 at 4:47














2 Answers
2






active

oldest

votes


















0














You can set your array size to match your user inputs if you just ask for them first, and define your 2D array after that. You're currently hard coding your 2D array to be a 10x10, but your logic is correct other than that.



Here is what it should look like.



    public void getMultiplicationTable(){

Scanner numsEnter = new Scanner(System.in);

System.out.println("Please enter in two numbers: ");

int numRow = numsEnter.nextInt();
int numCol = numsEnter.nextInt();

int table = new int[numRow+1][numCol+1];

System.out.println("Please enter in two numbers: ");

while(true){
if((numRow >= 1 && numRow <= 10) && (numCol >= 1 && numCol <= 10)){
break;
}else{
System.out.println("These numbers are invalid, please enter two numbers between 0 & 10");
numRow = numsEnter.nextInt();
numCol = numsEnter.nextInt();
}
}

numRow++;
numCol++;

for(int row=0; row < table.length; row++){
for(int column = 0; column < table[row].length; column++){
System.out.print(table [row] [column] = (row) * (column) );
System.out.print(" ");
}
System.out.println();
}

}


The code above will output the following for an input of 5 7:



0 0 0 0 0 0 0 0 
0 1 2 3 4 5 6 7
0 2 4 6 8 10 12 14
0 3 6 9 12 15 18 21
0 4 8 12 16 20 24 28
0 5 10 15 20 25 30 35





share|improve this answer


























  • Thank you so much Ryan!! This worked perfectly. Thank you very much for your help! :)

    – Andrew Shearer
    Nov 22 '18 at 5:23



















0














It’s because you declare the table to be a 10x10 array to start then use the length of the array in your condition in the for loops. You should create the table after you know the size of the table (or change your conditions).






share|improve this answer
























    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%2f53423966%2fjava-a-2d-array-multiplication-table-with-user-input-has-too-many-rows-and-col%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can set your array size to match your user inputs if you just ask for them first, and define your 2D array after that. You're currently hard coding your 2D array to be a 10x10, but your logic is correct other than that.



    Here is what it should look like.



        public void getMultiplicationTable(){

    Scanner numsEnter = new Scanner(System.in);

    System.out.println("Please enter in two numbers: ");

    int numRow = numsEnter.nextInt();
    int numCol = numsEnter.nextInt();

    int table = new int[numRow+1][numCol+1];

    System.out.println("Please enter in two numbers: ");

    while(true){
    if((numRow >= 1 && numRow <= 10) && (numCol >= 1 && numCol <= 10)){
    break;
    }else{
    System.out.println("These numbers are invalid, please enter two numbers between 0 & 10");
    numRow = numsEnter.nextInt();
    numCol = numsEnter.nextInt();
    }
    }

    numRow++;
    numCol++;

    for(int row=0; row < table.length; row++){
    for(int column = 0; column < table[row].length; column++){
    System.out.print(table [row] [column] = (row) * (column) );
    System.out.print(" ");
    }
    System.out.println();
    }

    }


    The code above will output the following for an input of 5 7:



    0 0 0 0 0 0 0 0 
    0 1 2 3 4 5 6 7
    0 2 4 6 8 10 12 14
    0 3 6 9 12 15 18 21
    0 4 8 12 16 20 24 28
    0 5 10 15 20 25 30 35





    share|improve this answer


























    • Thank you so much Ryan!! This worked perfectly. Thank you very much for your help! :)

      – Andrew Shearer
      Nov 22 '18 at 5:23
















    0














    You can set your array size to match your user inputs if you just ask for them first, and define your 2D array after that. You're currently hard coding your 2D array to be a 10x10, but your logic is correct other than that.



    Here is what it should look like.



        public void getMultiplicationTable(){

    Scanner numsEnter = new Scanner(System.in);

    System.out.println("Please enter in two numbers: ");

    int numRow = numsEnter.nextInt();
    int numCol = numsEnter.nextInt();

    int table = new int[numRow+1][numCol+1];

    System.out.println("Please enter in two numbers: ");

    while(true){
    if((numRow >= 1 && numRow <= 10) && (numCol >= 1 && numCol <= 10)){
    break;
    }else{
    System.out.println("These numbers are invalid, please enter two numbers between 0 & 10");
    numRow = numsEnter.nextInt();
    numCol = numsEnter.nextInt();
    }
    }

    numRow++;
    numCol++;

    for(int row=0; row < table.length; row++){
    for(int column = 0; column < table[row].length; column++){
    System.out.print(table [row] [column] = (row) * (column) );
    System.out.print(" ");
    }
    System.out.println();
    }

    }


    The code above will output the following for an input of 5 7:



    0 0 0 0 0 0 0 0 
    0 1 2 3 4 5 6 7
    0 2 4 6 8 10 12 14
    0 3 6 9 12 15 18 21
    0 4 8 12 16 20 24 28
    0 5 10 15 20 25 30 35





    share|improve this answer


























    • Thank you so much Ryan!! This worked perfectly. Thank you very much for your help! :)

      – Andrew Shearer
      Nov 22 '18 at 5:23














    0












    0








    0







    You can set your array size to match your user inputs if you just ask for them first, and define your 2D array after that. You're currently hard coding your 2D array to be a 10x10, but your logic is correct other than that.



    Here is what it should look like.



        public void getMultiplicationTable(){

    Scanner numsEnter = new Scanner(System.in);

    System.out.println("Please enter in two numbers: ");

    int numRow = numsEnter.nextInt();
    int numCol = numsEnter.nextInt();

    int table = new int[numRow+1][numCol+1];

    System.out.println("Please enter in two numbers: ");

    while(true){
    if((numRow >= 1 && numRow <= 10) && (numCol >= 1 && numCol <= 10)){
    break;
    }else{
    System.out.println("These numbers are invalid, please enter two numbers between 0 & 10");
    numRow = numsEnter.nextInt();
    numCol = numsEnter.nextInt();
    }
    }

    numRow++;
    numCol++;

    for(int row=0; row < table.length; row++){
    for(int column = 0; column < table[row].length; column++){
    System.out.print(table [row] [column] = (row) * (column) );
    System.out.print(" ");
    }
    System.out.println();
    }

    }


    The code above will output the following for an input of 5 7:



    0 0 0 0 0 0 0 0 
    0 1 2 3 4 5 6 7
    0 2 4 6 8 10 12 14
    0 3 6 9 12 15 18 21
    0 4 8 12 16 20 24 28
    0 5 10 15 20 25 30 35





    share|improve this answer















    You can set your array size to match your user inputs if you just ask for them first, and define your 2D array after that. You're currently hard coding your 2D array to be a 10x10, but your logic is correct other than that.



    Here is what it should look like.



        public void getMultiplicationTable(){

    Scanner numsEnter = new Scanner(System.in);

    System.out.println("Please enter in two numbers: ");

    int numRow = numsEnter.nextInt();
    int numCol = numsEnter.nextInt();

    int table = new int[numRow+1][numCol+1];

    System.out.println("Please enter in two numbers: ");

    while(true){
    if((numRow >= 1 && numRow <= 10) && (numCol >= 1 && numCol <= 10)){
    break;
    }else{
    System.out.println("These numbers are invalid, please enter two numbers between 0 & 10");
    numRow = numsEnter.nextInt();
    numCol = numsEnter.nextInt();
    }
    }

    numRow++;
    numCol++;

    for(int row=0; row < table.length; row++){
    for(int column = 0; column < table[row].length; column++){
    System.out.print(table [row] [column] = (row) * (column) );
    System.out.print(" ");
    }
    System.out.println();
    }

    }


    The code above will output the following for an input of 5 7:



    0 0 0 0 0 0 0 0 
    0 1 2 3 4 5 6 7
    0 2 4 6 8 10 12 14
    0 3 6 9 12 15 18 21
    0 4 8 12 16 20 24 28
    0 5 10 15 20 25 30 35






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 5:11

























    answered Nov 22 '18 at 4:49









    Ryan KozakRyan Kozak

    70651319




    70651319













    • Thank you so much Ryan!! This worked perfectly. Thank you very much for your help! :)

      – Andrew Shearer
      Nov 22 '18 at 5:23



















    • Thank you so much Ryan!! This worked perfectly. Thank you very much for your help! :)

      – Andrew Shearer
      Nov 22 '18 at 5:23

















    Thank you so much Ryan!! This worked perfectly. Thank you very much for your help! :)

    – Andrew Shearer
    Nov 22 '18 at 5:23





    Thank you so much Ryan!! This worked perfectly. Thank you very much for your help! :)

    – Andrew Shearer
    Nov 22 '18 at 5:23













    0














    It’s because you declare the table to be a 10x10 array to start then use the length of the array in your condition in the for loops. You should create the table after you know the size of the table (or change your conditions).






    share|improve this answer




























      0














      It’s because you declare the table to be a 10x10 array to start then use the length of the array in your condition in the for loops. You should create the table after you know the size of the table (or change your conditions).






      share|improve this answer


























        0












        0








        0







        It’s because you declare the table to be a 10x10 array to start then use the length of the array in your condition in the for loops. You should create the table after you know the size of the table (or change your conditions).






        share|improve this answer













        It’s because you declare the table to be a 10x10 array to start then use the length of the array in your condition in the for loops. You should create the table after you know the size of the table (or change your conditions).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 4:44









        tplusktplusk

        115




        115






























            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%2f53423966%2fjava-a-2d-array-multiplication-table-with-user-input-has-too-many-rows-and-col%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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)