Como determinar os valores máximos e mínimos lidos em um arquivo de texto em Java
Eu estou fazendo uma lição de casa para uma aula e estou procurando algumas dicas úteis, não soluções completas. Basicamente, eu tenho que escrever um programa Java que lê um arquivo de texto e lista as informações linha por linha, lista o número da linha e, finalmente, imprime o valor máximo e mínimo e os anos que se relacionam com cada um. O arquivo de texto contém um ano e a temperatura para esse ano. Então, ele lista algo como "1900 50.9". Não pretendo usar uma matriz ou o scanner, isso faz parte da tarefa. Eu já consegui fazer com que o programa imprima a cada ano e a temperatura correspondente, linha por linha, com a contagem de linhas. Foi-me dito e usei o loop while. Agora, meu único problema é acessar o arquivo de texto de uma maneira que eu possa distinguir de todas as temperaturas, que é o máximo e qual é o mínimo, e em que ano cada uma ocorreu. Eu não procurei ajuda até agora. porque eu queria ser capaz de descobrir por conta própria, mas a atribuição não vale mais nenhum crédito devido a multas por atraso. Qualquer ajuda seria muito apreciada, pois ainda quero resolver isso. Obrigado.
É isso que tenho.
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)
}
}
}
A saída até agora é esta:
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. todo o caminho para ...
Line 99: Year 1999: Temp 52.7
BUILD SUCCESSFUL (total time: 0 seconds)