[英]String processing using scanner
Question: Write a method called wordWrap that accepts a Scanner representing an input file as its parameter and outputs each line of the file to the console, word-wrapping all lines that are longer than 60 characters. 问题:编写一个名为wordWrap的方法,该方法接受代表输入文件的扫描程序作为其参数,并将文件的每一行输出到控制台,将所有长度超过60个字符的行进行文字换行。 For example, if a line contains 112 characters, the method should replace it with two lines: one containing the first 60 characters and another containing the final 52 characters.
例如,如果一行包含112个字符,则该方法应将其替换为两行:一行包含前60个字符,另一行包含最后52个字符。 A line containing 217 characters should be wrapped into four lines: three of length 60 and a final line of length 37.
包含217个字符的行应换成四行:三行长度为60,最后一行长度为37。
My code: 我的代码:
public void wordWrap(Scanner input) {
while(input.hasNextLine()){
String line=input.nextLine();
Scanner scan=new Scanner(line);
if(scan.hasNext()){
superOuter:
while(line.length()>0){
for(int i=0;i<line.length();i++){
if( i<60 && line.length()>59){
System.out.print(line.charAt(i));
}
//FINISH OFF LINE<=60 characters here
else if(line.length()<59){
for(int j=0;j<line.length();j++){
System.out.print(line.charAt(j));
}
System.out.println();
break superOuter;
}
else{
System.out.println();
line=line.substring(i);
break ;
}
}
}
}
else{
System.out.println();
}
}
}
Problem in the output: 输出中的问题:
Expected output: 预期产量:
Hello How are you The quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog I am fine Thank you The quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog This line is exactly sixty characters long; how interesting! Goodbye
Produced Output: 产生的输出:
Hello How are you The quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog I am fine Thank you The quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog This line is exactly sixty characters long; how interesting!This line is exactly sixty characters long; how interesting!This line is exactly sixty characters long; how interesting!... *** ERROR: excessive output on line 13
Where did i do wrong ???? 我在哪里做错了????
In the else
condition (for lines of exactly 60 characters), you are only breaking from the inner for
loop, whereas you want to break from the outer while
loop (and, therefore, you end up writing out the same line 60 times). 在
else
条件下(对于正好60个字符的行),您只是从内部for
循环中断,而您想从外部while
循环中断(因此,最终将同一行写出60次)。
Use instead break superOuter
as you have for lines of less than 59 characters. 对于少于59个字符的行,请改为使用
break superOuter
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.