26.3k views
4 votes
Write a static method named fixSpacing that accepts a Scanner representing a file as a parameter and writes that file's text to the console, with multiple spaces or tabs reduced to single spaces between words that appear on the same line.

1 Answer

4 votes

Answer:

See the code and explanation below

Step-by-step explanation:

Is neccesary that accepts a Scan representing a file as a parameter and writes that file's text on the language, and we need to reduce the multiple spaces to single spaces between words that appear using just one line. Each word is to appear on the same line in output as it appears in the file. We need to consider that the lines can be blank.

The code on Jave is this one:

public void DavidSpaces(Scan sc) {

while(sc.hasNextLine()) {

String line = sc.nextLine();

Scanner linesc = new Scan(line);

while(linesc.hasNext())

System.out.print(linesc.next() + " ");

System.out.println();

}

}

The algorithm scheme is given on the picture attached.

Write a static method named fixSpacing that accepts a Scanner representing a file-example-1
User Sikandar Amla
by
9.1k points