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

时间:2017-11-29 18:46:31

标签: java

我在用Java编写代码时遇到过这种情况。该错误提到我的代码newLine() has private access in PrintWriter中我从未收到过此错误,因为我无法理解为什么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();
    }

2 个答案:

答案 0 :(得分:1)

PrintWriter没有公共newLine()方法(see the Javadoc)。只需编写换行符"\n"即可创建新行,或者在没有参数的情况下调用println()

扫描仪没有readLine()方法。你可能意味着nextLine()

答案 1 :(得分:0)

似乎newLine()是PrintWriter中的私有方法,这意味着您无法从另一个即时PrintWriter作为对象的类外部调用它。