Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Create a class called StreamStats with one constructor and two methods cons

ID: 3696561 • Letter: J

Question

JAVA

Create a class called StreamStats with one constructor and two methods

constructor

Given an InputStream input for which you will process some statistics

Methods

read the entire InputStream supplied in the constructor. While reading the stream determine what types of input you are consuming

number of booleans in the stream

number of doubles in stream

number of ints in the stream

number of Strings (any token that does not classify as boolean, double or int)

example: "This is a 2 true 3.14 13.45"

one boolean

two doubles

one int

three Strings

return a vector of statistics in the stream seen in the stream. [# of boolean, # of double, # of int, # of String] (the order is the alphabetical order the types.)

if consume() has not been invoked, the vector returned should be all zeros.

Example: the vector returned in the example above would be [1,2,1,3]

NOTES

Look at the Scanner class to see how to check if the stream still has input, and then to see what kind of input is available.

Tokens that do not fit into boolean, int, or double are classified as Strings.

Make sure that your code checks for ints before doubles, for example, 5000 is an int but can fit into a double.

Explanation / Answer

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;


public class StreamStats {
   InputStream input;
   int booleanCount=0;
   int intCount=0;
   int doubleCount=0;
   int stringCount=0;
   public StreamStats(InputStream input) {
       this.input=input;
   }
   public void consume(){
       try{
           int data=0;
           String a="";
           ArrayList<String> dataStorage=new ArrayList<String>();
       while(data!=-1){
           data=input.read();
           if((char)data!=' '){
               a+=(char)data;
           }
           else{
               dataStorage.add(a);
               a="";
           }
       }
      
       for(String s:dataStorage){
           if(s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false")){
               booleanCount++;
           }
           else if(s.matches("[0-9]+")) {
               intCount++;
           }
           else if(s.matches("\d+\.\d+")){
               doubleCount++;
           }
           else{
               stringCount++;
           }
       }
       }
       catch(Exception e){
          
       }
   }
   public int[] stats(){
       int[] array=new int[4];
       array[0]=booleanCount;
       array[1]=doubleCount;
       array[2]=intCount;
       array[3]=stringCount;
      
       return array;
   }
   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       String input=sc.nextLine();
       input=input+" ";
       InputStream is=new ByteArrayInputStream(input.getBytes());
       StreamStats s1=new StreamStats(is);
       s1.consume();
       int[] a=s1.stats();
       System.out.println("["+a[0]+","+a[1]+","+a[2]+","+a[3]+"]");
   }
}