Question

Consider the following code snippet. Scanner inputFile = new Scanner(hoursWorked.txt); Which of the following statements is
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The right answer is:

This code will treat the string "hoursWorked.txt" as an input value. (third option)

Explanation:

Scanner is used to read inputs from a given stream. If you want to read from a file, you should pass the file object to the constructor, like this: Scanner inputFile=new Scanner(new File("hoursWorked.txt")); If you just pass a String, like the above code, it will treat the String "hoursWorked.txt" as the input, not the file. So, inputFile.next() or  inputFile.nextLine() will return the string "hoursWorked.txt". Another example: if you create a scanner like this: Scanner scanner=new Scanner("Hello world how are you?"); then the scanner will have 5 tokens, "Hello", "world", "how", "are", "you?" each of which will be returned in subsequent calls to the next() method.

let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Add a comment
Know the answer?
Add Answer to:
Consider the following code snippet. Scanner inputFile = new Scanner("hoursWorked.txt"); Which of the following statements is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python: 1) Consider the following code segment: try : inputFile = open("lyrics.txt", "r") line = inputFile.readline()...

    Python: 1) Consider the following code segment: try : inputFile = open("lyrics.txt", "r") line = inputFile.readline() print(line) _____________________ print("Error") What should be placed in the blank so that the program will print Error instead of crashing if an exception occurs while opening or reading from the file? Group of answer choices a- except RuntimeError : b- except EnvironmentError : c- except IOError : d- except IndexError : 2) Consider the following code segment: line = "hello world!" parts = line.split()...

  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...

  • JAVA Which of these statements does not match by using exception action in Java? 1. Exception...

    JAVA Which of these statements does not match by using exception action in Java? 1. Exception action makes it possible for the calling method to handle errors in called methods.    2. Exception action simplifies programming since incorrect reporting and handling can be located in catch blocks separately from the rest of the code.    3. Exception action increases the performance of programs. 4. Java separates exception action from common processes. Why create instances of File Class? - several alternatives...

  • Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from...

    Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from lab 4.1, add the ability to read from a file. Modify the input function:       * Move the input function out of the Cargo class to just below the end of the Cargo class       * At the bottom of the input function, declare a Cargo object named         temp using the constructor that takes the six parameters.       * Use the Cargo output...

  • use python IDEL Please highlight the answer Problem 2 Files. Since almost all data can be...

    use python IDEL Please highlight the answer Problem 2 Files. Since almost all data can be stored in files, knowing how to use files in programs is invaluable. Today you will practice with opening a file for reading, writing and editing using the Python interpreter. Do not type messages in red into Python shell Question 14 0.5 pts create a new file mytext.txt for >>>f1 = open("mytest.txt","w+") writing & reading >>>f1.write("hello\n") >>>for i in range(5): f1.write(str(i) + "\n") >>>f1.readlines() record...

  • i have to write a program that asks the names of 2 files. the first should...

    i have to write a program that asks the names of 2 files. the first should be open for reading second for writing. the program should read the the contents of the first file change all characters to uppercase and store in the second file. the second file will be a copy of the first file, except that all characters will be uppercase. use notepad to test the program. i got this so far {         String firstfile;         String...

  • Lab 4.1 Utilizing the code from Lab 3.2, add an overloaded operator == which will be...

    Lab 4.1 Utilizing the code from Lab 3.2, add an overloaded operator == which will be used to compare two objects to see if they are equal. For our purposes, two objects are equal if their abbreviation and uldid are the same. You’ll need to make your overloaded operator a friend of the Cargo class. Create a unit1 object and load it with this data: uld – Pallet abbreviation – PAG uldid – PAG32597IB aircraft - 737 weight – 3321...

  • Which of the following statements uses a Scanner variable named sc to return the next value...

    Which of the following statements uses a Scanner variable named sc to return the next value the user enters at the consolse as a String object and store it in a variable named city? a. String city = sc.next(); b. String city = sc.nextString(); c. String city = sc.next().toString(); d. String city = sc.nextValue();

  • help! Run the FileClient with the debugger (with a breakpoint on the line with while). You'll...

    help! Run the FileClient with the debugger (with a breakpoint on the line with while). You'll need to create a file called input.txt in the src folder (which the program will open) and add some (any) contents to it. Expand the inputFile Scanner variable in the debugger window to check out its stored data. How much of it can you make sense of? Watch the values of keep looping, next word, and inputFileScanner's buf field in the debugger as you...

  • QUESTION 1 What is the output of the following code snippet? int main() { bool attendance...

    QUESTION 1 What is the output of the following code snippet? int main() { bool attendance = false; string str = "Unknown"; attendance = !(attendance); if (!attendance) { str = "False"; } if (attendance) { attendance = false; } if (attendance) { str = "True"; } else { str = "Maybe"; } cout << str << endl; return 0; } False True Unknown Maybe QUESTION 2 What is the output of the following code snippet? #include <iostream> #include <string> using...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT