How to get characters in one row | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to get characters in one row

https://code.sololearn.com/cFYfScs81fa6/?ref=app public class Pattern25 { public static void main(String args[]) { for(int i=1; i<=5; i++) { for(int j=1; j<=5;j++) { if(i==1 &&(j==1||j==2||j==3||j==4||j==5)) { System.out.print("*"); } else if(i==2 &&(j==1)) { System.out.print("*"); } else if(i==3 &&(j==1||j==2||j==3||j==4||j==5)) { System.out.print("*"); } else if(i==4 &&(j==5)) { System.out.print("*"); } else if(i==5 &&(j==1||j==2||j==3||j==4||j==5)) { System.out.print("*"); } else if(i==5 &&(j==1||j==2||j==3||j==4)) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } for(int i=1; i<=6; i++) { for(int j=1; j<=5;j++) { if(i==1) { System.out.print(" "); } if(i==2 &&(j==2||j==3||j==4)) { System.out.print("*"); } else if(i==3 &&(j==1||j==5)) { System.out.print("*"); } else if(i==4 &&(j==1||j==2||j==3||j==4||j==5)) { System.out.print("*"); } else if(i==5 &&(j==1||j==5)) { System.out.print("*"); } else if(i==6 &&(j==1||j==5)) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } for(int i=1; i<=6; i++) { for(int j=1; j<=5;j++) { if(i==1) { System.out.print(" "); } if(i==2 &&(j==1||j==5)) { System.out.print("*"); } else if(i==3 &&(j==1||j==5)) { System.out.print("*"); } else if(i==4 &&(j==1||j==2||j==3||j==4||j==5)) { System.out.print("*"); } else if(i==5 &&(j==1||j==5)) { System.out.print("*"); } else if(i==6 &&(j==1||j==5)) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } for(int i=1; i<=6; i++) { for(int j=1; j<=5;j++) { if(i==1) { System.out.print("

19th Feb 2023, 5:05 PM
Sahil Kshirsagar
Sahil Kshirsagar - avatar
9 Answers
+ 7
Ausgrindtube Doh! You could be right, I had considered that but then thought hmm.... clearly knows how to do it using loops and if-else statements so why ask that question? When I personally thought of "characters" all in one line I may have mistakenly assumed "string". Generally I avoid giving out solutions, especially to course materials and Code Coaches, only hints and tips. I figured they already have a diploma in C & are on their way thru C++; this isn't course material, so I'd have fun with the ternary practice. Changing it took about 3 minutes as it was a simple matter of reversing the loop lengths & small adjustments to the existing ternary. Ternary is weird, I'm just trying to get used to it. Much shorter but pretty ugly, especially as I didn't use many spaces. I wouldn't really recommend something this dense to beginners either, better to start with much simpler ternary but it's worth learning. https://code.sololearn.com/cN4Jm6VSTtO6/?ref=app https://code.sololearn.com/ckccR6p4Gze8/?ref=app
20th Feb 2023, 8:22 AM
Scott D
Scott D - avatar
+ 5
Scott D I think Sahil wants his text to read left to right, not vertically.
20th Feb 2023, 7:03 AM
Ausgrindtube
Ausgrindtube - avatar
+ 5
D_Stark Yes, Ausgrindtube pointed that out. I responded with two modified codes that do exactly that.
20th Feb 2023, 10:56 AM
Scott D
Scott D - avatar
+ 4
You would have to redo your loops to print all the tops of the letters before moving to the next line. **** *** * * * * * * * * * * * **** *** *** * * * * * * * * * **** * * * * * ***
19th Feb 2023, 7:26 PM
Ausgrindtube
Ausgrindtube - avatar
+ 4
Sahil Kshirsagar One example with a single string, another using nested ternary, a short form of if - else (seemed like a good opportunity to dust off my rusty java) https://code.sololearn.com/cb9avhPL7nfW/?ref=app https://code.sololearn.com/cr0rM78do5sR/?ref=app
20th Feb 2023, 5:25 AM
Scott D
Scott D - avatar
+ 3
Scott D I think he wants to print the name left to right rather then vertical
20th Feb 2023, 9:48 AM
D_Stark
D_Stark - avatar
+ 2
You could try to combine the first line of stars of each letter, print them, then the second, etc.
19th Feb 2023, 7:25 PM
HelloWorld
+ 2
Take the first row of each letter and print it then the same with the second ect.. eventually it will match, its more difficult printings in none gui languages as you have to take into account lines and spaces.
20th Feb 2023, 12:29 AM
D_Stark
D_Stark - avatar
+ 1
Hi
21st Feb 2023, 3:30 PM
Chethu CM
Chethu CM - avatar