Question: (20 pts) In the following code inside a try block, what kind of object is passed to the Scanner constructor? Scanner file = new Scanner

  1. (20 pts) In the following code inside a try block, what kind of object is passed to the Scanner constructor?

Scanner file = new Scanner ( new File (test.txt));

  1. (20 pts) What is the output of this code sequence?

Scanner parse = new Scanner (A 1 B 2 C 3 D);

while (parse.hasNext())

System.out.print(parse.next());

  1. (20 pts) What is wrong with following code?

try

{

Scanner file = new Scanner(new File(c:\docs\data.txt));

int n = 0;

while (file.hasNext())

{

String s = file.nextLine();

if (s.equals(A))

n++;

}

System.out.println(The value of n is +n);

file.close();

}

catch(IOException ioe)

{

ioe.printStackTrace();

}

  1. (40 pts) You coded the following class:

try

{

Scanner file = new Scanner( new File(test.txt));

String s = file.nextLine();

} catch (FileNotFoundException fnf)

{

System.out.println(fnf.getMessage());

}

Will this code work correctly? If not how do you fix it.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!