简体   繁体   English

Java错误消息“ ...在...中具有私有访问权”

[英]Java error message “… has private access in …”

I have run into a situation when writing code in Java. 用Java编写代码时遇到了一种情况。 The error mentions that in my code newLine() has private access in PrintWriter I have never gotten this error and it worries me as I cannot understand why newLine would be private to my variable PrintWriter 该错误提到在我的代码中newLine() has private access in PrintWriter我从未收到此错误,这让我感到担忧,因为我不明白为什么newLine将对变量PrintWriter private

Below will be my error messages and part of my code where this issue comes from. 以下将是我的错误消息以及此问题来自的部分代码。

Errors: 错误:

: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

Code: 码:

    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 does not have a public newLine() method ( see the Javadoc ). PrintWriter没有公共的newLine()方法( 请参见Javadoc )。 Just write a newline character "\\n" to make a new line, or call println() with no arguments. 只需编写换行符"\\n"以换行,或调用不带参数的println()

Scanner does not have a readLine() method. 扫描仪没有readLine()方法。 You probably meant nextLine() 您可能是指nextLine()

似乎newLine()是PrintWriter中的私有方法,这意味着您不能从将PrintWriter实例化为对象的其他类中进行外部调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM