繁体   English   中英

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

[英]I want to Copy the Largest number from one file to another File?But, I cannot find the largest Number

我试图从文件中获取数据,将其转换为字符数组,从文件first.txt中找到最大的数字,然后将结果复制到文件second.txt。 但是,当我编译并运行程序时,会出现以下问题:

  1. 不使用两位数的数组。
  2. 文本文件中的数字也不超过5
  3. 每当我编译程序时,它都会在第二个文件中写入最后一个数字。

这是我正在使用的代码。

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);

如果没有必要使用char数组,最好不要使用。 使用字符串,因为它更易于处理。 String.split()是拆分first.txt的好方法,Integer.parseInt()可以帮助您将String转换为整数。 您可以将它们存储在数组上,然后对数组进行排序。

(但是,如果我尝试将数字放在下面-1 2 3 4,那么只需要一个数字即1。)

BufferedReader out = new BufferedReader(new FileReader(“ Nos for finding most.txt”)); BufferedWriter in = new BufferedWriter(new FileWriter(“ third.txt”)); 字符串str =“”; str = out.readLine(); 字符串[] numbers = str.split(“”); // array = str.toCharArray(); // char max = array [0]; int [] array = new int [numbers.length]; int count = 0; //将字符串转换为整数格式,用于(String strs:number){array [count ++] = Integer.parseInt(strs); } int max = array [0]; 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM