[英]Java error message “… has private access in …”
用Java編寫代碼時遇到了一種情況。 該錯誤提到在我的代碼中newLine() has private access in PrintWriter
我從未收到此錯誤,這讓我感到擔憂,因為我不明白為什么newLine
將對變量PrintWriter
private
以下將是我的錯誤消息以及此問題來自的部分代碼。
錯誤:
: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沒有公共的newLine()方法( 請參見Javadoc )。 只需編寫換行符"\\n"
以換行,或調用不帶參數的println()
。
掃描儀沒有readLine()
方法。 您可能是指nextLine()
似乎newLine()
是PrintWriter中的私有方法,這意味着您不能從將PrintWriter實例化為對象的其他類中進行外部調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.