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

标签 java

我在用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/

相关文章:

java - Spring boot jpa查询生成器的聚合值?

java - java - 如何在不考虑空格的情况下比较java中的两个字符串?

java - 使用 Java 8 中断/停止 forEachRemaining

java - 通过 CheckBox onClick 更改 Android 应用程序的主题

java - 针对 Android 设备优化的 UniqueID

java - 请求的配置文件 "pom.xml"无法激活,因为它不存在

java - java 无效的赋值运算符

java - 注入(inject)不适用于嵌套对象[Jersey 2.22.1]

java - 使用 cloudhopper 发送异步 PDU 响应

java - SurefireReflectionException