Skip to content

Instantly share code, notes, and snippets.

@imduffy15
Created October 12, 2013 18:42
Show Gist options
  • Save imduffy15/6953420 to your computer and use it in GitHub Desktop.
Save imduffy15/6953420 to your computer and use it in GitHub Desktop.
Find largest number in a text file with java
import java.io.*;
import java.util.*;
class LargestFromTextFile {
public static void main(String[] args) {
try {
Scanner file = new Scanner(new File("numbers.txt"));
int largest = file.nextInt();
while(file.hasNextInt()) {
int number = file.nextInt();
if(number > largest) {
largest = number;
}
System.out.println(number);
}
file.close();
System.out.println("The biggest number in the file is: " + largest);
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
}
1
2
3
4
5
10
6
7
8
9
@stewwie
Copy link

stewwie commented Dec 4, 2020

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment