Ask Question
28 November, 20:43

Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of lines, write the code necessary to read in every line and print them all out on a single line, separated by a space. Hint: Use input. nextLine () to read a line and use input. hasNextLine () to test if the input has a next line available to read.

+4
Answers (1)
  1. 29 November, 00:24
    0
    Code

    import java. util. Scanner;

    public class FileReader{

    public static void main (String []args) throws FileNotFoundException {

    Scanner scanner = new Scanner (new FileInputStream (new File ("D:/input. txt")));

    String output="";

    while (scanner. hasNextLine ()) {

    output+=scanner. nextLine () + " ";

    }

    System. out. println (output);

    }

    }

    Code Explanation

    Create scanner object by passing file input stream which will read file as input.

    Then declare a string variable which will cancatinate the file text into single line util we don't have any other input from scanner.

    Then print that output.

    Input File

    Hello this is dummy

    file.

    happy holidays

    bye

    Output

    Hello this is dummy file. happy holidays bye
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of lines, write the ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers