Ask Question

g 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. Respond to other student postings by enhancing their code to write the summary output to a file instead of standard output.

+5
Answers (1)
  1. 9 March, 01:54
    0
    See explaination

    Explanation:

    //ReadFile. java

    import java. io. File;

    import java. io. FileNotFoundException;

    import java. util. Scanner;

    public class ReadFile

    {

    public static void main (String[] args)

    {

    int elementsCount=0;

    //Create a File class object

    File file=null;

    Scanner fileScanner=null;

    String fileName="sample. txt";

    try

    {

    //Create an instance of File class

    file=new File (fileName);

    //create a scanner class object

    fileScanner=new Scanner (file);

    //read file elements until end of file

    while (fileScanner. hasNext ())

    {

    double value=fileScanner. nextInt ();

    elementsCount++;

    }

    //print smallest value

    System. out. println (elementsCount+" data values are read");

    }

    //Catch the exception if file not found

    catch (FileNotFoundException e)

    {

    System. out. println ("File Not Found");

    }

    }

    }

    Sample output:

    sample. txt

    10

    20

    30

    40

    50

    output:

    5 data values are read
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “g Create your own data file consisting of integer, double or String values. Create your own unique Java application to read all data from ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers