gpt4 book ai didi

Java错误消息 "... has private access in ..."

转载 作者:行者123 更新时间:2023-12-02 11:56:42 25 4
gpt4 key购买 nike

我在用Java编写代码时遇到了一种情况。该错误提到在我的代码中 newLine() 在 PrintWriter 中具有私有(private)访问权限 我从未收到此错误,这让我很担心,因为我无法理解为什么 newLine 会是 private 到我的变量 PrintWriter

下面是我的错误消息以及此问题所在的部分代码。

错误:

:75: error: newLine() has private access in PrintWriter
savingsFile.newLine();
^
:77: error: newLine() has private access in PrintWriter
savingsFile.newLine();
^
:96: error: cannot find symbol
while((str1=savingsFile.readLine())!=null){
^
symbol: method readLine()
location: variable savingsFile of type Scanner
3 errors

代码:

    public static void writeToFile(String[] months, double[] savings) throws IOException{
PrintWriter savingsFile = null;
try {
savingsFile = new PrintWriter(new FileWriter("E:/savings.txt", true));
} catch (IOException e) {
e.printStackTrace();
}

//code to write to the file and close it
int ctr = 0;
while(ctr<6){
savingsFile.write(months[ctr]);
savingsFile.newLine();
savingsFile.write(savings[ctr]+"");
savingsFile.newLine();
ctr = ctr + 1;;
}
savingsFile.close();
}

public static void readFromFile(String[] months, double[] savings) throws IOException{
String str1, str2;
Scanner savingsFile = null;
try {
savingsFile = new Scanner(new File("E:/savings.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}

//code to read the file, load data in the array and close file
str1 = savingsFile.nextLine();
System.out.println(str1);
int ctr = 0;
while((str1=savingsFile.readLine())!=null){
System.out.println(str1);
}
savingsFile.close();
}

最佳答案

PrintWriter 没有公共(public) newLine() 方法 ( see the Javadoc )。只需编写一个换行符“\n”即可创建新行,或者调用不带参数的println()

扫描仪没有 readLine() 方法。您可能指的是 nextLine()

关于Java错误消息 "... has private access in ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47559677/

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