Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble printing to same line

I'm trying to write a code where you enter an integer in the console, and then the integer you entered is shown bigger, made up of letters (like ascii art).

So let's say the input is 112. Then the output will be

   #       #     #####  
  ##      ##    #     # 
 # #     # #          # 
   #       #     #####  
   #       #    #       
   #       #    #       
 #####   #####  ####### 

My code will have the same output, just not in the same line :(

It will print one number under the other.. From my code you can see why:

import java.util.Scanner;
public class Tester {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String any = input.nextLine();
        String[] sArray = any.split("");

        for(int i=0; i<sArray.length; i++){
            if(sArray[i].equals("1")){
                System.out.println("  #  ");
                System.out.println(" ##  ");
                System.out.println("# #  ");
                System.out.println("  #  ");
                System.out.println("  #  ");
                System.out.println("  #  ");
                System.out.println("#####");
            }
            if(sArray[i].equals("2")){
                System.out.println(" ##### ");
                System.out.println("#     #");
                System.out.println("      #");
                System.out.println(" ##### ");
                System.out.println("#      ");
                System.out.println("#      ");
                System.out.println("#######");
            }
        }
    }
}

I somehow have to print all at once, not single output with println as my code.. Maybe there is an easy way to solve that, preferably without changing my entire code? I can imagine it could be done with a 2d array as well, but not sure. Hints are very welcome too. And this is no homework.

like image 783
cnmesr Avatar asked Mar 16 '26 08:03

cnmesr


1 Answers

Dirty but works:

private static final Map<Integer, String[]> art = new HashMap<Integer, String[]>() {{
    put(1, new String[] {
            "   #   ",
            "  ##   ",
            " # #   ",
            "   #   ",
            "   #   ",
            "   #   ",
            " ##### " });
    put(2, new String[] {
            " ##### ",
            "#     #",
            "      #",
            " ##### ",
            "#      ",
            "#      ",
            "#######" });
    }};

public static void main(String[] args) {
    int[] input = { 1, 1, 2 };
    for (int row = 0; row < 7; row++) {
        for (int num : input) {
            System.out.print(art.get(num)[row] + " ");
        }
        System.out.println();
    }
}

I skipped the scanner code and assumed an input of 1 1 2.

Output

   #       #     #####  
  ##      ##    #     # 
 # #     # #          # 
   #       #     #####  
   #       #    #       
   #       #    #       
 #####   #####  ####### 
like image 158
Harmlezz Avatar answered Mar 18 '26 22:03

Harmlezz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!