简体   繁体   English

如何确定从Java中的文本文件读入的最大值和最小值

[英]How to Determine the Max and Min Values Read in From a Text File in Java

I am doing a homework assignment for a class, and am looking for some helpful pointers, not full solutions. 我正在上课的作业,正在寻找一些有用的指示,而不是完整的解决方案。 Basically, I have to write a Java program that reads in a text file and lists the information line by line, lists the line number, and finally, prints out the maximum and minimum value and the years that relates to each. 基本上,我必须编写一个Java程序,该程序读取一个文本文件并逐行列出信息,列出行号,最后打印出最大值和最小值以及与每个值相关的年份。 The text file contains a year and the temperature for that year. 文本文件包含一年和该年的温度。 So, it lists something like, "1900 50.9." 因此,它列出了类似“ 1900 50.9”的内容。 I am not meant to use an array, or the scanner, this is part of the assignment. 我不是要使用数组或扫描仪,这是分配的一部分。 I've already been able to successfully get the program to print out each year and corresponding temperature line by line with the line count. 我已经能够成功地使程序逐行打印,并逐行打印相应的温度。 I was told to, and did use the while loop. 有人告诉我,我确实使用了while循环。 Now, my only issues is accessing the text file in a way that I can somehow distinguish out of all the temperatures, which is the maximum and which is the minimum, and which year each one occurred in. I haven't sought help until now because I wanted to be able to figure it out on my own, but the assignment is no longer worth any credit due to late penalties. 现在,我唯一的问题是访问文本文件的方式可以以某种方式区分所有温度,即最高和最低,以及每年发生的年份。到目前为止,我一直没有寻求帮助。因为我希望自己能解决这个问题,但是由于罚款较晚,因此该作业不再值得任何荣誉。 Any help would be really appreciated, as I still want to solve this. 任何帮助将不胜感激,因为我仍然想解决这个问题。 Thanks. 谢谢。

This is what I have. 这就是我所拥有的。

public class main {

/**
 * @param args the command line arguments
 */

public static void main(String[] args) throws Exception {


File temps = new File ("temps.txt"); //Creates path to temps.txt file
FileReader textReader = new FileReader (temps); //Input information from temps.txt file into file reader
BufferedReader kb = new BufferedReader (textReader); //Use buffered reader to hold temps.txt file info from the file reader




String tempList; //Create string variable named tempList
int lineCount = 0; //Create integer variable named lineCount
String sep = ": Temp "; //Create string variable named sep (short for separation) and set it equal to the literal string ":"
String space = " "; //Create string variable named space and set it equal to an actual space between texts


System.out.println("The following is the provided information from the file input. ");
while ((tempList = kb.readLine()) !=null) { //while loop stating that as long as the text file still has values to read (is not null), continue to execute


    System.out.println("Line " + lineCount++ + ": Year " + tempList.replace(space, sep)); //Prints out the line number (lineCount++), the info from the temps.txt file with a ":" between the year and the number (tempList.replace (space,sep)

}




}

} }

The output thus far is this: 到目前为止的输出是这样的:

Line 0: Year 1900: Temp 50.9
Line 1: Year 1901: Temp 49
Line 2: Year 1902: Temp 49.7
Line 3: Year 1903: Temp 49.5
Line 4: Year 1904: Temp 47.1
Line 5: Year 1905: Temp 49.1

Etc. all the way to... 等等...

Line 99: Year 1999: Temp 52.7
BUILD SUCCESSFUL (total time: 0 seconds)

Here is a way to do it: 这是一种方法:

String tempList; //Create string variable named tempList
int lineCount = 0; //Create integer variable named lineCount
String sep = ": Temp "; //Create string variable named sep (short for separation) and set it equal to the literal string ":"
String space = " "; //Create string variable named space and set it equal to an actual space between texts

String maxValueYear = "";
String minValueYear = "";
double maxValue = 0;
double minValue = Double.MAX_VALUE;
System.out.println("The following is the provided information from the file input. ");
while ((tempList = kb.readLine()) !=null) { //while loop stating that as long as the text file still has values to read (is not null), continue to execute

    String year = tempList.substring(0, tempList.indexOf(space));
    double temp = Double.valueOf(tempList.substring(tempList.indexOf(space), tempList.length()));

    if (temp > maxValue) {
        maxValue = temp;
        maxValueYear = year;
    }
    if (temp < minValue) {
        minValue = temp;
        minValueYear = year;
    }
    System.out.println("Line " + lineCount++ + ": Year " + tempList.replace(space, sep)); //Prints out the line number (lineCount++), the info from the temps.txt file with a ":" between the year and the number (tempList.replace (space,sep)

}

System.out.println("The minimum temp occured in year " + minValueYear + " and was " + minValue);
System.out.println("The maximum temp occured in year " + maxValueYear + " and was " + maxValue);

You need to use a few variables to keep track of the min and max temp. 您需要使用一些变量来跟踪最小和最大温度。
Every time a higher (lower) temp comes along you update the variables. 每次出现更高(更低)的温度时,您都要更新变量。

Ok start with very high max and very low min outside the loop. 好的,从循环外的最高最大值和最低最小值开始。
As soon as you see a (higher) lower temp inside the loop you adjust the vars. 一旦在回路内看到(较高)较低的温度,就可以调整无功功率。
After the loop you recap. 循环后,您可以回顾一下。

Highest Seen So Far: -Infinity
    (or any really low number so that any number you see next will be higher)
Lowest Seen So Far: Infinity
    (or any really high number so that any number you see next will be lower)

Walk through each data point "d":
    is d higher than your latest "highest seen so far"?
        -> if yes, your new highest seen so far is now d
    is d lower than your latest "lowest seen so far"?
        -> if yes, your new lowest seen so far is now d

Your highest seen so far is now the highest data point
Your lowest seen so far is now the lowest data point

In pseudocode: 用伪代码:

highest = -inf
lowest = inf

for d in dataset:
  if d > highest:
    highest = d
  if d < lowest:
    lowest = d

print "highest: " + highest
print "lowest: " + lowest

here is an example 这是一个例子

let's say your dataset is 5 2 8 4 假设您的数据集是5 2 8 4

Step 0
    Highest: -inf
    Lowest: inf

Step 1
  See d = 5...that's higher than highest -inf, so new highest is 5.
  See d = 5...that's lower than lowest -inf, so new lowest is 5
    Highest: 5
    Lowest: 5

Step 2:
  See d = 2...that's not higher than highest 5...highest is still 5
  See d = 2...that is lower than lowest 5...new lowest is 2
    Highest: 5
    Lowest: 2

Step 3:
  See d = 8...that's higher than highest 5...new highest is 8
  See d = 8...that's not lower than lowest 2...lowest is still 2
    Highest: 8
    Lowest: 2

Step 4:
  See d = 4...that's not higher than highest 8...highest is still 8
  See d = 4...that's not lower than lowest 2...lowest is still 2
    Highest: 8
    Lowest: 2

Result:
    Highest: 8
    Lowest: 2

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

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