I am currently working on a small for-loop pattern and I stumbled upon a road block in my project. Basically, what I want is to make a for loop pattern from my .java file that reads the source char by char and replace the asterisks in my current for-loop pattern:
Into something like this
Here's the current code that I have that does the for-loop pattern
for( i = 1; i <= 5; ++i, z = 0) { // first line
for(int space = 1; space <= segments - i; ++space) {
System.out.print(" ");
}
while(z != 2 * i - 1) {
System.out.print("* ");
z++;
}
System.out.println();
}
for( i = 1; i <= 10; ++i, z = 0) { // second line
for(int space = 1; space <= segments - i; ++space) {
System.out.print(" ");
}
while(z != 2 * i - 1) {
System.out.print("* ");
z++;
}
System.out.println();
}
Thanks in Advance!
This is sample for how you can read a file char by char,
try {
BufferedReader r=new BufferedReader(new FileReader("file.txt"));
int ch;
while((ch=r.read())!=-1){
System.out.print((char)ch);
}
} catch(Exception e) {
System.out.print((char)ch);
}
To implement it in your code, just put this while loop in your while loop of asterisk's.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With