+ 2
Java code problem with array
I just want the results to have space in between them .the results are stacked together public class Program { public static void main(String[] args) { String[][] grids = { {"shoe","clothes","cap"}, {"watch","cream","tshirt"}, {"brace","socks","boxers"}, }; for(int i=0;i<grids.length;i++){ for(int j=0;j<grids[i].length;j++){ System.out.print(grids[i][j]); } System.out.println(); } } }
18 Answers
+ 5
Tony Jadesola, it could have been done as you tried, but not println, but print:
System.out.print(' ');
I advise you to carefully reread the lesson on the topic "Data Output".
+ 3
Add two tabs, ("\t"), to the output and not only will you have spaces, but everything will align with columns.
+ 3
Tony Jadesola, this course is a negligible part of JAVA.
Who knows, it may take you a lifetime to learn JAVA, because while you just started learning, and at this very moment, programmers continue to expand the capabilities of Java.
Good luck in your studies! ☺️
+ 2
Or, if good enough, try it this way:
https://code.sololearn.com/cIkqM8A867p1/?ref=app
+ 2
Coding Cat thanks very much that alternative is very much better
+ 2
Tony Jadesola, you do not know where you have the data output???😶
System.out.print(grids[i][j]+"\t\t");
Agree, it is much easier to use someone else's code than to think a little and try to write it yourself. ☺️
+ 2
ok
+ 1
Tony Jadesola, 🤣🤣🤣 now it is clear why you have not learned to make even spaces between variables.
Ask yourself the question:
“What is more important for me to complete the course as quickly as possible, or to assimilate the material of this course as best as possible in order to learn how to program?".
+ 1
Solo true am still practicing what ave learned cause it looks like I didn't know it well
+ 1
how i can be a pro in python?
+ 1
Tony Jadesola in your case it is probably better to use neither println nor print. I suggest printf.
You can call System.out.printf("%s ", grids[i][j])
If you want each output on a new line you need to append a linebreak (\n) to each Output.
0
@solo I don't understand where you said I should add two tabs
What I did was I added another System.out.println();
Results was like this:
Shoeclothescap
Watchcreamtshirt
Bracesocksboxers
As you can see it only gives space to the row(or column anyhow it's called)
0
Solo lol 😆 even if I use his code I still want to know how it works that's why I used your own
0
@🙂🙂
0
@solo I started java yesterday and I finished arrays today .how long do u think will take me to finish java
0
Instead of writing println(), use print(" ") to print a space between values.
- 1
System.out.print(grids[I][j]+" ");