在学校,我们正在学习编写一个程序,该程序将从名为“Names.txt”的文本文档中读取人名。我们必须在文件夹输出中输入名字的第一个字母,然后是完整的姓氏。所有这些必须在我们对while循环,for循环,if else语句和布尔值的基本知识下。我的代码遇到了问题:
Scanner scFile = new Scanner (new File ("Names.txt"));
String line = "", name = "";
int num, sum = 0;
while (scFile.hasNext())
{
Scanner scLine = new Scanner (line).useDelimiter("#");
name = scLine.next();
num = scLine.nextInt();
scLine.close();
System.out.println(name.substring(0,1) + "\t" + num);
sum = sum+ num;
}
scFile.close();
System.out.println("The sum is " + sum);
答案 0 :(得分:0)
\\s
是空格分隔符。所以试试这个:
Scanner scLine = new Scanner (line).useDelimiter("\\s");
答案 1 :(得分:0)
这很简单,可以从谷歌获得它,因为你考虑作为学生在下面的代码尝试与你的工作。它将按照您的要求工作..
BufferedReader reader = new BufferedReader(new FileReader("D:/Names.txt"));
String line = null;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
String[] lastName = line.split("\\s");
String firtLetterOfFirstName = lastName[0].substring(0, 1);
System.out.println("Folder Name : "+firtLetterOfFirstName + lastName[1]);
File file = new File("D:\\"+firtLetterOfFirstName + lastName[1]);
if(false== file.exists())
{
file.mkdir();
}
}