gudii9
asked on
output nested for loop
package com.bharaththippireddy.trainings.jaxrs;
public class NestedFor2 {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
when i wrote above code and ran
package com.bharaththippireddy.tra
public class NestedFor2 {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
i got below output
*****
*****
*****
*****
i cannot get above output without nested for loops right?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This set the variable stars equal to a string with 5 stars, '*****'.is it java 8 or java 9 thing?
for (int j = 1 ; j <= 5 ; j++)
stars = stars + "*" ;
System.out.println(StringUis it java 8 or java 9 thing?tils.repea t("*", 5))
ASKER
This set the variable stars equal to a string with 5 stars, '*****'.
for (int j = 1 ; j <= 5 ; j++)
stars = stars + "*" ;
Select all
Open in new window
This outputs the variable stars 4 times.
for (int i = 1; i <= 4; i++) {
System.out.println(stars);
}
Select all
Open in new window
In particular this line outputs the variable stars once.
System.out.println(stars);
above is clear.
above loop we are preparing *****
next loop we are printing it in 4 lines
ASKER
public class NestedFor3 {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
System.out.println(StringUtils.repeat("*", 5));
}
}
}
giving compilation error at line 6
StringUtils cannot be resolved
how to add StingUtils to work?
You probably need to download Apache Commons Lang 3.6 (for Java 7.0 and higher).
above is clear.
above loop we are preparing *****
next loop we are printing it in 4 lines
Yep - sounds like you've mastered this one.
Doug
ASKER
i downloade the jar and added to build path as external jar
still why i need to import inside the program as below
still why i need to import inside the program as below
import org.apache.commons.lang3.StringUtils;
public class NestedFor3 {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
System.out.println(StringUtils.repeat("*", 5));
}
}
}
ASKER
abve gave below output
*****
*****
*****
*****
*****
*****
*****
*****
Success.
>>still why i need to import inside the program as below<<
Because StringUtils is not part of the standard java library (which includes java.utils, java.text, java.io, etc., which are all you mostly need to do many of these challenges). If you wanted to use ArrayUtils, for example, you would need to import org.apache.common.lang3.Ar rayUtils. There are some 30 very usable classes in the org.apache.common.lang3 library of which StringUtils and ArraUtils are only two, so you may just want to import org.apache.common.lang3.*, which would provide a vast amount of added functonality. For example, the StringUtils class has some 140 methods besides repeat (such as contains, isAlpha or isNumeric, reverse, etc.) that are extremely useful.
Because StringUtils is not part of the standard java library (which includes java.utils, java.text, java.io, etc., which are all you mostly need to do many of these challenges). If you wanted to use ArrayUtils, for example, you would need to import org.apache.common.lang3.Ar
ASKER
>>still why i need to import inside the program as below<<
Because StringUtils is not part of the standard java library (which includes java.utils, java.text, java.io, etc.
i downloade the jar and added to build path as external jarsince i added to build path does not import should happen by itself. i still need to refer the jar that i added to build path. if yes what is the point of adding jar to build path? please advise
Plenty of good help here.
ASKER
Open in new window
above gave below output
*****
*****
*****
*****
I am not clear what this line is doing ?
System.out.println(stars);
stars is varialbe right? are we calling variable in sysout to print 5 stars in 4 lines? please advise