我正在從文件中讀取文本,然後創建另一個包含這些文本的文件,但是當我調用函數.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