1

I want to print variable from right. This is my code to show '#' symbol:

static void printNumberSignVertical(int n, boolean newLine){
    for(int i = 0; i < n; i++){
        System.out.print('#');
    }
    if(newLine){
        System.out.println("");
    }
}

and then this is my output :

enter image description here

I want to print symbol '#' from the right. How can i make that symbol printed from right to left?

this is my representation how the print order is desired :

enter image description here

it starts from 1st one.

dazzle
  • 109
  • 2
  • 15
  • What do you mean right-to-left? You want it to print from the right side of the screen instead? – Zephyr Aug 08 '18 at 00:32
  • yes, i want to print symbol '#' from the right side first @Zephyr – dazzle Aug 08 '18 at 00:35
  • see https://docs.oracle.com/javase/tutorial/2d/text/textlayoutbidirectionaltext.html – Scary Wombat Aug 08 '18 at 00:35
  • or search for java rtl – Scary Wombat Aug 08 '18 at 00:35
  • @ScaryWombat - It looks like the OP is using the console. Would the AWT article apply? (I'm not familiar with AWT, but I believe it is all GUI-based, no?) – Zephyr Aug 08 '18 at 00:36
  • @dazzle - I'm not sure that can be done with `System.out` as the text direction depends on the OS and console implementation. You might need to do some calculations to determine the width of the display and go from there. – Zephyr Aug 08 '18 at 00:37
  • @dazzle your requirement is not clear enough. Could you please add your current and expected outcome examples. – Vikasdeep Singh Aug 08 '18 at 00:40
  • i have edited my question and give an example about my representation ouput @VicJordan – dazzle Aug 08 '18 at 01:25
  • @dazzle sadly I can not see images you have posted. Could you please post links from other site or instead of image provide textual information? – Vikasdeep Singh Aug 08 '18 at 01:28

2 Answers2

3

Assuming the terminal is exactly 80 characters wide, you could use formatted io to indent the String (#) (and a newline). Like,

System.out.printf("%80s%n", "#");

This still has to draw left to right, but it will align as I think you wanted.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0
for(int i = 79; i > -1; i--){
if(i>=n){
System.out.print(" ");
}else{
System.out.print('#');
     }


}

Example for n=4

Example for n=4

bakero98
  • 805
  • 7
  • 18