0

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.

Giulio Tedesco
  • 179
  • 1
  • 3
  • 14
  • I'm afraid the question isn't clear. Can you add a sample of expected output please? – Adam Rice Apr 02 '16 at 20:22
  • @AdamRice Thanks, I edited it, I thought it was clear that what it asked me to reproduce was what I expected as an output. Now I'm only getting this: 345678910111211109876543, with no vertical text. – Giulio Tedesco Apr 02 '16 at 20:56
  • Thanks, but no, not really. See the expected output. What i need are those "11111" (that are 10,11,12,11,10 in reality) to go up. Now i can only print 345678910111211109876543 in a single row. – Giulio Tedesco Apr 02 '16 at 21:26

1 Answers1

1

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.

Adam Rice
  • 880
  • 8
  • 20
  • This! Thanks so much. I'm sorry I've waisted your time not explaining myself correctly. That was the trick. Works like a charm. – Giulio Tedesco Apr 02 '16 at 22:04