简体   繁体   English

我想将最大编号从一个文件复制到另一个文件?但是,找不到最大编号

[英]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: 但是,当我编译并运行程序时,会出现以下问题:

  1. Does not take two digit Arrays. 不使用两位数的数组。
  2. It also does not take more than 5 numbers from the text file 文本文件中的数字也不超过5
  3. Whenever I compile the program it writes the last number in the second file. 每当我编译程序时,它都会在第二个文件中写入最后一个数字。

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.

相关问题 我想将最大数字从一个文件复制到另一个文件?但是,输入数字时遇到一些问题 - I want to Copy the Largest number from one file to another File?But,I get some problems while inputting the numbers 查找文件中的最大数 - Find Largest Number in a File 我想从斐波那契数列中减去最大可能的数字 - I want to subtract the largest possible number from the Fibonacci sequence by decreasing it in 在批处理文件中,如何获取名称中编号最大的文件? - In a Batch file, How do I get files with the largest number in the names? 文本文件包含字符串和数字。 如何找到与字符串关联的最大数字? - Text file contains strings and numbers. How do I find the largest number associated with the string? 搜索文件中的第二大数字 - Searching for the second largest number in a file 查找最大的公用号码 - Find the Largest Common Number 如何从用户输入中找到最大的数字和最小的数字? (同时循环) - How do I find the largest number and the smallest number from the user input? (while loop) 我如何找到数组中的第二大数字? - How would I find the next largest number in an Array? 如何使用lambda从列表及其出现的位置中找到最大的数字? - How do I find the largest number from a list and its occurrences using lambdas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM