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
- (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));
- (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());
- (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();
}
- (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
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