Java错误消息“;。。。在…中拥有私人访问权限。”;
我在用Java编写代码时遇到了一种情况。该错误提到,在我的代码中,Java错误消息“;。。。在…中拥有私人访问权限。”;,java,Java,我在用Java编写代码时遇到了一种情况。该错误提到,在我的代码中,newLine()在PrintWriter中具有私有访问权,我从未遇到过此错误,这让我很担心,因为我无法理解为什么newLine对我的变量PrintWriter 下面是我的错误消息和我的部分代码,这是这个问题的根源 错误: :75: error: newLine() has private access in PrintWriter savingsFile.newLine();
newLine()在PrintWriter中具有私有访问权,我从未遇到过此错误,这让我很担心,因为我无法理解为什么newLine
对我的变量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(字符串[]个月,双[]节省)引发IOException{
PrintWriter savingsFile=null;
试一试{
savingsFile=newprintwriter(newfilewriter(“E:/savings.txt”,true));
}捕获(IOE异常){
e、 printStackTrace();
}
//写入文件并关闭它的代码
int ctr=0;
而(ctrPrintWriter没有公共换行符()方法()。只需编写换行符“\n”
即可换行,或者调用println()
,不带任何参数
扫描仪没有readLine()
方法。您可能是指nextLine()
它看起来像newLine()
是PrintWriter中的私有方法,这意味着您不能从其他类外部调用它,该类将PrintWriter实例化为对象。如何使其非私有?@Sutdent\u Sequej,您不能使其非私有。它是标准Java类的内部方法,因此您不能更改其可见性。请使用Jim Garrison的建议。好的,非常感谢你今天帮助我!非常感谢你抽出一天的时间来帮助我!:)
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();
}