Write an application that will search a textdata file for th

Write an application that will search a text/data file for the largest and smallest integer values. A try and catch block is required to be employed in your program. This text/data file contains both integer and non-integer data. the file is named Lab6Data.txt and contain the following data ww tt pp l8 l5 85 69 81 tt 97 64 90 77 kk 34 79 3r 98 -50 138 71 88 ff 67 83 86 94 yy 55 ll Have the Finch respond to the input read from the data file. Finch announces the first integer found. When a non-integer is found the Finch will say so, its beak will turn red for 2 seconds. When an integer is found, the program will determine if the integer is the smallest or largest value read to date. If the data is the smallest value, the Finch will say so speaking the value along with the beak turns blue for 2 seconds. And if the data read is the largest value, Finch will say so speaking the value along with the beak turns yellow for 2 seconds. The Finch will announce the largest and smallest value found after all data from the file is read. Assume the data file will change and any size integer value will be stored in the data file.

Solution

Please find the required solution:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class SearchFile {

public static void main( String[] args ) throws IOException
{

File file = new File(\"Lab6Data.txt\");
//let first found variable is used find the first integer value
Integer firstfound = -1;

//let first found variable is used store the small value
Integer smallValue = 0;

//let first found variable is used store the large value
Integer largeValue = 0;

//BufferedReader used to read file
BufferedReader reader = new BufferedReader(new FileReader(file));

//Iterate through file
for( String line = reader.readLine(); line != null; line = reader.readLine() )
{
String[] values = line.split(\"\\\\s+\");
for( String value : values )
{
try
{
Integer num = Integer.parseInt(value);
if( firstfound == -1 )
{
System.out.println(num + \" the first integer found.\");
firstfound = 0;
smallValue = num;
largeValue = num;
}
if( smallValue > num )
{
//for the small value encounterd print message and replace value
System.out
.println(num + \" so speaking the value along with the beak turns blue for 2 seconds.\");
smallValue = num;
}
else if( largeValue < num )
{
//for the large value encounterd print message and replace value
System.out
.println(num + \"so speaking the value along with the beak turns yellow for 2 seconds\");
largeValue = num;
}
}
catch( NumberFormatException nfe )
{
//for evry non integer value print the msg
System.out.println(value + \" so, its beak will turn red for 2 seconds.\");
}
if( firstfound == 0 )
{
firstfound = 1;
}
}
}

//finally close the reader
reader.close();
}
}

Lab6Data.txt

ww tt pp l8 l5 85 69 81 tt 97 64 90 77 kk 34 79 3r 98 -50 138 71 88 ff 67 83 86 94 yy 55 ll

Sample output:

ww so, its beak will turn red for 2 seconds.
tt so, its beak will turn red for 2 seconds.
pp so, its beak will turn red for 2 seconds.
l8 so, its beak will turn red for 2 seconds.
l5 so, its beak will turn red for 2 seconds.
85 the first integer found.
69 so speaking the value along with the beak turns blue for 2 seconds.
tt so, its beak will turn red for 2 seconds.
97so speaking the value along with the beak turns yellow for 2 seconds
64 so speaking the value along with the beak turns blue for 2 seconds.
kk so, its beak will turn red for 2 seconds.
34 so speaking the value along with the beak turns blue for 2 seconds.
3r so, its beak will turn red for 2 seconds.
98so speaking the value along with the beak turns yellow for 2 seconds
-50 so speaking the value along with the beak turns blue for 2 seconds.
138so speaking the value along with the beak turns yellow for 2 seconds
ff so, its beak will turn red for 2 seconds.
yy so, its beak will turn red for 2 seconds.
ll so, its beak will turn red for 2 seconds.

Write an application that will search a text/data file for the largest and smallest integer values. A try and catch block is required to be employed in your pro
Write an application that will search a text/data file for the largest and smallest integer values. A try and catch block is required to be employed in your pro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site