[英]Comparing numbers from File and PrintWriter
I have to write a function using PrintWriter which compares line by line: int in calkowite.txt with sredniaCalk and if int is bigger -> print it in cpowyzej.txt double in rzeczywiste.txt with sredniaRzecz and if double is bigger -> print it in rpowyzej.txt 我必须使用PrintWriter编写一个函数,该函数逐行进行比较:calkowite.txt中的int与sredniaCalk进行比较,如果int较大->在cpowyzej.txt中将其打印在sredniaRzecz中的rzeczywiste.txt中,如果double较大->打印它在rpowyzej.txt中
Why doesn't it work? 为什么不起作用?
String buf;
File rpowyzej = new File("c:/folder/rpowyzej.txt");
File cpowyzej = new File("c:/folder/cpowyzej.txt");
File rzeczywiste = new File("c:/folder/rzeczywiste.txt");
File calkowite = new File("c:/folder/calkowite.txt");
PrintWriter pw = new PrintWriter(rpowyzej);
PrintWriter cpw = new PrintWriter(cpowyzej);
Scanner sc = new Scanner(calkowite);
Scanner sr = new Scanner(rzeczywiste);
double sredniaRzecz = 0.28340862342710715;
int sredniaCalk = 1;
while (sc.hasNextLine()) {
if (Integer.parseInt(sc.nextLine()) > sredniaCalk){
cpw.println(Integer.parseInt(sc.nextLine()));
}
}
while (sr.hasNextLine()) {
if (Double.parseDouble(sr.nextLine()) > sredniaRzecz)
pw.println(Double.parseDouble(sr.nextLine()));
}
pw.close();
sc.close();
sr.close();
Only read the line once per pass through the loop: 每次循环仅读取一次该行:
while (sc.hasNextLine()) {
Integer i = Integer.parseInt(sc.nextLine());
if (i > sredniaCalk){
cpw.println(i);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.