Sutdent_Sequej
Sutdent_Sequej

Reputation: 59

Java error message "... has private access in ..."

I have run into a situation when writing code in 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

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();
    }

Upvotes: 3

Views: 9567

Answers (2)

Jim Garrison
Jim Garrison

Reputation: 86764

PrintWriter does not have a public newLine() method (see the Javadoc). Just write a newline character "\n" to make a new line, or call println() with no arguments.

Scanner does not have a readLine() method. You probably meant nextLine()

Upvotes: 4

Michał Bil
Michał Bil

Reputation: 3589

It seems like newLine() is a private method in PrintWriter what means that you can't invoke it externally from some other class that instantied PrintWriter as object.

Upvotes: 2

Related Questions