为什么我收到 .format 错误? JAVA 个文件
Why am i getting error for .format? JAVA FILES
我正在从一个文件中读取文本,然后创建另一个包含这些文本的文件,但是当我调用函数 .format 时,无论我做什么,它都会一直带有下划线。
这是我的代码:
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();
}
}
错误消息说:
The method format(Locale, String, Object[]) in the type Formatter is not applicable for the arguments (String, String, String, String, int, int)
我不明白为什么,因为根据我的说法,我写的格式是正确的。
知道我做错了什么吗?谢谢
您可能需要检查您的 Java 版本。
同时,你可以尝试使用这个功能
OutFile.format(Locale.getDefault(),"%s %s %s %d %d \r\n", new Object[]{plate,brand,model,price,power});
希望对您有所帮助 ;)
对我来说效果很好。那是输出:
Plate Brand Model Price Power Status
您遇到的问题是您的编译器无法识别方法中的可变参数(并认为它只是一个 Object[]
)并且可能甚至不会自动装箱整数。这两个都是 Java 1.5.
的特性
这可能意味着您的 IDE/compiler 设置有误。打开项目的设置并查找目标兼容性、生成的 .class 兼容性或类似内容,并将其从 1.4 切换到 1.7/1.8.
我正在从一个文件中读取文本,然后创建另一个包含这些文本的文件,但是当我调用函数 .format 时,无论我做什么,它都会一直带有下划线。 这是我的代码:
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();
}
}
错误消息说:
The method format(Locale, String, Object[]) in the type Formatter is not applicable for the arguments (String, String, String, String, int, int)
我不明白为什么,因为根据我的说法,我写的格式是正确的。 知道我做错了什么吗?谢谢
您可能需要检查您的 Java 版本。
同时,你可以尝试使用这个功能
OutFile.format(Locale.getDefault(),"%s %s %s %d %d \r\n", new Object[]{plate,brand,model,price,power});
希望对您有所帮助 ;)
对我来说效果很好。那是输出:
Plate Brand Model Price Power Status
您遇到的问题是您的编译器无法识别方法中的可变参数(并认为它只是一个 Object[]
)并且可能甚至不会自动装箱整数。这两个都是 Java 1.5.
这可能意味着您的 IDE/compiler 设置有误。打开项目的设置并查找目标兼容性、生成的 .class 兼容性或类似内容,并将其从 1.4 切换到 1.7/1.8.