Various Java programs to illustrate various concepts from an old CS course at UTexas.edu. This java code is from the last century.
Fork this repo to your account, clone your repo to your machine.
Read all this code. Puzzle over what it does.
Choose one of these code samples. Add a file with your name as the filename (ex: SteveJ.md), write me a brief paragraph that describes what your chosen code sample does and how it works.
Add, commit, push and do a Pull Request.
//Selected
1 import java.util.Scanner; 2 import java.io.File; 3 import java.io.IOException; 4 5 public class ReadAndPrintScores 6 { 7 public static void main(String[] args) 8 { try 9 { Scanner s = new Scanner( new File("scores.dat") ); 10 while( s.hasNextInt() ) 11 { System.out.println( s.nextInt() ); 12 } 13 } 14 catch(IOException e) 15 { System.out.println( e ); 16 } 17 } 18 }
This is a class name ReadAndPrintScores. They imported java.util.Scanner library, file library, and exception library. Then they have main method which does not return any value. Then we have try block - where exception can occur followed by catch block - which can catch an error if it occurs. Inside Try block is where Scanner method is where it is going to read the File Scores.dat to see if there is an error. Then we have a while loop, that has hasNextInt method, the hasNextInt method takes input from scanner to see whether next token is a valid int value or not, if is true then it will print out the int value, else it will print out “e” from catch block.
From this code, I have learnt how to use try and catch exception handling block.