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

前端 未结 3 996
不思量自难忘°
不思量自难忘° 2021-01-25 08:33

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 a

3条回答
  •  耶瑟儿~
    2021-01-25 09:00

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

提交回复
热议问题