Create your own data file consisting of integer double or St

Create your own data file consisting of integer, double or String values.

Create your own unique Java application to read all data from the file echoing the data to standard output. After all data has been read, display how many data were read. For example, if 10 integers were read, the application should display all 10 integers and at the end of the output, print \"10 data values were read\"

Demonstrate your code compiles and runs without issue.

Solution

DisplayDataTypeCount.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Pattern;


public class DisplayDataTypeCount {

  
   public static void main(String[] args) throws FileNotFoundException {
       Scanner scan = new Scanner(System.in);
       System.out.print(\"Enter file name: \");
       String fileName = scan.next();
       File file = new File(fileName);
       int stringCount = 0;
       int doubleCount = 0;
       int intCount = 0;
       int totalCount = 0;
       ArrayList<Integer> intList = new ArrayList<Integer>();
       ArrayList<Double> doubleList = new ArrayList<Double>();
       ArrayList<String> stringList = new ArrayList<String>();
       if(file.exists()){
           Scanner scan1 = new Scanner(file);
           String decimalPattern = \"([0-9]*)\\\\.([0-9]*)\";
           String integerPattern = \"[0-9]+\";
           while(scan1.hasNext()){
               Object obj = scan1.next();
               totalCount++;
               boolean doubleMatch = Pattern.matches(decimalPattern, obj.toString());
               boolean intMatch = Pattern.matches(integerPattern, obj.toString());
               if(doubleMatch){
                   doubleCount++;
                   doubleList.add(Double.valueOf(\"\"+obj));
               }
               else if(intMatch){
                   intCount++;
                   intList.add(Integer.valueOf(\"\"+obj));
               }
               else {
                   stringCount++;
                   stringList.add(\"\"+obj);
               }
           }
           System.out.println(\"Number of data were read: \"+totalCount);
           System.out.println(intCount+\" integers values were read\");
           System.out.println(intList);
           System.out.println(doubleCount+\" doubles values were read\");
           System.out.println(doubleList);
           System.out.println(stringCount+\" strings values were read\");
           System.out.println(stringList);
          
       }
       else{
           System.out.println(\"File does not exist\");
       }
   }

}

Output:

Enter file name: D:\\\\input2.txt
Number of data were read: 22
8 integers values were read
[1, 2, 4, 4, 4, 4, 4, 9999]
3 doubles values were read
[5.5, 6.6, 7.8]
11 strings values were read
[many, spaces, on, this, line!, ss, dd, ee, a, d, g]

input2.txt:

many spaces on this line! 1 2 4 5.5 6.6 4 ss dd ee 4 4 4 a d g 7.8 9999

Create your own data file consisting of integer, double or String values. Create your own unique Java application to read all data from the file echoing the dat
Create your own data file consisting of integer, double or String values. Create your own unique Java application to read all data from the file echoing the dat

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site