In java language Question 1 Write a method to read in two pieces of information
ID: 3685847 • Letter: I
Question
In java language
Question 1
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
Solution: Create a method to read in two pieces of information from a user as Shown below and Use Expection to read in the number of lines in the file
Case 1:
// Programmer-declared method user.
import java.util.Scanner;
public class userFinder
{
// obtain three floating-point values and locate the user value
public void determineUser()
{
// create Scanner for input from command window
Scanner input = new Scanner( System.in );
// obtain user input
System.out.print("Enter three floating-point values separated by spaces: " );
double user1 = input.nextDouble(); // read first double
double user2 = input.nextDouble(); // read second double
double user3 = input.nextDouble(); // read third double
// determine the user value
double result = user( user1, user2, user3 );
// display user value
System.out.println( "user is: " + result );
} // end method determineUser
// returns the User of its three double parameters
public double user( double x, double y, double z )
{
double userValue = x; // assume x is the largest to start
// determine whether y is greater than userValue
if ( y > userValue )
userValue = y;
// determine whether z is greater than userValue
if ( z > userValue )
userValue = z;
return userValue;
} // end method user
} // end class userFinder
Case 2:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class TryCatchFinally {
public static void main(String[] args) {
try { // main logic
System.out.println("Start of the main logic");
System.out.println("Try opening a file ...");
Scanner in = new Scanner(new File("test.in"));
System.out.println("File Found, processing the file ...");
System.out.println("End of the main logic");
} catch (FileNotFoundException ex)
{ // error handling separated from the main logic
System.out.println("File Not Found caught ...");
} finally { // always run regardless of exception status
System.out.println("finally-block runs regardless of the state of exception");
}
// after the try-catch-finally
System.out.println("After try-catch-finally, life goes on...");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.