Write a program in Java that reads a set of doubles from a f

Write a program in Java that reads a set of doubles from a file, stores them in an array or ArrayList, and then prints them back out to the console (using System.out.println statements) in REVERSE order.

For example, if the input file input.txt file contains

27.3

45.6

98.3

10.1

The console output will display

10.1

98.3

45.6

27.3

Solution

ReadAndDisplayReverse.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;


public class ReadAndDisplayReverse {

  
   public static void main(String[] args) throws FileNotFoundException {
       File inputFile = new File(\"D:\\\\input.txt\");
       if(inputFile.exists()){
           Scanner scan = new Scanner(inputFile);
           ArrayList<Double> list = new ArrayList<Double>();
           while(scan.hasNextDouble()){
               list.add(scan.nextDouble());
           }
           System.out.println(\"The numbers in reverse order: \");
           for(int i=list.size()-1; i>=0; i--){
               System.out.println(list.get(i));
           }
           }
       else{
           System.out.println(\"Input file does not exist\");
       }
   }

}

Output:

The numbers in reverse order:
10.1
98.3
45.6
27.3

Write a program in Java that reads a set of doubles from a file, stores them in an array or ArrayList, and then prints them back out to the console (using Syste
Write a program in Java that reads a set of doubles from a file, stores them in an array or ArrayList, and then prints them back out to the console (using Syste

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site