Why is this line printing twice?










0















So I help tutor an Algebra 2 class at my local high school, and the class is currently looking over matrices. Though not there, they will eventually get to multiplication of matrices. After taking Computer Science last year and learning Java, the teacher I help thought I should try to write a program to multiple matrices.



At the moment, I have up to defining the numbers for the first array that holds the information for the first matrix. However, I have a small issue. As represented by this picture:
enter image description here



the line asking for the index integers is being repeated after already recording the integers. I assume this is due to my layered for loops, but I can't be for certain. Usually new eyes see problems clearer. Any who could help me would be appreciated.



Code:



package matrixmultiplication;
import java.util.*;

public class MatrixMultiplication

public static void main(String args)
System.out.println("What is the size of the first matrix?");
int matrix1Rows = matrixRows();
int matrix1Columns = matrixColumns();
int matrix1 = new int[matrix1Rows * matrix1Columns];
doubleSpace();
System.out.println("What is the size of the second matrix?");
int matrix2Rows = matrixRows();
int matrix2Columns = matrixColumns();
int matrix2 = new int[matrix2Rows * matrix2Columns];
doubleSpace();
if (matrix1Columns != matrix2Rows)
System.out.println("These cannot be multiplied!");
System.exit(0);
else
matrix1Numbers(matrix1Rows, matrix1Columns);



public static int matrixRows()
System.out.print("Rows:");
Scanner rowSc = new Scanner(System.in);
int rows = rowSc.nextInt();
return rows;


public static int matrixColumns()
System.out.print("Columns:");
Scanner colSc = new Scanner(System.in);
int cols = colSc.nextInt();
return cols;


public static int matrix1Numbers(int rows, int cols)
int numb = new int[rows * cols];
for (int j = 0; j <= numb.length; j += rows)
for (int i = 1; i <= cols; i++)
for (int k = 1; k <= rows; k++)
System.out.println("What is the value for index (" + k + "," + i + ")?");
Scanner inp = new Scanner(System.in);
if (j + k <= numb.length)
numb[j + k - 1] = inp.nextInt();




for (int i = 0; i < numb.length; i++)
System.out.println(numb[i]);

return numb;


public static void doubleSpace()
System.out.println();
System.out.println();





I use NetBeans 8.2 and the latest working version of Java for NetBeans.










share|improve this question






















  • Did you realize you can have an int in Java? It's a lot more convenient to write numb[j][k] rather than numb[j * cols + k] for the item at row j, column k.

    – Kevin Anderson
    Nov 14 '18 at 2:54











  • In any case, you've got an extra level of loop nesting that's unneeded. The first level steps through the rows, the next steps through the columns, and the third steps through the rows again.

    – Kevin Anderson
    Nov 14 '18 at 2:57















0















So I help tutor an Algebra 2 class at my local high school, and the class is currently looking over matrices. Though not there, they will eventually get to multiplication of matrices. After taking Computer Science last year and learning Java, the teacher I help thought I should try to write a program to multiple matrices.



At the moment, I have up to defining the numbers for the first array that holds the information for the first matrix. However, I have a small issue. As represented by this picture:
enter image description here



the line asking for the index integers is being repeated after already recording the integers. I assume this is due to my layered for loops, but I can't be for certain. Usually new eyes see problems clearer. Any who could help me would be appreciated.



Code:



package matrixmultiplication;
import java.util.*;

public class MatrixMultiplication

public static void main(String args)
System.out.println("What is the size of the first matrix?");
int matrix1Rows = matrixRows();
int matrix1Columns = matrixColumns();
int matrix1 = new int[matrix1Rows * matrix1Columns];
doubleSpace();
System.out.println("What is the size of the second matrix?");
int matrix2Rows = matrixRows();
int matrix2Columns = matrixColumns();
int matrix2 = new int[matrix2Rows * matrix2Columns];
doubleSpace();
if (matrix1Columns != matrix2Rows)
System.out.println("These cannot be multiplied!");
System.exit(0);
else
matrix1Numbers(matrix1Rows, matrix1Columns);



