Your task is to write a program that uses a textfile containing any number of li
ID: 3848258 • Letter: Y
Question
Your task is to write a program that uses a textfile containing any number of lines of integer values as input. Each line in the file is supposed to have no more than 15 values in it. If the file exists and each line in the file does in fact contain 15 or fewer integers, then the program processes and reports on each line in turn by displaying its line number, the number of values on that line, and the range of values on the line.
But, as you know, things are not always as they seem, or as they are supposed to be. For example, the file your program is looking for may not be there, there may be no integers at all on some lines, some lines may contain more values than the allowed maximum of 15, and some lines may contain
"values" that aren't even integers (i.e., badly formatted "invalid" integer values). The program must recognize and respond appropriately to each of these problems.
Note: You must implement exception handling. Not only am I requiring it, but Java itself will also require it since you will have to use Java resources that force you to deal with the IOException. In addition to this exception, you are also required to handle the ArrayIndexOutOfBounds and the NumberFormatException at appropriate points in your program.
Your program should read in, from the command line, the name of a text file containing any number of lines of integers and then process each line in turn. Processing a line means computing and then reporting, for that line, its line number, along with the range of values on the line.
The program should recognize a number of problems that may occur when trying to process such a file. First, the file itself may not be present. If so, the following message is displayed:
filename.txt (the system cannot find the file specified). Program will now terminate.
If the file exists, and contains lines of integers, possibly with blank lines, possibly lines with too many values, and possibly lines with invalid integers on them, then the following messages are output, as appropriate:
Line #: Number of values = # and value range = [#, #]. Line #: This is a blank line. Line #: This line contains more than the maximum of 15 allowed data values. Line #: This line contains an invalid integer value.
For example, if a file named “text.txt” contains the following text:
1 2 30
3 4 5
2 3 4 5 6.5 7
abs sej jdh& 4
1 2 3 4 5 6 6 6 8 99 0 0 -1
34 34 23
24
1 2 3 4 5 6 7 8 9 10 -2 3 4 5 6 7
1
Then the output should be like this: Output:
The file test.txt was successfully opened. The data in it will now be processed. Press Enter to continue ...
Line 1:Number of values = 3 and value range is [1, 30].
Line 2:This is a blank line. Line 3:Number of values = 3 and value range is [3, 5].
Line 4:This line contains an invalid integer value.
Line 5:This line contains an invalid integer value.
Line 6:Number of values = 13 and value range is [-1, 99].
Line 7:Number of values = 3 and value range is [23, 34].
Line 8:Number of values = 1 and value range value range consists of this single value.
Line 9:This line contains more than the maximum of 15 allowed data values. Line 10:Number of values = 1 and value range value range consists of this single value.
Explanation / Answer
CountInteger.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CountInteger {
private boolean check(String s, int j){
String[] temp = s.split(" ");
boolean flag = false;
for(int i=0 ; i<temp.length ; i++){
if(temp[i] == " "){
System.out.println("Line " + (j+1) + "This is a blank line.");
break;
}
try {
Integer.parseInt(temp[i]);
flag = true;
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
flag = false;
break;
}
}
return flag;
}
private int getCount(String s){
int count = 0;
String[] temp = s.split(" ");
count = temp.length;
return count;
}
private void countInt(){
String filename = "test.txt";
String filepath = "D:/Demos/" + filename;
try {
List<String> records = new ArrayList<String>();
boolean flag = false;
boolean flag1 = false;
BufferedReader reader;
reader = new BufferedReader(new FileReader(filepath));
String line;
int number,min,max;
while ((line = reader.readLine()) != null)
{
records.add(line);
}
reader.close();
for(int i=0 ; i<records.size() ; i++){
flag = check(records.get(i), i);
if(flag){
number = getCount(records.get(i));
if(number > 15){
System.out.println("Line " + (i+1) + "This line contains more than the maximum of 15 allowed data values.");
}else if(number == 1){
System.out.println("Line " + (i+1) + ":" + "Number of values = " + number + " and value range value range consists of this single value.");
}else{
min = getMin(records.get(i));
max = getMax(records.get(i));
System.out.println("Line " + (i+1) + ":" + "Number of values = " + number + " and range is [" + min + ", " + max + "]");
}
}
else{
String[] temp = records.get(i).split(" ");
for(int j=0 ; j<temp.length ; j++){
if(temp[j].equalsIgnoreCase("")){
flag1 = true;
break;
}
else
flag1 = false;
}
if(flag1)
System.out.println("Line " + (i+1) + "This is a blank line.");
else
System.out.println("Line " + (i+1) + "This line contains an invalid integer value.");
}
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println(filename + " the system cannot find the file specified. Program will now terminate.");
// e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private int getMax(String string) {
// TODO Auto-generated method stub
String[] temp = string.split(" ");
int max = 0;
int val = 0;
for(int i=0 ; i<temp.length ; i++){
val = Integer.parseInt(temp[i]);
if(max < val)
max = val;
}
return max;
}
private int getMin(String string) {
// TODO Auto-generated method stub
String[] temp = string.split(" ");
int min = Integer.parseInt(temp[0]);
int val = 0;
for(int i=1 ; i<temp.length ; i++){
val = Integer.parseInt(temp[i]);
if(min > val)
min = val;
}
return min;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CountInteger ci = new CountInteger();
ci.countInt();
}
}
test.txt
1 2 30
3 4 5
2 3 4 5 6.5 7
abs sej jdh& 4
1 2 3 4 5 6 6 6 8 99 0 0 -1
34 34 23
24
1 2 3 4 5 6 7 8 9 10 -2 3 4 5 6 7
1
Copy the test.txt at any location and change the path in filepath variable
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.