我新的编程,我想知道如果有一种方法来识别时,有一个新的在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.
我不清楚 - 你是否试图将*现有的*行放在一起,或者只是*插入*换行符?如果是后者,只需使用'nextLine()'一次读取一行... –
相关:http://stackoverflow.com/questions/10891511/java-scanner-stopping-at-new-line?rq= 1 – 2012-12-05 06:52:53
所以你想在60个字符后放一个换行符? –