public static int matrixRows()
System.out.print("Rows:");
Scanner rowSc = new Scanner(System.in);
int rows = rowSc.nextInt();
return rows;


public static int matrixColumns()
System.out.print("Columns:");
Scanner colSc = new Scanner(System.in);
int cols = colSc.nextInt();
return cols;


public static int matrix1Numbers(int rows, int cols)
int numb = new int[rows * cols];
for (int j = 0; j <= numb.length; j += rows)
for (int i = 1; i <= cols; i++)
for (int k = 1; k <= rows; k++)
System.out.println("What is the value for index (" + k + "," + i + ")?");
Scanner inp = new Scanner(System.in);
if (j + k <= numb.length)
numb[j + k - 1] = inp.nextInt();




for (int i = 0; i < numb.length; i++)
System.out.println(numb[i]);

return numb;


public static void doubleSpace()
System.out.println();
System.out.println();





I use NetBeans 8.2 and the latest working version of Java for NetBeans.










share|improve this question






















  • Did you realize you can have an int in Java? It's a lot more convenient to write numb[j][k] rather than numb[j * cols + k] for the item at row j, column k.

    – Kevin Anderson
    Nov 14 '18 at 2:54











  • In any case, you've got an extra level of loop nesting that's unneeded. The first level steps through the rows, the next steps through the columns, and the third steps through the rows again.

    – Kevin Anderson
    Nov 14 '18 at 2:57













0












0








0








So I help tutor an Algebra 2 class at my local high school, and the class is currently looking over matrices. Though not there, they will eventually get to multiplication of matrices. After taking Computer Science last year and learning Java, the teacher I help thought I should try to write a program to multiple matrices.



At the moment, I have up to defining the numbers for the first array that holds the information for the first matrix. However, I have a small issue. As represented by this picture:
enter image description here



the line asking for the index integers is being repeated after already recording the integers. I assume this is due to my layered for loops, but I can't be for certain. Usually new eyes see problems clearer. Any who could help me would be appreciated.



Code:



package matrixmultiplication;
import java.util.*;

public class MatrixMultiplication

public static void main(String args)
System.out.println("What is the size of the first matrix?");
int matrix1Rows = matrixRows();
int matrix1Columns = matrixColumns();
int matrix1 = new int[matrix1Rows * matrix1Columns];
doubleSpace();
System.out.println("What is the size of the second matrix?");
int matrix2Rows = matrixRows();
int matrix2Columns = matrixColumns();
int matrix2 = new int[matrix2Rows * matrix2Columns];
doubleSpace();
if (matrix1Columns != matrix2Rows)
System.out.println("These cannot be multiplied!");
System.exit(0);
else
matrix1Numbers(matrix1Rows, matrix1Columns);



public static int matrixRows()
System.out.print("Rows:");
Scanner rowSc = new Scanner(System.in);
int rows = rowSc.nextInt();
return rows;


public static int matrixColumns()
System.out.print("Columns:");
Scanner colSc = new Scanner(System.in);
int cols = colSc.nextInt();
return cols;


public static int matrix1Numbers(int rows, int cols)
int numb = new int[rows * cols];
for (int j = 0; j <= numb.length; j += rows)
for (int i = 1; i <= cols; i++)
for (int k = 1; k <= rows; k++)
System.out.println("What is the value for index (" + k + "," + i + ")?");
Scanner inp = new Scanner(System.in);
if (j + k <= numb.length)
numb[j + k - 1] = inp.nextInt();




for (int i = 0; i < numb.length; i++)
System.out.println(numb[i]);

return numb;


public static void doubleSpace()
System.out.println();
System.out.println();





I use NetBeans 8.2 and the latest working version of Java for NetBeans.










share|improve this question














So I help tutor an Algebra 2 class at my local high school, and the class is currently looking over matrices. Though not there, they will eventually get to multiplication of matrices. After taking Computer Science last year and learning Java, the teacher I help thought I should try to write a program to multiple matrices.



