Sheet 5 Q4/5

//Sheet 5 - Q3 public static void main(String[] args){ Scanner input = new Scanner(; 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.