Write a static method printStrings that takes as a parameter

Write a static method printStrings that takes as a parameter a Scanner holding a sequence of integer/String pairs and that prints to System.out one line of output for each pair with the given String repeated the given number of times. For example if the Scanner contains the following data:

your method should produce the following output:

Notice that there is one line of output for each integer/String pair. The first line has 6 occurrences of \"fun.\", the second line has 3 occurrences of \"hello\", the third line has 10 occurrences of \"<>\" and the fourth line has 4 occurrences of \"wow!\". Notice that there are no extra spaces included in the output. You are to exactly reproduce the format of this sample output. You may assume that the input values always come in pairs with an integer followed by a String. If the Scanner is empty (no integer/String pairs), your method should produce no output.

Solution

sequence.txt (Save the file file under D Drive .then the path of the file pointing to it is D:\\\\sequence.txt )

6 fun. 3 hello 10 <> 4 wow!

Sequence.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Sequence {

   public static void main(String[] args) {
       //Creating the file class object by passing the filename as input
       File f=new File(\"D:\\\\sequence.txt\");
   try {
       //Creating the Scanner class object by passing the file as input
       Scanner sc=new Scanner(f);
      
       //calling the method by passing the Scanner class object as argument
       printStrings(sc);
   } catch (FileNotFoundException e) {
      
       System.out.println(\"Exception :\"+e);
   }

   }


   private static void printStrings(Scanner sc) {
       //Declaring the variables
       int num;
       String word;
         
       while(sc.hasNext())
       {
           //Getting the number from the file
           num=sc.nextInt();
          
           //getting the word form the file
           word=sc.next();
          
           //Display the word those many number of times of the number
           for(int i=0;i<num;i++)
           {
               //Displaying the word
               System.out.print(word);
           }
           System.out.println(\" \");
       }
      
   }

}

__________________

output:

fun.fun.fun.fun.fun.fun.
hellohellohello
<><><><><><><><><><>
wow!wow!wow!wow!

___________Thank You

Write a static method printStrings that takes as a parameter a Scanner holding a sequence of integer/String pairs and that prints to System.out one line of outp
Write a static method printStrings that takes as a parameter a Scanner holding a sequence of integer/String pairs and that prints to System.out one line of outp

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site