I need to read some big text file in scala. And append a line in that file to StringBuilder
. But I need to break the loop if the line in that file contain some String. And I don't want to append that String to StringBuilder. For example in java, loop A
will include "pengkor"
in resulted string. Loop B
not included that String, but in the loop there is break
statement that not available in scala. In loop C, I used for
statement, but with behaviour that very differents with for
loop in scala. My main concern is not to Include "pengkor"
String in the StringBuilder and not to load All content of the file to Scala List (for the purpose of List comprehension in scala or some other list operation) because the size of file.
public class TestDoWhile {
public static void main(String[] args) {
String s[] = {"makin", "manyun", "mama", "mabuk", "pengkor", "subtitle"};
String m = "";
StringBuilder builder = new StringBuilder();
// loop A
int a = 0;
while (!m.equals("pengkor")) {
m = s[a];
builder.append(m);
a++;
}
System.out.println(builder.toString());
// loop B
a = 0;
builder = new StringBuilder();
while (a < s.length) {
m = s[a];
if (!m.equals("pengkor")) {
builder.append(m);
} else {
break;
}
a++;
}
System.out.println(builder.toString());
// loop C
builder = new StringBuilder();
a = 0;
for (m = ""; !m.equals("pengkor"); m = s[a], a++) {
builder.append(m);
}
System.out.println(builder.toString());
}
}