Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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)); ArrayListlines = 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started