gpt4 book ai didi

java - 尝试使用 Dr.Java 打开文件时出现编译错误

转载 作者:行者123 更新时间:2023-12-02 13:27:07 24 4
gpt4 key购买 nike

所以我目前正在大学学习java入门类(class),到目前为止我已经成功地找出并解决了问题。到目前为止。当我尝试打开和读取文件时遇到某种编译错误。我按照我的课本及其基本代码的设定说明来执行此操作,但最终以错误告终。然后我从我的书中取出源代码并将其放入 Dr.Java 中,但它仍然编译错误,这是来自书中的。所以我不太确定问题是什么,想知道是否有人可以引导我走向正确的方向。供引用。总是同样的两个错误。谢谢。

import java.util.Scanner; // Needed for the Scanner class
import java.io.*; // Needed for the File class

/**
This program reads data from a file.
*/

public class FileReadDemo
{
public static void main(String[] args) throws IOException
{
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// Get the filename.
System.out.print("Enter the filename: ");
String filename = keyboard.nextLine();

// Open the file.
File file = new File(filename);
Scanner inputFile = new Scanner(file);

// Read lines from the file until no more are left.
while (inputFile.hasNext())
{
// Read the next name.
String friendName = inputFile.nextLine();

// Display the last name read.
System.out.println(friendName);
}

// Close the file.
inputFile.close();
}
}

这是不断发生的错误代码。

发现 2 个错误:

File: C:\Users\aspea\Documents\Intro to Computers and Java\FileReadDemo.java  [line: 20]
Error: constructor File in class File cannot be applied to given types;
required: no arguments
found: java.lang.String
reason: actual and formal argument lists differ in length

File: C:\Users\aspea\Documents\Intro to Computers and Java\FileReadDemo.java
[line: 21]
Error: no suitable constructor found for Scanner(File)
constructor java.util.Scanner.Scanner(java.lang.Readable) is not applicable
(argument mismatch; File cannot be converted to java.lang.Readable)
constructor java.util.Scanner.Scanner(java.io.InputStream) is not applicable
(argument mismatch; File cannot be converted to java.io.InputStream)
constructor java.util.Scanner.Scanner(java.io.File) is not applicable
(argument mismatch; File cannot be converted to java.io.File)
constructor java.util.Scanner.Scanner(java.nio.file.Path) is not applicable
(argument mismatch; File cannot be converted to java.nio.file.Path)
constructor java.util.Scanner.Scanner(java.lang.String) is not applicable
(argument mismatch; File cannot be converted to java.lang.String)
constructor java.util.Scanner.Scanner(java.nio.channels.ReadableByteChannel) is not applicable
(argument mismatch; File cannot be converted to java.nio.channels.ReadableByteChannel)

最佳答案

您似乎有一些自定义的File类,它不是java.io.File,以下错误表明了这一事实:

Error: constructor File in class File cannot be applied to given types;
required: no arguments
found: java.lang.String
reason: actual and formal argument lists differ in length

还有

File cannot be converted to java.io.File

关于java - 尝试使用 Dr.Java 打开文件时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43350113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com