我正在从文件中读取文本,然后创建另一个包含这些文本的文件,但是当我调用函数.format时,无论我做什么,都会持续加下划线。 这是我的代码:为什么我的.format错误? JAVA文件
package number3;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Scanner;
public class ReadFileCarData {
private static Scanner Infile;
private static Formatter OutFile;
public static void main(String[] args) throws FileNotFoundException{
try
{
Infile = new Scanner(new File("cardata.txt"));
OutFile = new Formatter("cardatasold.txt");
}
catch(FileNotFoundException fnfe)
{
System.out.println("File not found");
System.exit(0);
}
System.out.println("Plate Brand Model Price Power Status");
while(Infile.hasNext())
{
String plate,brand,model,status;
int price,power;
plate = Infile.next();
brand = Infile.next();
model = Infile.next();
price = Infile.nextInt();
power = Infile.nextInt();
status = Infile.next();
while(status.equals("Sold"))
{
try
{
OutFile.format("%s %s %s %d %d \r\n",plate,brand,model,price,power);
}
catch(Exception e)
{
System.out.print("Error");
}
}
}
Infile.close();
OutFile.close();
}
}
该错误消息表示:
方法格式(区域设置,字符串,对象[])中的类型格式化不适用于参数(字符串,字符串,字符串,字符串,int,int)
我不明白为什么,因为根据我我写了正确的格式。 任何想法我做错了什么?谢谢。
“根据我” - 你错了。编译器总是正确的。如果你马上接受这个事实,你会更快。 – duffymo 2015-04-05 13:46:37
你正在编译什么java版本。整数对象应该被自动装箱。 – 2015-04-05 13:54:58