0

Here's a problem. Please help me!

Enter a star bundle. (star='*') Suppose you input a bunch of stars one by one. Output by rotating it 90 degrees counterclockwise. In the first line, separate the star bundles by spaces. Array that stores star bundles has to be initialized with a space character(' '). From the second line, print a rotating star.

enter image description here

import java.util.Scanner;

public class Spin {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
         int n = sc.nextInt();
         int[][] A = new int[n][n];
            
         int K = 1;
         for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
               A[i][j] = K++;
            }
         }
               
               
public static int[][] leftRotate(int n, int[][] A) {
            int[][] B = new int[n][n];

            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                    B[i][j] = A[j][n-i-1];
                }
            }

    }

}
}
3
  • 2
    "Here's a problem." - no, you've posted your task but didn't state what problem you ran into. At a first glance your rotation code looks ok. Commented Apr 14, 2021 at 5:54
  • However, I don't know what to do when I get input from stars that have no fixed matrix. Commented Apr 14, 2021 at 5:59
  • Well, the matrix would surely have n * m dimensions then or at least you should treat it like this. When rotating that becomes a m * n matrix, i.e. you just swap the lengths of the 2 dimensions. Commented Apr 14, 2021 at 6:02

1 Answer 1

1

Take the input as a string using s = sc.nextLine(). E.g. if user enters *** * **, you get the same as s = "*** * **".

Split the line on spaces so you get String[] arr = {"***", "*", "**"}.

Find the longest string, e.g. in this case you get longest = 3.

The size of the array is the width of the resulting 3D grid, and the longest string is the height of the grid, so create the grid: char[][] grid = new char[longest][arr.length];

Fill the grid with spaces.

Now iterate arr and build up each column of the grid, starting at the bottom.

...   arr[0]   *..   arr[1]   *..   arr[2]   *..
...  ------->  *..  ------->  *..  ------->  *.*
...            *..            **.            ***

Print the grid.


static void rotate(String input) {
    String[] words = input.split(" ");
    int rows = Arrays.stream(words).mapToInt(String::length).max().orElse(0);
    char[][] grid = new char[rows][words.length];
    Arrays.stream(grid).forEach(row -> Arrays.fill(row, ' '));
    for (int i = 0; i < words.length; i++)
        for (int j = 0; j < words[i].length(); j++)
            grid[rows - j - 1][i] = words[i].charAt(j);
    Arrays.stream(grid).forEach(System.out::println);
}

Tests

rotate("*** * **");
rotate("** **** *");
rotate("The quick brown fox jumps over the lazy dog");

Outputs

*  
* *
***
 * 
 * 
** 
***
 kn s    
 cw pr y 
eioxmeezg
hurouvhao
Tqbfjotld
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this, but i got this error 'java.lang.NumberFormatException'. What does it mean?
@jjigaebudae Since this code doesn't parse any numbers, I have no idea where you'd get that error from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.