Reputation: 1
I have a txt file where there are multiple paragraphs and they are delimited by a "%". I'm trying to find the maximum characters for every line in every separate paragraphs in order to make a padding. My problem is that it finds the maximum number of character overall, instead of finding the max in every paragraph/
int nmar = 0;
int max = 0;
while (input.hasNextLine()) {
input.useDelimiter("%");
String nume = input.next();
lines = linii;
Scanner scan = new Scanner(nume);
while (scan.hasNextLine()) {
String linecount = scan.nextLine();
nmar = linecount.length();
if (nmar > max) {
max = nmar;
} else if (nmar == 0) {
break;
}
System.out.println(max);
}
}
Upvotes: 0
Views: 43
Reputation: 9756
I moved the nmar
and max
inside the while to reset them at every paragraph, seems to work.
int nmar, max;
while (input.hasNextLine()) {
nmar = 0;
max = 0;
input.useDelimiter("%");
String nume = input.next();
Scanner scan = new Scanner(nume);
while (scan.hasNextLine()) {
String linecount = scan.nextLine();
nmar = linecount.length();
if (nmar > max) {
max = nmar;
} else if (nmar == 0) {
break;
}
}
System.out.println(max);
}
With this input
dkdjdhd\ndpepe%nd\njkfdlfrkefjrekl%dffd
I get
7
15
4
Upvotes: 1