Are there any errors or suggestions to improve this code: import java.io.FileRea
ID: 3834678 • Letter: A
Question
Are there any errors or suggestions to improve this code: import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; import java.util.Scanner; public class DataRead { public static void main(String[] args) throws IOException { //setting default values for BufferedReader BufferedReader inputStream = null; //Creating object for the file String fileName = "Number.dat"; //Creating try and catch try { String line; int countInt = 0; inputStream = new BufferedReader(new FileReader(fileName)); //While loop to read the file contents line by line until it has //gone all the way through, and keeps track of number of lines read while ((line = inputStream.readLine()) != null) { System.out.println(line); countInt ++; } //Displays the total amount of integers in text System.out.println("There were " + countInt + " integers in this file"); } //Creating to assist with any exceptions catch (IOException ex) { ex.printStackTrace(); } } }
Explanation / Answer
UPDATED CODE(changes are in Bold Text)(Find Suggestions below code)
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Scanner;
class DataRead {
public static void main(String[] args) throws IOException {
// setting default values for BufferedReader
BufferedReader inputStream = null;
// Creating object for the file
String fileName = "Number.dat";
// Creating try and catch
try {
String line;
int countInt = 0;
int no_of_Integers=0;
inputStream = new BufferedReader(new FileReader(fileName));
// While loop to read the file contents line by line until it has
// //gone all the way through, and keeps track of number of lines
// read
while ((line = inputStream.readLine()) != null) {
System.out.println(line);
countInt++;
try{
Integer.parseInt(line);
}catch(NumberFormatException e){
continue;
}
no_of_Integers++;
}
// Displays the total amount of integers in text
System.out.println("There were " + no_of_Integers + " integers in this file");
}
// Creating to assist with any exceptions
catch (IOException ex) {
ex.printStackTrace();
}
}
}
SUGGESTIONS :
lets say if your Number.dat has some data as below -
CHEGG_MEMBER1
CHEGG_MEMBER2
CHEGG_MEMBER3
12
23
45
So here in your Number.dat file u have total 6 lines with above data - first three lines contains NO INTEGERS and rest all have only INTEGERS
Now, lets understand updated code -
Code reads each line and try to parse that line to an Integer So, here If you have only INTEGERS in your line , only then it can be parsed to INTEGER... if not it will throw an NUMBERFORMATEXCEPTION..So,Code checks if a particular line contains INTEGER or NOT , if not Code iterates through next line ... if contains only INTEGER Code increments a variable (no_of_Integers)....At last Code prints no of lines with Integers
So,for the example file above output would be -
CHEGG_MEMBER1
CHEGG_MEMBER2
CHEGG_MEMBER3
12
23
45
There were 3 integers in this file
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.