如何确定从Java中的文本文件读入的最大值和最小值 [英] How to Determine the Max and Min Values Read in From a Text File in Java

查看:156
本文介绍了如何确定从Java中的文本文件读入的最大值和最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个班级做一个家庭作业,我正在寻找一些有用的指针,而不是完整的解决方案。基本上,我必须编写一个Java程序,该程序读入文本文件并逐行列出信息,列出行号,最后打印出最大值和最小值以及与每个值相关的年份。文本文件包含该年份和该年份的温度。因此,它列出了类似1900 50.9的内容。我不打算使用数组或扫描仪,这是作业的一部分。我已经能够成功地使程序每年打印出来,并且相应的温度与行数一致。我被告知,并确实使用了while循环。现在,我唯一的问题是以某种方式访问​​文本文件,我可以以某种方式区分所有温度,这是最大值,哪个是最小值,以及每个温度发生在哪一年。我到目前为止还没有寻求帮助因为我希望能够自己解决这个问题,但由于延迟处罚,这项任务不再值得信任。任何帮助都会非常感激,因为我仍然想解决这个问题。谢谢。

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. The text file contains a year and the temperature for that year. So, it lists something like, "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. 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.

这就是我所拥有的。

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)

}




}

}

到目前为止的输出是:

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

等等一直到... 。

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


推荐答案

以下是一种方法:

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

这篇关于如何确定从Java中的文本文件读入的最大值和最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