Question

Write a Java program that contains a method named ReadAndFindMax. The argument to this method is...

Write a Java program that contains a method named ReadAndFindMax. The argument to this method is the filename (of String type). The method opens the file specified in the method argument filename and then reads integers from it. This file can contain any number of integers. Next, the method finds the maximum of these integers and returns it.

The main method must first ask the user to enter the filename; then call ReadAndFindMax method with the entered filename as an argument; and finally it must print the maximum integer value that is returned from this method.

You would have the screenshots similar to shown below (assuming that you are using console I/O):

Enter the file name: myfile.txt

The maximum number is 45

Assuming that the file myfile.txt contains the following four integer numbers:

23

34

45

2

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for you:

import java.util.*;
import java.io.*;
class ReadFileAndFindMaxInteger
{
    // The argument to this method is the filename (of String type). The method opens the
    // file specified in the method argument filename and then reads integers from it.
    // This file can contain any number of integers.
    // Next, the method finds the maximum of these integers and returns it.
    public static int ReadAndFindMax(String fileName) throws FileNotFoundException
    {
       Scanner sc = new Scanner(new File(fileName));
       int maxValue = sc.nextInt();
       while(sc.hasNext())
       {
          int value = sc.nextInt();
          if(value > maxValue)
              maxValue = value;  
       }
       return maxValue;
    }
   
    // The main method must first ask the user to enter the filename; then call
    // ReadAndFindMax method with the entered filename as an argument; and finally it must
    // print the maximum integer value that is returned from this method.
    public static void main(String[] args) throws FileNotFoundException
    {
       String fileName;
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the file name: ");
       fileName = sc.next();
       int max = ReadAndFindMax(fileName);
       System.out.println("The maximum number is " + max);
    }
}

And the output screenshot is:

Add a comment
Know the answer?
Add Answer to:
Write a Java program that contains a method named ReadAndFindMax. The argument to this method is...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT