Im trying to get the max number in a certain text file the user inputs. I also put it into separate methods. Heres what I have so far:
public static void FindMax(String file)throws IOException{
int maximum = 0;
Scanner fileScanner = new Scanner(new File(file)); {
int big = fileScanner.nextInt();
while (fileScanner.hasNextInt()) {
int num = fileScanner.nextInt();
if(num > big) {
maximum++;
System.out.println(num);;
}
}
}
}
public static void main(String[] args)throws IOException{
Scanner keyboard = new Scanner(System.in);
String file;
System.out.print("Enter file: ");
file = keyboard.nextLine();
FindMax(file);
}
The output is printing all the content in the text file except the first value, instead of printing the maximum. For example if the text file is:
1
2
3
4
5
It only prints 2,3,4, and 5 and I don't know why.How can I get the max value? Id appreciate any help/advice. Thanks in advance.
EDIT: All of you guys are saying similar solutions but when I try them, it just prints the same output. Im very confused.