我试图从一个数据文件中找到一行文字的最大和最小值。虽然这个数据文件中有很多行文字,但我想知道这些文字的最大和最小值。
数据
-99 1 2 3 4 5 6 7 8 9 10 12345
10 9 8 7 6 5 4 3 2 1 -99
10 20 30 40 50 -11818 40 30 20 10
32767
255 255
9 10 -88 100 -555 1000
10 10 10 11 456
-111 1 2 3 9 11 20 30
9 8 7 6 5 4 3 2 0 -2 -989
12 15 18 21 23 1000
250 19 17 15 13 11 10 9 6 3 2 1 -455
9 10 -8 10000 -5000 1000
我试着把这些线变成数组,但这也让我有所收获。
这是我的代码
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
// It is called "Average". Don't mind that.
public class Average
{
public static void main(String[] args) throws IOException
{
Scanner file = new Scanner(new File("average.dat"));
String[] array = null;
while(file.hasNextLine())
{
//Storing the ints from the data file into an array.
String line = file.nextLine();
String[] str = line.split("\\s+");
array = new String[str.length];
for (int i = 0; i < str.length; i++){
array[i] = (str[i]);
}
System.out.println("Max:" + Average.getMax(array));
System.out.println("Min:" + Average.getMin(array));
System.out.println();
}
}
//Simple way of getting max value. Nabbed it off google.
public static String getMax(String[] inputArray)
{
String maxValue = inputArray[0];
for(int i=1;i < inputArray.length;i++){
if(inputArray[i].compareTo(maxValue)>0){
maxValue = inputArray[i];
}
}
return maxValue;
}
//Simple way of getting min value. Took off google as well.
public static String getMin(String[] inputArray){
String minValue = inputArray[0];
for(int i=1;i<inputArray.length;i++){
if(inputArray[i].compareTo(minValue)<0){
minValue = inputArray[i];
}
}
return minValue;
}
}
这里是我的输出
Max:9
Min:-99
Max:9
Min:-99
Max:50
Min:-11818
Max:32767
Min:32767
Max:255
Min:255
Max:9
Min:-555
Max:456
Min:10
Max:9
Min:-111
Max:9
Min:-2
Max:23
Min:1000
Max:9
Min:-455
Max:9
Min:-5000
如你所见,最小值是正确的。但是,最大值却不对?它无缘无故给我9,我不知道为什么。你可能会问 "为什么不把数组做成int[]而不是String[]?" 我不知道如何将数据文件中的ints复制到一个整数组中。我只真正知道如何用一个String数组来做。有人知道正确的方法吗?
你可以用int
public static String getMax(String[] inputArray) {
int maxValue = Integer.MIN_VALUE;
for(int i = 0; i < inputArray.length; i++) {
if(maxValue < Integer.parseInt(inputArray[i])){
maxValue = Integer.parseInt(inputArray[i]);
}
}
return String.valueOf(maxValue);
}
public static String getMin(String[] inputArray) {
int minValue = Integer.MAX_VALUE;
for(int i = 0; i < inputArray.length; i++) {
if(minValue > Integer.parseInt(inputArray[i])){
minValue = Integer.parseInt(inputArray[i]);
}
}
return String.valueOf(minValue);
}
紧凑的解决方案,并附有注释和解释。
public static void main(String[] args) {
// Create an input stream
try (Stream<String> stream = Files.lines(Paths.get("average.dat"))) {
// Get each line from the stream
stream.forEach(line -> {
// Cut the line into peaces
String[] numbers = line.split(" ");
// Create a list of integers from the pieces
List<Integer> integers = Arrays.stream(numbers).map(Integer::parseInt).collect(Collectors.toList());
// Calculate max
System.out.println("Max:" + integers.stream().flatMapToInt(IntStream::of).max().orElseThrow());
// Calculate min
System.out.println("Min:" + integers.stream().flatMapToInt(IntStream::of).min().orElseThrow());
// Ouput a new line
System.out.println();
});
} catch (IOException e) {
e.printStackTrace();
}
你应该使用: Integer.pharseToInt()那么当你有一个整数数组的时候 你就可以很容易地找到最大的值:
int getMax(int [] array){
int max = INTEGER.MIN_VALUE;
for (int i=0: i<array.length; i++)
if (array[i]>max)
max = array[i];
return max;
}
就用完全对称的函数来找最小值.
实际上,你不需要分别计算最大值和最小值。考虑以下问题。
max
如果是,则将其分配给 max
.while (file.hasNextLine()) {
// Storing the ints from the data file into an array.
String[] str = file.nextLine().split("\\s+");
int max = Integer.parseInt(str[0]);
int min = max;
for (int i = 1; i < str.length; i++) {
int v = Integer.parseInt(str[i]);
if (v > max) {
max = v;
} else if (v < min) {
min = v;
}
}
System.out.println("Max:" + max);
System.out.println("Min:" + min);
System.out.println();
}