reader.readLine() not working as expected

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class CSS_Test_4_10_2024 {
public static void main(String args[]) {
try (BufferedReader reader = new BufferedReader(new FileReader("...\\sentences.txt"))) {
String sentence;
int c = 0; int inc = -5;
while ((sentence = reader.readLine()) != null) {

for (int i = 5; i > 0; i--) {
for (int o = 1; o < 4; o++) {
c++;
inc += i;
System.out.printf("<div class=\"text\" style=\"top: %dex;\">%s %d</div>%n", inc, sentence, c);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class CSS_Test_4_10_2024 {
public static void main(String args[]) {
try (BufferedReader reader = new BufferedReader(new FileReader("...\\sentences.txt"))) {
String sentence;
int c = 0; int inc = -5;
while ((sentence = reader.readLine()) != null) {

for (int i = 5; i > 0; i--) {
for (int o = 1; o < 4; o++) {
c++;
inc += i;
System.out.printf("<div class=\"text\" style=\"top: %dex;\">%s %d</div>%n", inc, sentence, c);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
I have a sentences.txt with 60 lines of text. I wanted it to print something along the lines of: <div class="text" style="top: [THIS STARTS FROM 5 AND INCREMENTS BY 5, AND FOR EACH THREE INCREMENTS, THE INCREMENT DECREASES BY 1]ex;">[SENTENCE GOES HERE] [HOW MANY TIMES THIS HAS RUN GOES HERE]</div> As an example, the first few lines should show: <div class="text" style="top: 5ex;"> Lorem ipsum... 1</div> <div class="text" style="top: 10ex;"> dolor sit amet... 2</div> <div class="text" style="top: 15ex;"> adipscicing ameold.. 3</div> <div class="text" style="top: 19ex;"> molor alor... 4</div> ... <div class="text" style="top: ??ex;"> blablabla... ?<60</div> To my confusion, the result looks like: ... <div class="text" style="top: 2694ex;">Curabitur rutrum... 899</div> <div class="text" style="top: 2695ex;">Curabitur rutrum... 900</div> Not only did reader.readLine() seem to read the same line (not shown, but it does this every 16~ times), and the top:??ex and number at the end seems to vastly exceed what I thought should have resulted from the for loop
4 Replies
JavaBot
JavaBot14mo ago
This post has been reserved for your question.
Hey @2ndchar! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Kyo-chan
Kyo-chan14mo ago
So... For each line you read, you do an additional for() that contains an additional for() Ringing a bell?
2ndchar
2ndcharOP14mo ago
uhh are you saying that i dont need two for loops? oh wait is it because of the while loop, whenever the for loops end, it runs again because of the while loop? wait i think thats it actually thx for that
JavaBot
JavaBot14mo ago
Post Closed
This post has been closed by <@614868464930258958>.

Did you find this page helpful?