Search code examples
javapalindromevertical-text

Java - vertical integers and palindrome


I stumbled upon an exercise that asked me to reproduce this (that's the expected output):

       11111
3456789012109876543

This is a palindrome (at the bottom) where numbers higher that 9 (double digits) have to be written vertical. This sounds complicated to me, and I needed some help.

This is what I did so far, the palindrome:

  class Print {
  public static void main(String[] args) {  
    System.out.println("Insert a number from 1 to 100: ");
    int input = Read.anInt();
    System.out.println("Insert another number from 1 to 100: ");
    int output = Read.anInt();    
     int a = input;    
     for (int i = a; i < output; i++){
            System.out.print(a);
            a++;
       }
     a = input -1;    
     for (int j = output; j > a; j--){
            System.out.print(output);
            output--;
       }    
  }
}

Could you help me by explaining how to make sure numbers higher than 9 will be written vertically?

AdamRice: i mean this:

3456789111119876543
       01210

But what I've managed to do so far is this mess:

 456789101
0
111
1
121110987654

This is all probably because I'm completely ignoring arrays.


Solution

  • Apologies for being a bit slow. After finally understanding the problem, I think I have a solution.

    import java.util.Scanner;
    
    public class VerticalText {
    
        public static void main(String[] args) {
    
            Scanner Read = new Scanner(System.in);
            System.out.println("Insert a number from 1 to 100: ");
            int start = Read.nextInt();
            System.out.println("Insert another number from 1 to 100: ");
            int end = Read.nextInt();
    
            String numbers = "";
    
            for(int i = start; i <= end; i++)
            {
                if(i < 10)
                {
                    numbers += String.format("%02d", i);
                }
                else
                {
                    numbers += i;
                }
            }
    
            for(int i = (end-1); i >= start; i--)
            {
                if(i < 10)
                {
                    numbers += String.format("%02d", i);
                }
                else
                {
                    numbers += i;
                }
            }
    
            String row1 = "";
            String row2 = "";
    
            char[] chars = numbers.toCharArray();
    
            for(int i = 0; i < chars.length; i++)
            {
                if(chars[i] == '0')
                {
                    chars[i] = ' ';
                }
                row1 += chars[i];
                i++;
                row2 += chars[i];
            }
    
            System.out.println(row1);
            System.out.println(row2);
        }
    }
    

    With inputs 5 and 15, it produced the following output:

         11111111111     
    567890123454321098765
    

    Explanation I build a string of the numbers and if it's less than 10 format it with a leading 0. This extra 0 is just a placeholder. When it comes to printing, we can print a space instead of a zero.