Question

JAVA Write a program that will search a text file of strings representing numbers of type...

JAVA Write a program that will search a text file of strings representing numbers of type int and will write the largest and the smallest numbers to the screen. Include appropriate error handling in case a line contains more than one int or it contains a String. Wrap all the work you are doing with the file in a try/catch/finally block with appropriate "catch" sections; the finally block will be where you close your file object (if desired, look this up online for more information regarding "finally" and when it is executed, specifically that it is executed in case of success or failure of the entire try block).

Homework Answers

Answer #1

/*Java program that reads an input file, data.txt file that contains data values. Then program read
* only valid data and then find the maximum and minimum values.The program prints the invalid
* data to console.
* */

//MAXMIN.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MAXMIN
{
   public static void main(String[] args)
   {
       //Set file name
       String fileName="data.txt";
       Scanner filereader=null;
       int num;
       int min;
       int max;
       try
       {
           filereader=new Scanner(new File(fileName));
           min=Integer.parseInt(filereader.nextLine());
           //assume starting value is max and min values
           max=min;
           /*Read file till end of file */
           while(filereader.hasNextLine())
           {
               String line=filereader.nextLine();
               if(line.contains(" "))
                   System.out.println("Invalid input : "+line);
               else if(line.matches("^[A-Za-z]+$"))
                   System.out.println("Invalid input : "+line);
               else
               {
                   num=Integer.parseInt(line);
                   if(num<min)
                       min=num;
                   if(num>max)
                       max=num;          
               }
           }

           //prinnt maximum and minimum values
           System.out.println("***MAX and MIN values***");
           System.out.println("Maximum value : "+max);
           System.out.println("Minimum value : "+min);
       }  
       //catch exceptioin if file not found
       catch(FileNotFoundException exp)
       {
           System.out.println(exp.getMessage());
       }
       //close the file in finally block
       finally
       {
           filereader.close();
       }
   }//end of main method
} //end of the class
-------------------------------------------data.txt file values---------------------------------------------------------------

5
6 7
one
1
5
10
99
0

------------------------------------------Sample Output#--------------------------------------------------------

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that takes two numbers from the Java console representing, respectively, an investment and...
Write a program that takes two numbers from the Java console representing, respectively, an investment and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate). Your program should calculate and output (in $ notation) the future value of the investment in 5, 10, and 20 years using the following formula: future value = investment * (1 + interest rate)year We will assume that the interest...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
Write a program of wordSearch puzzle that use the following text file as an input. The...
Write a program of wordSearch puzzle that use the following text file as an input. The output should be like this: PIXEL found (left) at (0,9). ( Use JAVA Array ) .Please do not use arrylist and the likes! Hints • The puzzle can be represented as a right-sized two-dimensional array of characters (char). • A String can be converted into a right-sized array of characters via the String method toCharArray. . A word can occur in any of 8...
**please write code with function definition taking in input and use given variable names** for e.g....
**please write code with function definition taking in input and use given variable names** for e.g. List matchNames(List inputNames, List secRecords) Java or Python Please Note:    * The function is expected to return a STRING_ARRAY.      * The function accepts following parameters:      *  1. STRING_ARRAY inputNames      *  2. STRING_ARRAY secRecords      */ Problem Statement Introduction Imagine you are helping the Security Exchange Commission (SEC) respond to anonymous tips. One of the biggest problems the team faces is handling the transcription of the companies reported...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called keys with other objects called values. It is implemented as a Java class that uses arrays internally. 1. Theory. A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps. You can test if...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT