Write the definition of a method named copy that receives a reference to a Scann
ID: 3811343 • Letter: W
Question
Write the definition of a method named copy that receives a reference to a Scanner object associated with a stream of input. The method reads all the Strings remaining to be read from the stream and displays them, one on a line with no other spacing, onto standard output. The method must not use a loop of any kind (for. white, do-while) to accomplish its job.//Import required packages. import java.io.*; import java.util.*; public class CopyDemo {//main method public static void main{String args(J) throws lOException 1//Import required packages. 2 3 import java.io.*; 4 5 import java.util.*; 6 7 public class CopyDemo {8 9//main method 10 11 public static voidExplanation / Answer
HI, I have implemented using recursion.
Please let me know in case of any issue.
import java.io.*;
import java.util.*;
public class CopyDemo{
public static void copy(Scanner sc){
if(sc.hasNext()){
System.out.println(sc.next());
copy(sc); // calling recursively
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.