At the moment, I have up to defining the numbers for the first array that holds the information for the first matrix. However, I have a small issue. As represented by this picture:
enter image description here



the line asking for the index integers is being repeated after already recording the integers. I assume this is due to my layered for loops, but I can't be for certain. Usually new eyes see problems clearer. Any who could help me would be appreciated.



Code:



package matrixmultiplication;
import java.util.*;

public class MatrixMultiplication

public static void main(String args)
System.out.println("What is the size of the first matrix?");
int matrix1Rows = matrixRows();
int matrix1Columns = matrixColumns();
int matrix1 = new int[matrix1Rows * matrix1Columns];
doubleSpace();
System.out.println("What is the size of the second matrix?");
int matrix2Rows = matrixRows();
int matrix2Columns = matrixColumns();
int matrix2 = new int[matrix2Rows * matrix2Columns];
doubleSpace();
if (matrix1Columns != matrix2Rows)
System.out.println("These cannot be multiplied!");
System.exit(0);
else
matrix1Numbers(matrix1Rows, matrix1Columns);



public static int matrixRows()
System.out.print("Rows:");
Scanner rowSc = new Scanner(System.in);
int rows = rowSc.nextInt();
return rows;


public static int matrixColumns()
System.out.print("Columns:");
Scanner colSc = new Scanner(System.in);
int cols = colSc.nextInt();
return cols;


public static int matrix1Numbers(int rows, int cols)
int numb = new int[rows * cols];
for (int j = 0; j <= numb.length; j += rows)
for (int i = 1; i <= cols; i++)
for (int k = 1; k <= rows; k++)
System.out.println("What is the value for index (" + k + "," + i + ")?");
Scanner inp = new Scanner(System.in);
if (j + k <= numb.length)
numb[j + k - 1] = inp.nextInt();




for (int i = 0; i < numb.length; i++)
System.out.println(numb[i]);

return numb;


public static void doubleSpace()
System.out.println();
System.out.println();





I use NetBeans 8.2 and the latest working version of Java for NetBeans.







java arrays debugging matrix matrix-multiplication






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 2:43









CurlyCueCurlyCue

141




141












  • Did you realize you can have an int in Java? It's a lot more convenient to write numb[j][k] rather than numb[j * cols + k] for the item at row j, column k.

    – Kevin Anderson
    Nov 14 '18 at 2:54











  • In any case, you've got an extra level of loop nesting that's unneeded. The first level steps through the rows, the next steps through the columns, and the third steps through the rows again.

    – Kevin Anderson
    Nov 14 '18 at 2:57

















  • Did you realize you can have an int in Java? It's a lot more convenient to write numb[j][k] rather than numb[j * cols + k] for the item at row j, column k.

    – Kevin Anderson
    Nov 14 '18 at 2:54











  • In any case, you've got an extra level of loop nesting that's unneeded. The first level steps through the rows, the next steps through the columns, and the third steps through the rows again.

    – Kevin Anderson
    Nov 14 '18 at 2:57
















Did you realize you can have an int in Java? It's a lot more convenient to write numb[j][k] rather than numb[j * cols + k] for the item at row j, column k.

– Kevin Anderson
Nov 14 '18 at 2:54





Did you realize you can have an int in Java? It's a lot more convenient to write numb[j][k] rather than numb[j * cols + k] for the item at row j, column k.

– Kevin Anderson
Nov 14 '18 at 2:54













In any case, you've got an extra level of loop nesting that's unneeded. The first level steps through the rows, the next steps through the columns, and the third steps through the rows again.

– Kevin Anderson
Nov 14 '18 at 2:57





In any case, you've got an extra level of loop nesting that's unneeded. The first level steps through the rows, the next steps through the columns, and the third steps through the rows again.

– Kevin Anderson
Nov 14 '18 at 2:57












2 Answers
2






active

oldest

votes


















0















Why is this line printing twice?




Because in your loop here:



for (int k = 1; k <= rows; k++)
System.out.println("What is the value for index (" + k + "," + i + ")?");
Scanner inp = new Scanner(System.in);
if (j + k <= numb.length)
numb[j + k - 1] = inp.nextInt();




