【发布时间】:2012-10-11 05:49:43
【问题描述】:
我正在编写一个程序,它从文件中读取字符串和整数,然后复制数据并写入另一个文件。数据条目应以空格分隔。
我的输入和输出应该遵循以下格式,前两组数字是字符串,其他是整数:
123123 242323 09 08 06 44
当我运行我的代码时,我在线程“main”java.util.NoSuchElementException 中得到异常,我不知道为什么
import java.util.Scanner;
import java.io.*;
public class Billing {
public static void main(String[] args) throws IOException {
//define the variables
String callingnumber;
String callednumber;
String line;
int startinghour;
int startingminute;
int endinghour;
int endingminute;
//open input and output files
FileReader freader = new FileReader("BillingData.txt");
BufferedReader inFile = new BufferedReader(freader);
FileWriter fwriter = new FileWriter("BillingOutput.txt");
PrintWriter outFile = new PrintWriter (fwriter);
// set space between the numbers
line=inFile.readLine();
while(line!=null)
{
//creat a scanner to use space between the numbers
Scanner space = new Scanner(line).useDelimiter(" ");
callingnumber=space.next();
callednumber=space.next();
startinghour=space.nextInt();
startingminute=space.nextInt();
endinghour=space.nextInt();
endingminute=space.nextInt();
// writing data to file
outFile.printf("%s %s %d %d %d %d", callingnumber, callednumber,startinghour, startingminute, endinghour, endingminute);
line=inFile.readLine();
}//end while
//close the files
inFile.close();
outFile.close();
}//end of mine
}//end of class
【问题讨论】:
-
您的输入文件中有空白字段?
标签: java exception file-io exception-handling textinput