Hello everyone, I have to write a method that given the name of a character file containing integers, one for each line, checks if the number of positive values in the file exceeds that of negative values.
This is what I managed to do. The problem I encounter is that it always tells me that positive values are greater than negative ones even when negative values are greater in the.txt file ... Could you help me? thank you
public static String valori(String nomeFile){ int accNeg=0; int accPos=0; try (FileInputStream bin = new FileInputStream(nomeFile); BufferedInputStream bis = new BufferedInputStream(bin); DataInputStream lettore = new DataInputStream(bis)) { while (lettore.available() != 0) { int parziale = lettore.readInt(); if (parziale > 0) { accPos++; } else if(parziale<0){ accNeg++; } } } catch (FileNotFoundException e) { System.out.println("File " + nomeFile + " non trovato"); } catch (IOException e) { System.out.println("Errore: " + e.getMessage()); } if(accPos>accNeg){ return "Valori positivi maggiori di quelli negativi"; } else return "Valori positivi minori o ugali di quelli negativi"; }