Question: i need the file name of this code Scanner in = new Scanner(System. in ); System. out .print(Enter file name: ); String filename = in.nextLine();
i need the file name of this code Scanner in = new Scanner(System.in); System.out.print("Enter file name: "); String filename = in.nextLine(); try { Scanner fin = new Scanner(new FileInputStream(filename)); ArrayList lines = new ArrayList(); while(fin.hasNextLine()) { lines.add(fin.nextLine()); } // part 1 System.out.print("Enter a line number: "); int num = in.nextInt(); String line; if(num >= lines.size()) { System.out.println("Line number is out of range."); } else { System.out.println("Inverted line: "); line = lines.get(num-1); for(int i = line.length()-1; i >= 0; i--) { System.out.print(line.charAt(i)); } System.out.println(); } // part 2 System.out.print("Enter a line number: "); num = in.nextInt(); if(num >= lines.size()) { System.out.println("Line number is out of range."); } else { System.out.println("line: " + lines.get(num-1)); } // part 3 lines.sort(new Comparator() { public int compare(String o1, String o2) { return o2.compareTo(o1); } }); System.out.println("Lines in sorted order"); for(int i = 0; i < lines.size(); ++i) { System.out.println(lines.get(i)); } fin.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