You only ask for input if j + k <= numb.length but you loop while k <= rows, and print every iteration. There must a value for which you are still looping but j + k > numb.length. To fix this, change so that your loop with both of the conditions:



for(int k = 1; k <= rows && j + k <= numb.length; k++)


And then remove the if part, and ask for input on every iteration.




Also you are creating a new Scanner object inside of your loop, which will create a new Scanner on every iteration. Move the deceleration outside of the loop.






share|improve this answer






























    0














    I'm not familiar with the matrixmultiplication package, so I may be rambling here.



    for (int j = 0; j <= numb.length; j += rows){


    I'm not entirely sure what the outer for loop your have is for, but this most outer for loop causes you to ask for the values of the indices cols times more than you want.
    I feel that you originally wanted to use this outer for loop to iterate through each row, and wasn't planning on having the second for loop iterating through cols perhaps?



    Also, Kevin Anderson mentions this above. You might avoid this problem altogether if you return a double int array as opposed to storing all values in the matrix in a single dimension. I personally feel it would make more sense.



    Just nitpicking, but I wouldn't make a new scanner every time you want to use one in a different method. You could just make a field at the top of your class, instantiate it once in your main method, and then pass it in as a parameter to all methods using the scanner.






    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%2f53292438%2fwhy-is-this-line-printing-twice%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















      Why is this line printing twice?




      Because in your loop here:



      for (int k = 1; k <= rows; k++)
      System.out.println("What is the value for index (" + k + "," + i + ")?");
      Scanner inp = new Scanner(System.in);
      if (j + k <= numb.length)
      numb[j + k - 1] = inp.nextInt();




      You only ask for input if j + k <= numb.length but you loop while k <= rows, and print every iteration. There must a value for which you are still looping but j + k > numb.length. To fix this, change so that your loop with both of the conditions:



      for(int k = 1; k <= rows && j + k <= numb.length; k++)


      And then remove the if part, and ask for input on every iteration.




      Also you are creating a new Scanner object inside of your loop, which will create a new Scanner on every iteration. Move the deceleration outside of the loop.






      share|improve this answer



























        0















        Why is this line printing twice?




        Because in your loop here:



        for (int k = 1; k <= rows; k++)
        System.out.println("What is the value for index (" + k + "," + i + ")?");
        Scanner inp = new Scanner(System.in);
        if (j + k <= numb.length)
        numb[j + k - 1] = inp.nextInt();




        You only ask for input if j + k <= numb.length but you loop while k <= rows, and print every iteration. There must a value for which you are still looping but j + k > numb.length. To fix this, change so that your loop with both of the conditions:



        for(int k = 1; k <= rows && j + k <= numb.length; k++)


        And then remove the if part, and ask for input on every iteration.




        Also you are creating a new Scanner object inside of your loop, which will create a new Scanner on every iteration. Move the deceleration outside of the loop.






        share|improve this answer

























          0












          0








          0








          Why is this line printing twice?




          Because in your loop here:



          for (int k = 1; k <= rows; k++)
          System.out.println("What is the value for index (" + k + "," + i + ")?");
          Scanner inp = new Scanner(System.in);
          if (j + k <= numb.length)
          numb[j + k - 1] = inp.nextInt();




          You only ask for input if j + k <= numb.length but you loop while k <= rows, and print every iteration. There must a value for which you are still looping but j + k > numb.length. To fix this, change so that your loop with both of the conditions:



          for(int k = 1; k <= rows && j + k <= numb.length; k++)


          And then remove the if part, and ask for input on every iteration.




          Also you are creating a new Scanner object inside of your loop, which will create a new Scanner on every iteration. Move the deceleration outside of the loop.






          share|improve this answer














          Why is this line printing twice?




          Because in your loop here:



          for (int k = 1; k <= rows; k++)
          System.out.println("What is the value for index (" + k + "," + i + ")?");
          Scanner inp = new Scanner(System.in);
          if (j + k <= numb.length)
          numb[j + k - 1] = inp.nextInt();




          You only ask for input if j + k <= numb.length but you loop while k <= rows, and print every iteration. There must a value for which you are still looping but j + k > numb.length. To fix this, change so that your loop with both of the conditions:



          for(int k = 1; k <= rows && j + k <= numb.length; k++)


          And then remove the if part, and ask for input on every iteration.




          Also you are creating a new Scanner object inside of your loop, which will create a new Scanner on every iteration. Move the deceleration outside of the loop.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 2:48









          GBlodgettGBlodgett

          9,68941733




          9,68941733























              0














              I'm not familiar with the matrixmultiplication package, so I may be rambling here.



              for (int j = 0; j <= numb.length; j += rows){


              I'm not entirely sure what the outer for loop your have is for, but this most outer for loop causes you to ask for the values of the indices cols times more than you want.
              I feel that you originally wanted to use this outer for loop to iterate through each row, and wasn't planning on having the second for loop iterating through cols perhaps?



              Also, Kevin Anderson mentions this above. You might avoid this problem altogether if you return a double int array as opposed to storing all values in the matrix in a single dimension. I personally feel it would make more sense.



              Just nitpicking, but I wouldn't make a new scanner every time you want to use one in a different method. You could just make a field at the top of your class, instantiate it once in your main method, and then pass it in as a parameter to all methods using the scanner.






              share|improve this answer



























                0














                I'm not familiar with the matrixmultiplication package, so I may be rambling here.



                for (int j = 0; j <= numb.length; j += rows){


                I'm not entirely sure what the outer for loop your have is for, but this most outer for loop causes you to ask for the values of the indices cols times more than you want.
                I feel that you originally wanted to use this outer for loop to iterate through each row, and wasn't planning on having the second for loop iterating through cols perhaps?



                Also, Kevin Anderson mentions this above. You might avoid this problem altogether if you return a double int array as opposed to storing all values in the matrix in a single dimension. I personally feel it would make more sense.



                Just nitpicking, but I wouldn't make a new scanner every time you want to use one in a different method. You could just make a field at the top of your class, instantiate it once in your main method, and then pass it in as a parameter to all methods using the scanner.






                share|improve this answer

























                  0












                  0








                  0







                  I'm not familiar with the matrixmultiplication package, so I may be rambling here.



                  for (int j = 0; j <= numb.length; j += rows){


                  I'm not entirely sure what the outer for loop your have is for, but this most outer for loop causes you to ask for the values of the indices cols times more than you want.
                  I feel that you originally wanted to use this outer for loop to iterate through each row, and wasn't planning on having the second for loop iterating through cols perhaps?



                  Also, Kevin Anderson mentions this above. You might avoid this problem altogether if you return a double int array as opposed to storing all values in the matrix in a single dimension. I personally feel it would make more sense.



                  Just nitpicking, but I wouldn't make a new scanner every time you want to use one in a different method. You could just make a field at the top of your class, instantiate it once in your main method, and then pass it in as a parameter to all methods using the scanner.






                  share|improve this answer













                  I'm not familiar with the matrixmultiplication package, so I may be rambling here.



                  for (int j = 0; j <= numb.length; j += rows){


                  I'm not entirely sure what the outer for loop your have is for, but this most outer for loop causes you to ask for the values of the indices cols times more than you want.
                  I feel that you originally wanted to use this outer for loop to iterate through each row, and wasn't planning on having the second for loop iterating through cols perhaps?



                  Also, Kevin Anderson mentions this above. You might avoid this problem altogether if you return a double int array as opposed to storing all values in the matrix in a single dimension. I personally feel it would make more sense.



                  Just nitpicking, but I wouldn't make a new scanner every time you want to use one in a different method. You could just make a field at the top of your class, instantiate it once in your main method, and then pass it in as a parameter to all methods using the scanner.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 3:36









                  Shawn GeShawn Ge

                  1




                  1



























                      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%2f53292438%2fwhy-is-this-line-printing-twice%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

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線