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

如何在Java中确定从文本文件读入的最大值和最小值,java,string,text-files,max,min,Java,String,Text Files,Max,Min,我正在为一堂课做家庭作业,我在寻找一些有用的指导,而不是完整的解决方案。基本上,我必须编写一个Java程序,读取一个文本文件,逐行列出信息,列出行号,最后打印出最大值和最小值以及与之相关的年份。文本文件包含一年和该年的温度。所以,它列出了类似“1900 50.9”的东西。我不打算使用数组或扫描仪,这是任务的一部分。我已经能够成功地让程序打印出每年和相应的温度,逐行与行计数。我被告知这样做,并使用了while循环。现在,我唯一的问题是以某种方式访问文本文件,我可以从所有温度中分辨出哪个是最高温度,

我正在为一堂课做家庭作业,我在寻找一些有用的指导,而不是完整的解决方案。基本上,我必须编写一个Java程序,读取一个文本文件,逐行列出信息,列出行号,最后打印出最大值和最小值以及与之相关的年份。文本文件包含一年和该年的温度。所以,它列出了类似“1900 50.9”的东西。我不打算使用数组或扫描仪,这是任务的一部分。我已经能够成功地让程序打印出每年和相应的温度,逐行与行计数。我被告知这样做,并使用了while循环。现在,我唯一的问题是以某种方式访问文本文件,我可以从所有温度中分辨出哪个是最高温度,哪个是最低温度,每个温度发生在哪一年。直到现在我还没有寻求帮助,因为我想自己解决这个问题,但是由于迟到的处罚,这个任务不再值得任何赞扬。任何帮助都将非常感谢,因为我仍然想解决这个问题。谢谢

这就是我所拥有的

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);
字符串模板//创建名为templast的字符串变量
int lineCount=0//创建名为lineCount的整数变量
字符串sep=“:Temp”//创建名为sep(分离的缩写)的字符串变量,并将其设置为等于文字字符串“:”
字符串空格=”//创建名为space的字符串变量,并将其设置为文本之间的实际空格
字符串maxValueYear=“”;
字符串minValueYear=“”;
双最大值=0;
双最小值=双最大值;
System.out.println(“以下是从文件输入中提供的信息”);
while((templast=kb.readLine())!=null){//while循环说明只要文本文件仍有值要读取(不是null),就继续执行
字符串year=templast.substring(0,templast.indexOf(space));
double temp=double.valueOf(templast.substring(templast.indexOf(space),templast.length());
如果(温度>最大值){
最大值=温度;
maxValueYear=年;
}
如果(温度<最小值){
最小值=温度;
minValueYear=年;
}
System.out.println(“Line”+lineCount++“:Year”+tempList.replace(space,sep));//打印行号(lineCount++),temps.txt文件中的信息在年份和编号(tempList.replace(space,sep)之间带有“:”
}
System.out.println(“最低温度出现在“+minValueYear+”年,为“+minValue”);
System.out.println(“最大温度出现在“+maxValueYear+”年,为“+maxValue”);

以下是一种方法:

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);
String templast;//创建名为templast的字符串变量
int lineCount=0;//创建名为lineCount的整数变量
String sep=“:Temp”;//创建名为sep(分隔的缩写)的字符串变量,并将其设置为等于文本字符串:
String space=“;//创建名为space的字符串变量,并将其设置为文本之间的实际空格
字符串maxValueYear=“”;
字符串minValueYear=“”;
双最大值=0;
双最小值=双最大值;
System.out.println(“以下是从文件输入中提供的信息”);
while((templast=kb.readLine())!=null){//while循环说明只要文本文件仍有值要读取(不是null),就继续执行
字符串year=templast.substring(0,templast.indexOf(space));
double temp=double.valueOf(templast.substring(templast.indexOf(space),templast.length());
如果(温度>最大值){
最大值=温度;
maxValueYear=年;
}
如果(温度<最小值){
最小值=温度;
minValueYear=年;
}
System.out.println(“Line”+lineCount++“:Year”+tempList.replace(space,sep));//打印行号(lineCount++),temps.txt文件中的信息在年份和编号(tempList.replace(space,sep)之间带有“:”
}
System.out.println(“最低温度出现在“+minValueYear+”年,为“+minValue”);
System.out.println(“最大温度出现在“+maxValueYear+”年,为“+maxValue”);
在伪代码中:

highest = -inf
lowest = inf

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

print "highest: " + highest
print "lowest: " + lowest
在伪代码中:

highest = -inf
lowest = inf

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

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

这比我想象的要简单得多。你能再详细一点吗?谢谢。所以我必须用设定值声明变量?比如double max=99.9;double min=0.0;这些必须在while循环之外?我明白。但就调整循环中的变量而言,我真正的挑战是从技术上讲,文件的输入是作为字符串变量读取的,那么我如何将我在循环外声明的max和min变量与它们在循环中遇到的每个值进行比较呢?很抱歉问了这么多问题,但如果我不问,我不理解如何解决任何问题。再次感谢!@animegirlluna:您需要用字符串,然后你们可以比较。这比我想象的要简单得多。你们能再详细一点吗?谢谢。所以我必须用设定值声明变量?比如double max=99