collections framework Create a class named DataDemo. This class will have a main
ID: 3536063 • Letter: C
Question
collections framework
Create a class named DataDemo. This class will have a main method. You will create only one file,
which will contain the DataDemo class.
You are to read in the data from the data.txt file.
Read in each line of text from the file.
Use the String Class split method to create an array of tokens. Your delimiter will be the blank spaces.
Note that the data.txt file contains integers, characters, and strings. Each of these values will be a
token.
Use collections framework class(s) from the Java API (ArrayList, LinkedLIst, or Vector for example) to
store the tokens.
Use an Iterator to cycle through your list. For all the tokens that are integers, add them up. For all the
tokens that are characters, concat them all together in a string. For all the tokens that are strings concat
them together into a phrase.
Print out the integer sum, and the character string, and the phrase string as follows:
Sum of numbers = 55
Characters = ABCDEFGHIJ
Text string = Four score and seven years ago our forefathers brought forth
You may specify a data type on your collections class – for example if you use an ArrayList:
ArrayList <String> al = new ArrayList <String>()
Or you may declare your collection class without specifying a data type:
ArrayList al = new ArrayList()
Explanation / Answer
import java.io.*; /** * * @author Krishna */ public class FileRead { public static void main(String[] args) { DataInputStream in = null; int numSum = 0; String charString = new String(); String sentence = new String(); try { // File to read FileInputStream fstream = new FileInputStream("C:\Users\Krishna\Desktop\sample.txt"); // Get the object of DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console //System.out.println(strLine); String[] tokenArray = strLine.split("\s"); for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.