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
  • Java Programming Write a method named isEven that accepts an integer argument. The method should return...

    Java Programming Write a method named isEven that accepts an integer argument. The method should return true if the argument is even, or false if not. The program’s main method should use a loop to generate a random integer. The number of random integers is provided by a user, who must enter a number greater than 10. The loop should then invoke the isEven method to determine whether each integer is even or odd, display the random number and the...

  • Write a program that reads a file named input.txt and writes a file that contains the...

    Write a program that reads a file named input.txt and writes a file that contains the same contents, but is named output.txt. The input file will contain more than one line when I test this. Do not use a path name when opening these files. This means the files should be located in the top level folder of the project. Do not use a copy method that is supplied by Java. Your program must read the file line by line...

  • Write in C++ program Larger than n In a program, write a function that accepts three...

    Write in C++ program Larger than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n. Input from the keyboard: The filename and path of a list of integer numbers.                                           The number n to test the file numbers. Output to the console: The...

  • Write a method named RandomDoubles that takes a parameter N as its argument and generates N...

    Write a method named RandomDoubles that takes a parameter N as its argument and generates N random double values where each double value is a random double between 0.0 and 0.9999…. (i.e. simply use Math.random()) and save these double values into a file named doubles.txt.(java please) Next, write the following method that returns the number of digits in the fractional part of a double value. For example, 12.345 has 3 digits in the fractional part, 1.2345 has 4 digits in...

  • java Write a method named printEntireFile that prompts the user for a file name and points...

    java Write a method named printEntireFile that prompts the user for a file name and points the contents of that file to the console as output. You may assume that the file exists. For example. If the file example. txt contains the following input data: Then the following would be an example dialogue of your method:

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Write a C program, named sortit, that reads three integers from the command line argument and...

    Write a C program, named sortit, that reads three integers from the command line argument and returns the sorted list of the integers on the standard output screen, For instance, >sortit 3 5 7 >The sorted list is 3, 5, 7. >sortit 4 0 9 >The sorted list is 0, 4, 9 >sortit 1 p 9 >sortit: invalid input p. >sortit >usage: sortit num1 num2 num3! 1. The source code of a c file 2. Screenshots that show successful execution...

  • Java program called countDigits– use input file named countDigits.txt Read in a number and a digit....

    Java program called countDigits– use input file named countDigits.txt Read in a number and a digit. Count the number of times you find the digit in the number. Example: Sent to the method: 10203040586970 0 Returned the number of times it finds a match: 5

  • Write a Java code to define an interface called FrugalList which contains a method named pushBack....

    Write a Java code to define an interface called FrugalList which contains a method named pushBack. The method pushBack takes an Object type argument and returns a boolean value.

  • 1. use c++ write a c++ program that reads 10 integers from a file named input.txt...

    1. use c++ write a c++ program that reads 10 integers from a file named input.txt in this file the 10 integers wil be on a single line with space between the. calculate the average of these numbers and then write his number to a file named output.txt. if the program is unable to successfully complete the file operations then display eero message to the console.

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