[英]I want to Copy the Largest number from one file to another File?But, I cannot find the largest Number
I tried to take the data taken from a file, convert it to a character array, find the largest number from file first.txt and copy the result to file second.txt. 我试图从文件中获取数据,将其转换为字符数组,从文件first.txt中找到最大的数字,然后将结果复制到文件second.txt。 However, when I compile and run the program the following problems occur:
但是,当我编译并运行程序时,会出现以下问题:
This is the code I am working with. 这是我正在使用的代码。
BufferedReader out = new BufferedReader(new FileReader("first.txt"));
PrintWriter in = new PrintWriter(new FileWriter("second.txt"));
char[] array = null;
String str = " ";
str = out.readLine();
array = str.toCharArray();
char max = array[0];
for (char c : array) {
if(c > max);
max = c;
in.write(max);
If it is not necessary to use char array, better don't. 如果没有必要使用char数组,最好不要使用。 Use String as it is easier in handling.
使用字符串,因为它更易于处理。 String.split() is a good way to split first.txt and Integer.parseInt() can help you with turning a String to integer.
String.split()是拆分first.txt的好方法,Integer.parseInt()可以帮助您将String转换为整数。 You can store them on an array and sort the array afterwards.
您可以将它们存储在数组上,然后对数组进行排序。
(But,If I try to put Numbers like below- 1 2 3 4 Then it takes only one number that is 1.) (但是,如果我尝试将数字放在下面-1 2 3 4,那么只需要一个数字即1。)
BufferedReader out = new BufferedReader(new FileReader("Nos for fnding highest.txt")); BufferedReader out = new BufferedReader(new FileReader(“ Nos for finding most.txt”)); BufferedWriter in = new BufferedWriter(new FileWriter("third.txt"));
BufferedWriter in = new BufferedWriter(new FileWriter(“ third.txt”)); String str = " ";
字符串str =“”; str = out.readLine();
str = out.readLine(); String []numbers = str.split(" ");
字符串[] numbers = str.split(“”); //array = str.toCharArray();
// array = str.toCharArray(); //char max = array[0];
// char max = array [0]; int[] array = new int[numbers.length];
int [] array = new int [numbers.length]; int count = 0;
int count = 0; //converting the string to integer format for(String strs : numbers){ array[count++] = Integer.parseInt(strs);
//将字符串转换为整数格式,用于(String strs:number){array [count ++] = Integer.parseInt(strs); } int max = array[0];
} int max = array [0]; for (int c : array) { if(c > max) max = c;
for(int c:array){if(c> max)max = c;
}
in.write(new Integer(max).toString());
out.close();
in.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.