0

I am writing a simple program as some practice for Java. It takes in integers and puts them into a two-dimensional array of R rows and C columns and then simply prints out each element of the array (just for troubleshooting). When I run the code it prints every integer like it should but the program does not stop running. I have to force stop it. Why is it not stopping on its own? I tried some basic debugging and tried to see if I accidentally had an infinite loop in the code but I couldn't find it.

In case it is important I am using IntelliJ Idea Ultimate 2019.3.

Thanks

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt(); //Takes number of test cases
        for(int i = 1; i <= T; i++){ //This is the counter for each test case
            int R = in.nextInt(); //R is # of rows
            int C = in.nextInt(); //C is # of columns
            int K = in.nextInt(); //K is the thickness allowed
            int numArray[][] = new int[R+1][C+1];

            for(int j = 0; j < R; j++){
                for(int k = 0; k < C; k++){
                    numArray[j][k] = in.nextInt();
                    System.out.println(numArray[j][k]);
                }
            }

        }
        in.close();
    }//End of main
}//End of main class

Edit: Just for future reference I will include the input and the output I was getting.

Input: 3 1 4 0 3 1 3 3 2 3 0 4 4 5 7 6 6 4 5 0 2 2 4 4 20 8 3 3 3 12 6 6 3 3 3 1 6 8 6 4

Output: 3 1 3 3 4 4 5 7 6 6 2 2 4 4 20 8 3 3 3 12 6 6 3 3 3 1 6 8 6 (and here the cursor is stuck blinking not doing anything and the program doesn't quit on its own)

I solved the issue by manually typing the input instead of copying and pasting it. Not sure what caused that issue but it worked!

Boxman07
  • 13
  • 1
  • 4

3 Answers3

0

This little piece of code requires the user to input 3 integers (R, C and K) T-times:

int T = in.nextInt(); 
for(int i = 1; i <= T; i++){
    int R = in.nextInt(); 
    int C = in.nextInt();
    int K = in.nextInt(); 
    // ...
}

Additionally the user has to input an integer R*C-times until i <= T:

for(int j = 0; j < R; j++){
    for(int k = 0; k < C; k++){
        numArray[j][k] = in.nextInt(); // << requires user-interaction each loop here >>
        System.out.println(numArray[j][k]);
    }
}

If you use T=1, R=1, C=1, K=1 the user has to input only 1 additional integer, as there is only 1 test-repitition and R*C = 1. With that your program terminates just as expected.

With bigger values of T, R, C, K the required user-interactions grow exponentially, which probably makes you think that your program should have ended but it still waits for additional user-input.

oRole
  • 1,233
  • 1
  • 6
  • 22
0

Ok I think I figured out the issue. I was copying and pasting the test inputs from a web page. I tried double checking to make sure the input was valid and then inputting them by hand and it fixed it. Not 100% why but that fixes the problem. Thanks for the help!

Boxman07
  • 13
  • 1
  • 4
-2

your for(int i = 1; i <= T; i++) probably newer ends because you are increasing T.

You need to made some condition in loop to exit. For example if (xx == yy) break;

Gedza
  • 1
  • 2