重複の可能性:
Java:スキャナーが改行で停止している
私はプログラミングに不慣れで、に新しい行があるかどうかを識別する方法があるかどうか疑問に思っていましたScanner
。私の方法ではScanner
、テキストファイルを取得し、60文字を超える場合は、単語の途中で分割せずに行を分割することになっています。
私が抱えている問題は、各トークンを使用しているため、コードが60文字未満の行を考慮せず、60文字までの前の行に追加することです。
これは私が作成したメソッドです:
public static void wordWrap3(Scanner s) {
String k = "";
int length = 0;
while(s.hasNext()) {
k = s.next();
length = length + k.length() + 1;
if (length>60) {
System.out.print("\n");
length = 0;
}
System.out.print(" " + k);
}
}
これは私が使用しているテキストです:
We're no strangers to love, You know the rules and so do I,
A full commitment's what Im thinking of, You wouldn't get this from any other guy.
I just wanna tell you how I'm feeling, Gotta make you understand.
Never gonna give you up, Never gonna let you down, Never gonna run around and desert you.
Never gonna make you cry, Never gonna say goodbye, Never gonna tell a lie and hurt you.