0
import java.util.Scanner;

public class RotateBy90Degree {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int m = s.nextInt();
        int[][] arr = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = s.nextInt();
            }
        }
        int[][] arr1 = new int[m][n];
        s.close();
        int row=0, col=n-1;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(row>=0 && col>=0){
                    arr1[row][col]=arr[j][i];
                }
                if(col<=0){
                    row++;
                    col=n-1;
                } else {
                    col--;
                }
            }
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                System.out.print(arr1[i][j]+" ");
            }
            System.out.println();
        }  
    }
}

This is Java Code I don't know why when I am running this on VScode I am not getting any error, my output is perfectly good but If I run this code on a website called "pepcoding" it showing me this error like this

Exception in thread "main" java.util.NoSuchElementException

at java.base/java.util.Scanner.throwFor(Scanner.java:937)

at java.base/java.util.Scanner.next(Scanner.java:1594)

at java.base/java.util.Scanner.nextInt(Scanner.java:2258)

at java.base/java.util.Scanner.nextInt(Scanner.java:2212)

at Main.main(Main.java:11)

can somebody confirm that it is my error in the program or the website is not correct

  • Maybe try changing the class name to Main. Some websites restrict public class names to only Main. – AkSh Jun 12 '21 at 15:33
  • I ran your code and its working fine , try to run it somewhere else – Adnan Jun 12 '21 at 15:34
  • Does this answer your question? [What is java.util.NoSuchElementException and how do i fix it?](https://stackoverflow.com/questions/28249102/what-is-java-util-nosuchelementexception-and-how-do-i-fix-it) – Hülya Jun 12 '21 at 15:38

1 Answers1

0

you need to use hasNextInt() to check if scanner has next int input which can avoid scenario where user just press enter without typing number

sanjeevRm
  • 1,541
  • 2
  • 13
  • 24