In: Computer Science
Assignment:
Create data file consisting of integer, double or String values.
Create 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"
My issue is displaying how many integers were read and how many strings were read.
Here's my code:
/*
* File: ReadDateFile.java
* Author: Mia Robinson
* Date: 10/7/2020
* Purpose: This program will read data from a file
* and will echo the data to standard output
*/
package readdatafile;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author mrsar
*/
public class ReadDataFile {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner scannerIn = null;
FileInputStream in = null;
BufferedReader inputStream = null;
// int equivalent of the char
int fileChar;
String fileLine;
int intCount=0; // Initialize integer and word counter
String stringCount =0;
try {
in = new FileInputStream("Read.txt");
System.out.println("Fun with integers");
// Read one char at a time
while ((fileChar = in.read()) != -1) {
int length = String.valueOf(fileChar).length();
intCount += String.valueOf(fileChar).length();
// convert int to char
System.out.print((char) fileChar);
}
// Print the number or string values read to the console
System.out.println("\n\n" + intCount + " data values were read!");
// Separate the file output
System.out.println(" \n");
// Use of Scanner and BufferedReader
inputStream = new BufferedReader(new FileReader("ReadStrings.txt"));
System.out.println("We've reached the end:");
// Read one Line using BufferedReader
while ((fileLine = inputStream.readLine() ) != null) {
System.out.println(fileLine);
}
// Print the number or string values read to the console
System.out.println("\n" + stringCount + " string values were read!");
} catch (IOException io) {
System.out.println("File IO exception" + io.getMessage());
} finally {
// Need another catch for closing
// the streams
try {
// Close the streams
if (in != null) {
in.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException io) {
System.out.println("Issue closing the Files" +
io.getMessage());
}
}
}
}
EDIT:
Hey, if you are using windows please specify you file path using "\\"
for example: "C:\\Downloads\\javaprograms\\filename"
----------------------------------------------------------------
Hey , by reading the question what I understood is :
1. There is a file which contains only numbers , assume Numbers.txt . We should read all the numbers and display the count.
2. There is another file which contains only String data , assume words.txt. We should read all the words ( separated by space ) and display the number of words.
3. And same with the double.
--------------------------------------------------------------------------------------------------------------------------------------------------------------
file-name: ReadData.java --------------------------------------------- import java.io.*; import java.util.*; /** * We are using two different files here * 1. numbers.txt contains only numbers ( integers ) * 2. words.txt contains only Strings * both files are in the same directory "/home/rahul/Pictures" ( here in my case ) * NOTE : You need to change the file-names and file-address as per your computer. * if windows , your path address should be like "C:\\Downloads\\JavaFiles\\file-name" ( example ) * * one thing to be remembered , when we read a file using buffered reader, br.readLine( ) * * -> this always gives a String type as output, so it considers a 100 in file also as String "100" * -> So, I used Integer.parseInt( ) for converting this String "100" to Integer type 100 */ public class ReadData { // main method public static void main(String[] args) { // file-name and file-address String filename = "numbers.txt"; String fileaddress = "/home/rahul/Pictures/"; /* If you want to store the file data we can add it to the ArrayList, Here in the problem they asked to just display it. */ // ArrayList<Integer> listOfIntegers = new ArrayList<>(); // to count the number of integers. int count = 0; BufferedReader br; /** * try catch blocks for the purpose of exception handling * FileNotFoundException : This type of exception is raised, when there is no file in your specified location ( path ) * IOException : This is raised if there is some I/O problem * NumberFormatException : This is raised when, the data which is read is not of desired type ( can't be stored in that primitive data type ) * here we want to read an integer, so if we get a String value (example: "robinson" ) then NumberFormatException occurs. */ try { br = new BufferedReader(new FileReader(fileaddress+filename)); String line = br.readLine(); while( line != null ) { int data = Integer.parseInt(line); System.out.println(data); count++; line = br.readLine(); } br.close(); System.out.println(count + " data values were read "); } catch(FileNotFoundException e) { System.out.println("No file with that name!!"); } catch (IOException e) { System.out.println("No data!"); } catch (NumberFormatException n){ System.out.println("The value is not an Integer"); } System.out.println("-----------------------------"); /***********************************************************************/ /** * Now we read data from words.txt, which contains only Strings ( separated by space " " ) * We display those Strings and count */ String filename2 = "words.txt"; int stringCount = 0; try { br = new BufferedReader(new FileReader(fileaddress+filename2)); String data = br.readLine(); while( data != null ) { String[] input = data.split(" "); for(String s : input) { System.out.println(s); stringCount++; } data = br.readLine(); } System.out.println(stringCount+ " data values were read"); } catch(FileNotFoundException e) { System.out.println("No file with that name"); } catch(IOException e) { System.out.println("IOException raised"); } } // END OF main( ) method } // END OF ReadData class
--------------------------------------------------------------------------------------------------------------
OUTPUT:
THANK YOU !! If you have any doubt please ask in comment section , I will modify the answer ( add EDITS ) . If you like my effort please upvote.