JAVA HOMEWORK HELP THANK YOU! Write a method to read in two pieces of informatio
ID: 3686074 • Letter: J
Question
JAVA HOMEWORK HELP THANK YOU!
Write a method to read in two pieces of information from a user (using a Scanner object).
First, read in the name of a file. Using this name, create a file object to be returned from the method.
Use exception handling so that the code will compile.
Second, read in the number of lines in the file and store this in an int.
Use exception handling to account for the user typing a non-number.
If the user enters in a proper number, print it back out to the user before returning the file.
Note: you are not opening or reading from the file or checking the number- you are just creating the File object and returning it (presumably to be dealt with elsewhere).
Here is the method header- use this exact header with no changes:
public File readFileAndLength()
Explanation / Answer
import java.util.*;
class Example{
public static void main(String [] args){
System.out.println("enter the file name");
Scanner sc=new Scanner(System.in);
String file_name=sc.read();
ClassExam ce=new ClassExam();
File result=ce.readFileAndLength(file_name);
}
}
public class ClassExam{
public File readFileAndLength(String File_input)
{
File fp=new File(File_input);
long length=fp.length();
return fp;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.