简体   繁体   English

我想将最大数字从一个文件复制到另一个文件?但是,输入数字时遇到一些问题

[英]I want to Copy the Largest number from one file to another File?But,I get some problems while inputting the numbers

Whenever I try to write the Numbers like this- 每当我尝试这样写数字时,

(1
2
3
4
56)

It takes only the first number.What is wrong with my code.This is my code. 它只需要第一个数字。我的代码有什么问题。这是我的代码。

BufferedReader out = new BufferedReader(new FileReader("Nos for finding highest.txt"));
PrintWriter in = new PrintWriter(new FileWriter("third.txt"));
String str = " ";
str = out.readLine();
String[] numbers = str.split("\n");
int[] array = new int[numbers.length];
int count = 0;
for (String strs : numbers) {
    array[count++] = Integer.parseInt(strs);
}
int max = array[0];
for (int c : array) {
    if (c > max)
        max = c;
}

in.println(new Integer(max).toString());

in.close();

out.close();

If I take while((str = out.readLine()) != null) in the above code then it printouts all the numbers instead of printing max(Largest Number) . 如果我在上面的代码中使用while((str = out.readLine()) != null) ,那么它将打印出所有数字,而不是打印max(Largest Number)

If you input the numbers are on on the same line then split the input using space: 如果您输入的数字在同一行上,请使用空格分隔输入:

String[] numbers = str.split(" ");

If your numbers in the file are each in new line then use 如果文件中的数字都换行,请使用

String[] numbers = str.split("\n"); 

I'll expand on my comment for formatting reasons: Assuming your file has one number per line you'll want to first read them all into a list: 由于格式方面的原因,我将在评论中进行扩展:假设文件每行有一个数字,您首先需要将它们全部读入一个列表:

List<Integer> list = new LinkedList<>();
while((str = out.readLine()) != null) {
  //assuming the line is not empty and contains a valid integer
  list.add( Integer.valueOf(str) ); 
}

Then iterate and look for the highest number: 然后迭代并寻找最大的数字:

for( Integer i : list ) {
  //check for max here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想将最大编号从一个文件复制到另一个文件?但是,找不到最大编号 - I want to Copy the Largest number from one file to another File?But, I cannot find the largest Number 仅显示最大数字,而我想打印两个整数之间的所有数字 - only showing largest number while i want to print all numbers between two integers 在批处理文件中,如何获取名称中编号最大的文件? - 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? 我想从斐波那契数列中减去最大可能的数字 - I want to subtract the largest possible number from the Fibonacci sequence by decreasing it in 我想将一个Excel文件从一个位置复制到另一个URL服务器位置 - I want to copy an excel file from one location to other URL server location 我想将2个文件的内容复制到第三个文件中 - I want to copy the content of 2 file into third file 我是骆驼框架的新手,我尝试了一个程序将文件从一个目录复制到另一个目录,但是在运行程序时出现以下错误 - I am new to camel framework , I tried a program to copy a file from one directory to another but while running the program I got the following error 从数字列表中获取最大数字 - Get Largest Number from a list of numbers 如何在Java中将wav文件从一个目录复制到另一个目录? - How can I copy a wav file from one directory to another in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM