出于某种原因,我输入的所有内容都不断得到空行,我正在尝试制作一个读取输入的程序,删除所有不必要的空格,但每个单词之间有一个空格。然后打印出所有的行。
不使用任何哨兵来阻止。
import java.util.Scanner ;
import java.lang.Character;
import java.lang.String ;
public class trim
{
public static void main (String[]args)
{
String SongName = " " ;
String Song = " " ;
int i = 0 ;
int count = 0 ;
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine())
{
count++;
scan.nextLine();
}
for ( ; i < count ; i ++)
{
if (scan.hasNext())
{
SongName =scan.nextLine() ;
SongName = SongName.trim() ;
SongName = SongName.replaceAll("\\s+"," ");
if (Song== " " )
{
Song = SongName;
}
else
{
Song = Song + "\n" +SongName ;
}
}
System.out.println(Song) ;
}
}
}