tttt

/* OOP */ import java.util.Scanner; import java.util.*; import java.io.*; public class test{ /* Exception Handling */ /*public static void main(String[] args){ try{ int x = 3/0; System.out.println(x); System.out.println("test"); } catch(ArithmeticException e){ System.out.println("You divided by a zero"); } System.out.println("Another instruction"); }*/ /* ArrayList */ /*public static void main(String[] args){ List<Integer> list = new ArrayList<>(); list.add(5); list.add(3); list.add(1); list.add(8); //Sort in decreasing order Collections.sort(list); Collections.reverse(list); for(int i=0;i<4;i++) System.out.println(list.get(i)); }*/ /* Files I/O */ // Reading /* public static void main(String[] args) throws Exception{ try{ File file = new File("data.txt"); Scanner read = new Scanner(file); while(read.hasNextLine()){ String i = read.next(); System.out.println(i); } read.close(); } catch(FileNotFoundException e){ System.out.println("File not found"); } } */ //Writing /* public static void main(String[] args){ try{ File file = new File("data.txt"); PrintWriter output = new PrintWriter(file); output.println("test"); output.close(); } catch(FileNotFoundException e){ System.out.println("File not found"); } } */ //Sheet 5 - Q3 /* public static void main(String[] args){ Scanner input = new Scanner(System.in); ArrayList<Integer> nums = new ArrayList<>(); for(int i=0;i<5;i++){ nums.add(input.nextInt()); } Collections.sort(nums); for(int i=0;i<5;i++){ System.out.println(nums.get(i)); } } */ //Sheet 5 - Q4 /* public static void main(String[] args){ try{ //Open Desired file File input = new File("data.txt"); //Create a temporary file File output = new File("temp.txt"); //Use Scanner to read from data.txt Scanner read = new Scanner(input); //Use PrintWriter to write in the temp file PrintWriter write = new PrintWriter(output); //Loop through every line in data.txt while(read.hasNextLine()){ //Write contents of data in temp except for args[0] //use trim() to remove the \n that we automatically read String z = read.nextLine().trim(); if(!z.equals(args[0])){ write.println(z); } } //Delete data.txt input.delete(); //Rename temp to data.txt output.renameTo(new File("data.txt")); //Close both files read.close(); write.close(); } //Catch any exception catch(Exception e){ System.out.println("Error."); } } */ }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.