#Is there another way to print the arrays?
1 messages · Page 1 of 1 (latest)
Is there another way to print the code?
public static void main(String[] args) {
String[] aCoolString;
aCoolString = new String[11];
aCoolString[1] = "Bro Code didn't help me on this one, java.dev/learn did!";
aCoolString[2] = "You should still subscribe to Bro Code though...";
aCoolString[3] = "Also go to java.dev/learn!";
aCoolString[4] = "Join the Java community on Discord: n7ETn8zf";
aCoolString[5] = "I should really stop talking, but I'm not going to!";
aCoolString[6] = "Just 4 more codes, you'll be fine!";
aCoolString[7] = "First sub to BroCode,";
aCoolString[8] = "Second join the Java community,";
aCoolString[9] = "Last learn from java.dev/learn!";
aCoolString[10] = "Great! Now shutting down....";
System.out.println(aCoolString[1]);
System.out.println(aCoolString[2]);
System.out.println(aCoolString[3]);
System.out.println(aCoolString[4]);
System.out.println(aCoolString[5]);
System.out.println(aCoolString[6]);
System.out.println(aCoolString[7]);
System.out.println(aCoolString[8]);
System.out.println(aCoolString[9]);
System.out.println(aCoolString[10]);
}
}```
Detected code, here are some useful tools:
System out
[Nothing]
use a loop
either put all the aCoolStrings into an array then loop over it, or jsut have for (int i = 0; i <= 10; i++)...
That's quite an old way. There are more modern ways such as https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/String.html#join(java.lang.CharSequence,java.lang.Iterable) and even that's not exactly new.
declaration: module: java.base, package: java.lang, class: String
Though for printing lines I wouldn't use String.join - for large arrays this would create a huge String first, and is just extra work.
Once you've encountered Streams and Lambdas or Method References, you can do this...
// get a Stream over the array
Arrays.stream(strings)
// for each element in the Stream, pass it to System.out.println
.forEach(System.out::println);
Which can also be written with a lambda...
// get a Stream over the array
Arrays.stream(strings)
// for each element in the Stream, pass it to System.out.println
.forEach(string -> System.out.println(string));
But you can still use a for-loop, and an enhanced for-loop is pretty simple...
for (String string : strings) {
System.out.println(string);
}
This last is actually translated to almost exactly the loop proposed by zarakshR...
Something like...
for (int $index = 0, $length = strings.length; $index < $length; $index++) {
String string = strings[$index];
System.out.println(string);
}
It’s funny how I’m the only one who doesn’t understand
I suspect you are new to programming, in which case I would recommend learning what a loop is and how to use it here; you can try to understand the other solutions posted here once you are a little more comfortable with Java and programming in general
There are a lot of ways beyond basic for loops.
use this loop
for(String str: aCoolString){
System.out.println(str);
}
yeah, but I think the guy is new to programming so he's probably better off starting with loops
Usually they would teach a while loop first.
int i = 0;
while (i < aCoolString.length) {
String string = aCoolString[i];
System.out.println(string);
i++;
}
The simple for-loop is just a rearrangement of that
for (int i = 0; i < aCoolString.length; i++) {
String string = aCoolString[i];
System.out.println(string);
}
And the enhanced-for loop does some of these steps for you
for (String string : aCoolString.) {
System.out.println(string);
}
The MOOC will teach this gradually.
For learning Java, we recommend MOOC.
It is a completely free introductory Java course created by the University of Helsinki, it is a great way to learn Java from the ground up.
Visit MOOC here:
https://java-programming.mooc.fi
(the course is available in both English and Finnish)
- The MOOC teaches a broad introduction to programming in Java in two parts - one at beginner, and another at intermediate level.
The end of the course is marked by creating your own Asteroids game clone! - The MOOC allows using features up to Java 11 - you can install Temurin OpenJDK 11 from the Adoptium project.
- To submit exercises for evaluation, you need to configure an Editor/IDE (Integrated Development Environment) with the TMC Plugin.
The course instructions will suggest to use TMCBeans/NetBeans or VS Code for the course, but you can also use IntelliJ, which we generally recommend.
- TMCBeans/NetBeans is the easiest to configure - but has the most dated user experience
- VS Code is very popular as an editor, but it is quite new for Java Development. Some extra configuration is needed.
- IntelliJ arguably has the best user experience and is most widely used Java IDE by professionals.
IntelliJ requires installing a version no newer than2023.1- because the IntelliJ TMC Plugin doesn't work with newer installs.
The IntelliJ Community version is completely free and all you need to install the TMC plugin.
To use IntelliJ with the MOOC, simply install the TMC plugin by opening IntelliJ -> File -> Settings -> Plugins and searching for TMC. You will then be able to use IntelliJ to complete MOOC.
About the course - Java Programming