Help please For this assignment you are to write an unusual formatting program t
ID: 3565814 • Letter: H
Question
Help please
For this assignment you are to write an unusual formatting program that works like this:
First you enter a (small) integer value that represents the number of words(tokens) in a displayed line - say k. Then you type in multiple lines of text, ending with two carriage returns (following the model for data input that is used, for example, in program Backwards in chapter 7). When you finish typing the text, your program should then print to the console the text you typed, but with only k words (tokens) per line. Here is a sample:
Your program should have two classes, a driver class called TokenDriver, and a second class called TokenStore. TokenDriver should handle user input, using the Scanner class. IMPORTANT: in TokenDriver use two Scanner objects, one to read the words-per-line value (3, in the above example); and a second to read in the lines of text.
You must use an ArrayList in TokenStore to save the typed line information (in my solution I break up the lines into tokens first, and thus store tokens rather than lines in the ArrayList).
Read from the keyboard in TokenDriver.
Put your driver class, named TokenDriver, below. Do not include any import statements, as the necessary ones have been provided:
Explanation / Answer
package vijay;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class StringMain {
public static void main(String[] args) throws Exception {
System.out.println("enter n : ");
Scanner sc=new Scanner(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=sc.nextInt();
System.out.println("enter String ...");
String str=br.readLine();
StringTokenizer st=new StringTokenizer(str," ");
int c=1;
while(st.hasMoreTokens())
{
if(c%(n+1)!=0)
{
System.out.print(st.nextToken()+" ");
c++;
}
else
{
c=1;
System.out.println();
}
}
}
}
o/p:
enter n :
3
enter String ...
hi this is vijay how are you der
hi this is
vijay how are
you der
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.