【问题标题】:How to Determine the Max and Min Values Read in From a Text File in Java如何确定从 Java 中的文本文件中读取的最大值和最小值
【发布时间】:2013-03-10 03:58:50
【问题描述】:

我正在为一个班级做家庭作业,我正在寻找一些有用的指导,而不是完整的解决方案。基本上,我必须编写一个 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)

【问题讨论】:

    标签: java string text-files max min


    【解决方案1】:

    这是一种方法:

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

    【讨论】:

    • 好的,我了解您列出的所有内容,除了一部分。您能否向我解释一下 double temp = Double.valueOf (tempList.substring....etc为您的帮助!
    • @animegirlluna: temp.substring(int, int) 返回原始字符串的一部分,从第一次出现的空格开始(temp.indexOf(space)),结束于行 (tmpList.length())。函数 Double.valueOf(String) 接受该字符串,类似于“49.7”,并将创建一些双精度类型。一旦你有了数字,你就可以比较它。
    • 您是否有商务电子邮件或类似的东西,我可以用来不时与您联系,以便将来获得 Java 帮助?你一直很有帮助,我需要一个我知道他们在说什么的人,很明显你知道。作为大学入门课程的初学者,我仍然对您的解释有些动摇,所以如果您可以将其分解得更多,我会欣喜若狂。比如,为什么将 double maxValue 分配给 0,为什么将 minValue 分配给 Double.Max_Value?再一次,你太棒了,非常感谢!
    • @animegirlluna:maxValue 被赋值为 0,因为您需要一个足够小的值,以使任何其他值总是大于它。例如,如果较小的值为 -10,则 maxValue 的初始值应为 -11。而对于 minValue 也是一样的。 Double.MAX_VALUE 是任何 double 可以包含的较大值,因此任何值“应该”更小(double 非常大)。同样,我可以将 maxValue 初始化为 -Double.MAX_VALUE,以确保即使您的值小于零,它们仍然会被捕获。
    【解决方案2】:

    您需要使用一些变量来跟踪最低和最高温度。
    每次出现更高(更低)的温度时,您都会更新变量。

    好的,在循环外从非常高的最大值和非常低的最小值开始。
    一旦您在循环内看到(较高)较低温度,您就可以调整变量。
    循环结束后你回顾一下。

    【讨论】:

    • 这比我想象的要简单得多。你能详细说明一下吗?谢谢。
    • 所以我必须用设定值声明变量?像双最大 = 99.9;双最小 = 0.0;这些需要在while循环之外吗?我明白那个。但就调整循环中的变量而言,我真正面临的挑战是,因为文件的输入在技术上被读取为字符串变量,所以我如何将我在循环外声明的最大和最小变量与它们在循环中遇到的每个值进行比较环形?我很抱歉问了这么多问题,但如果我不问,我将无法理解任何事情是如何解决的。再次感谢!
    • @animegirlluna: 你需要用带有temp的String创建一个数字,然后你可以比较。
    【解决方案3】:
    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
    

    在伪代码中:

    highest = -inf
    lowest = inf
    
    for d in dataset:
      if d > highest:
        highest = d
      if d < lowest:
        lowest = d
    
    print "highest: " + highest
    print "lowest: " + lowest
    

    这是一个例子

    假设您的数据集是 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
    

    【讨论】:

    • 我知道你是怎么把它弄坏的,我只是不知道从这里到哪里去。我最大的问题是我的代码过于复杂,而且我还只是一个初学者,所以它给我带来了很多问题。我打算尝试用 parseInt 之类的东西做点什么。没有把我带到任何地方。
    猜你喜欢
    • 1970-01-01
    • 2014-07-15
    • 2023-04-09
    • 1970-01-01
    • 2017-08-01
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    相关资源
    最近更新 更多