SlideShare a Scribd company logo
1 of 2
Download to read offline
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 list = new ArrayList();
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

More Related Content

Similar to Write a program in Java that reads a set of doubles from a file, sto.pdf

import java.io.BufferedReader;import java.io.File;import java.io.pdf
import java.io.BufferedReader;import java.io.File;import java.io.pdfimport java.io.BufferedReader;import java.io.File;import java.io.pdf
import java.io.BufferedReader;import java.io.File;import java.io.pdf
manojmozy
 
Here is my code for a linefile editor import java.io.BufferedRea.pdf
Here is my code for a linefile editor import java.io.BufferedRea.pdfHere is my code for a linefile editor import java.io.BufferedRea.pdf
Here is my code for a linefile editor import java.io.BufferedRea.pdf
pratyushraj61
 
Create a text file named lnfo.txt. Enter the following informatio.pdf
Create a text file named lnfo.txt. Enter the following informatio.pdfCreate a text file named lnfo.txt. Enter the following informatio.pdf
Create a text file named lnfo.txt. Enter the following informatio.pdf
shahidqamar17
 
Write a java program that allows the user to input the name of a file.docx
Write a java program that allows the user to input  the name of a file.docxWrite a java program that allows the user to input  the name of a file.docx
Write a java program that allows the user to input the name of a file.docx
noreendchesterton753
 
Write a Java program that opens the file, reads all the Grade from t.pdf
Write a Java program that opens the file, reads all the Grade from t.pdfWrite a Java program that opens the file, reads all the Grade from t.pdf
Write a Java program that opens the file, reads all the Grade from t.pdf
fashiionbeutycare
 
Please help me with a UML class diagram for the following code im.pdf
Please help me with a UML class diagram for the following code im.pdfPlease help me with a UML class diagram for the following code im.pdf
Please help me with a UML class diagram for the following code im.pdf
aioils
 
import java.io.BufferedReader;import java.io.BufferedWriter;.docx
import java.io.BufferedReader;import java.io.BufferedWriter;.docximport java.io.BufferedReader;import java.io.BufferedWriter;.docx
import java.io.BufferedReader;import java.io.BufferedWriter;.docx
wilcockiris
 
In java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdfIn java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdf
aromalcom
 
I can not get my code to comply it gets mad that I have run declare mo.docx
I can not get my code to comply it gets mad that I have run declare mo.docxI can not get my code to comply it gets mad that I have run declare mo.docx
I can not get my code to comply it gets mad that I have run declare mo.docx
hamblymarta
 
Write a java program that compute the frequency of each word in the fi.docx
Write a java program that compute the frequency of each word in the fi.docxWrite a java program that compute the frequency of each word in the fi.docx
Write a java program that compute the frequency of each word in the fi.docx
noreendchesterton753
 
JAVAWrite a program that takes a command-line argument representin.pdf
JAVAWrite a program that takes a command-line argument representin.pdfJAVAWrite a program that takes a command-line argument representin.pdf
JAVAWrite a program that takes a command-line argument representin.pdf
jibinsh
 

Similar to Write a program in Java that reads a set of doubles from a file, sto.pdf (20)

import java.io.BufferedReader;import java.io.File;import java.io.pdf
import java.io.BufferedReader;import java.io.File;import java.io.pdfimport java.io.BufferedReader;import java.io.File;import java.io.pdf
import java.io.BufferedReader;import java.io.File;import java.io.pdf
 
Write a program that will count the number of characters- words- and l.docx
Write a program that will count the number of characters- words- and l.docxWrite a program that will count the number of characters- words- and l.docx
Write a program that will count the number of characters- words- and l.docx
 
Here is my code for a linefile editor import java.io.BufferedRea.pdf
Here is my code for a linefile editor import java.io.BufferedRea.pdfHere is my code for a linefile editor import java.io.BufferedRea.pdf
Here is my code for a linefile editor import java.io.BufferedRea.pdf
 
Create a text file named lnfo.txt. Enter the following informatio.pdf
Create a text file named lnfo.txt. Enter the following informatio.pdfCreate a text file named lnfo.txt. Enter the following informatio.pdf
Create a text file named lnfo.txt. Enter the following informatio.pdf
 
Write a java program that allows the user to input the name of a fil.docx
 Write a java program that allows the user to input  the name of a fil.docx Write a java program that allows the user to input  the name of a fil.docx
Write a java program that allows the user to input the name of a fil.docx
 
Write a java program that allows the user to input the name of a file.docx
Write a java program that allows the user to input  the name of a file.docxWrite a java program that allows the user to input  the name of a file.docx
Write a java program that allows the user to input the name of a file.docx
 
Lab4
Lab4Lab4
Lab4
 
Write a Java program that opens the file, reads all the Grade from t.pdf
Write a Java program that opens the file, reads all the Grade from t.pdfWrite a Java program that opens the file, reads all the Grade from t.pdf
Write a Java program that opens the file, reads all the Grade from t.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
Please help me with a UML class diagram for the following code im.pdf
Please help me with a UML class diagram for the following code im.pdfPlease help me with a UML class diagram for the following code im.pdf
Please help me with a UML class diagram for the following code im.pdf
 
import java.io.BufferedReader;import java.io.BufferedWriter;.docx
import java.io.BufferedReader;import java.io.BufferedWriter;.docximport java.io.BufferedReader;import java.io.BufferedWriter;.docx
import java.io.BufferedReader;import java.io.BufferedWriter;.docx
 
Complete the following Java code such that it opens a file named Fruit.docx
Complete the following Java code such that it opens a file named Fruit.docxComplete the following Java code such that it opens a file named Fruit.docx
Complete the following Java code such that it opens a file named Fruit.docx
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
In java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdfIn java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdf
 
I can not get my code to comply it gets mad that I have run declare mo.docx
I can not get my code to comply it gets mad that I have run declare mo.docxI can not get my code to comply it gets mad that I have run declare mo.docx
I can not get my code to comply it gets mad that I have run declare mo.docx
 
Java ppt
Java pptJava ppt
Java ppt
 
Write a java program that compute the frequency of each word in the fi.docx
Write a java program that compute the frequency of each word in the fi.docxWrite a java program that compute the frequency of each word in the fi.docx
Write a java program that compute the frequency of each word in the fi.docx
 
JAVAWrite a program that takes a command-line argument representin.pdf
JAVAWrite a program that takes a command-line argument representin.pdfJAVAWrite a program that takes a command-line argument representin.pdf
JAVAWrite a program that takes a command-line argument representin.pdf
 
Lec2
Lec2Lec2
Lec2
 
srgoc
srgocsrgoc
srgoc
 

More from arihantpatna

Compare and contrast the advantages and disadvantages of arrays and .pdf
Compare and contrast the advantages and disadvantages of arrays and .pdfCompare and contrast the advantages and disadvantages of arrays and .pdf
Compare and contrast the advantages and disadvantages of arrays and .pdf
arihantpatna
 
Answer and describe the following five plants habit, habitat, life .pdf
Answer and describe the following five plants habit, habitat, life .pdfAnswer and describe the following five plants habit, habitat, life .pdf
Answer and describe the following five plants habit, habitat, life .pdf
arihantpatna
 
Whats wrong with this Hardy Weinberg question I thought it was a.pdf
Whats wrong with this Hardy Weinberg question I thought it was a.pdfWhats wrong with this Hardy Weinberg question I thought it was a.pdf
Whats wrong with this Hardy Weinberg question I thought it was a.pdf
arihantpatna
 
6. List and define the underlying conditions, or assumptions, which .pdf
6. List and define the underlying conditions, or assumptions, which .pdf6. List and define the underlying conditions, or assumptions, which .pdf
6. List and define the underlying conditions, or assumptions, which .pdf
arihantpatna
 
Wha is the difference between the large intestine and colon .pdf
Wha is the difference between the large intestine and colon .pdfWha is the difference between the large intestine and colon .pdf
Wha is the difference between the large intestine and colon .pdf
arihantpatna
 
Please Explain CompletelyWhat defines the beginning of the hepatic.pdf
Please Explain CompletelyWhat defines the beginning of the hepatic.pdfPlease Explain CompletelyWhat defines the beginning of the hepatic.pdf
Please Explain CompletelyWhat defines the beginning of the hepatic.pdf
arihantpatna
 
17) Fill out the following table Structure Pili or Fimbriae Flagella.pdf
17) Fill out the following table Structure Pili or Fimbriae Flagella.pdf17) Fill out the following table Structure Pili or Fimbriae Flagella.pdf
17) Fill out the following table Structure Pili or Fimbriae Flagella.pdf
arihantpatna
 

More from arihantpatna (20)

Compare and contrast the advantages and disadvantages of arrays and .pdf
Compare and contrast the advantages and disadvantages of arrays and .pdfCompare and contrast the advantages and disadvantages of arrays and .pdf
Compare and contrast the advantages and disadvantages of arrays and .pdf
 
Answer and describe the following five plants habit, habitat, life .pdf
Answer and describe the following five plants habit, habitat, life .pdfAnswer and describe the following five plants habit, habitat, life .pdf
Answer and describe the following five plants habit, habitat, life .pdf
 
A plant has the genotype (Aa; Bb; Cc). How many different genotype.pdf
A plant has the genotype (Aa; Bb; Cc). How many different genotype.pdfA plant has the genotype (Aa; Bb; Cc). How many different genotype.pdf
A plant has the genotype (Aa; Bb; Cc). How many different genotype.pdf
 
A) Explain how bilateral symmetry contributed to motion and cephaliz.pdf
A) Explain how bilateral symmetry contributed to motion and cephaliz.pdfA) Explain how bilateral symmetry contributed to motion and cephaliz.pdf
A) Explain how bilateral symmetry contributed to motion and cephaliz.pdf
 
_______ are any substances that when introduced into the body, stimul.pdf
_______ are any substances that when introduced into the body, stimul.pdf_______ are any substances that when introduced into the body, stimul.pdf
_______ are any substances that when introduced into the body, stimul.pdf
 
You decide to spend Break hiking through the Rockies upon arrival, yo.pdf
You decide to spend Break hiking through the Rockies upon arrival, yo.pdfYou decide to spend Break hiking through the Rockies upon arrival, yo.pdf
You decide to spend Break hiking through the Rockies upon arrival, yo.pdf
 
Which of the following statements is true about cultureA. Culture.pdf
Which of the following statements is true about cultureA. Culture.pdfWhich of the following statements is true about cultureA. Culture.pdf
Which of the following statements is true about cultureA. Culture.pdf
 
Which data set has the least sample standard deviation Data set (ii.pdf
Which data set has the least sample standard deviation  Data set (ii.pdfWhich data set has the least sample standard deviation  Data set (ii.pdf
Which data set has the least sample standard deviation Data set (ii.pdf
 
Whats wrong with this Hardy Weinberg question I thought it was a.pdf
Whats wrong with this Hardy Weinberg question I thought it was a.pdfWhats wrong with this Hardy Weinberg question I thought it was a.pdf
Whats wrong with this Hardy Weinberg question I thought it was a.pdf
 
6. List and define the underlying conditions, or assumptions, which .pdf
6. List and define the underlying conditions, or assumptions, which .pdf6. List and define the underlying conditions, or assumptions, which .pdf
6. List and define the underlying conditions, or assumptions, which .pdf
 
What does the LP tableau below indicate (The xs are decision varia.pdf
What does the LP tableau below indicate (The xs are decision varia.pdfWhat does the LP tableau below indicate (The xs are decision varia.pdf
What does the LP tableau below indicate (The xs are decision varia.pdf
 
Wha is the difference between the large intestine and colon .pdf
Wha is the difference between the large intestine and colon .pdfWha is the difference between the large intestine and colon .pdf
Wha is the difference between the large intestine and colon .pdf
 
Was airpower decisive in winning World War II Defend your answer..pdf
Was airpower decisive in winning World War II Defend your answer..pdfWas airpower decisive in winning World War II Defend your answer..pdf
Was airpower decisive in winning World War II Defend your answer..pdf
 
Summarize the workflow of the study of microbial diversity. Draw a d.pdf
Summarize the workflow of the study of microbial diversity. Draw a d.pdfSummarize the workflow of the study of microbial diversity. Draw a d.pdf
Summarize the workflow of the study of microbial diversity. Draw a d.pdf
 
Shifting allocations most often arise as a result of which of the fo.pdf
Shifting allocations most often arise as a result of which of the fo.pdfShifting allocations most often arise as a result of which of the fo.pdf
Shifting allocations most often arise as a result of which of the fo.pdf
 
16.) How would you determine if Cellulous is hydrophobic or hydrophi.pdf
16.) How would you determine if Cellulous is hydrophobic or hydrophi.pdf16.) How would you determine if Cellulous is hydrophobic or hydrophi.pdf
16.) How would you determine if Cellulous is hydrophobic or hydrophi.pdf
 
Please Explain CompletelyWhat defines the beginning of the hepatic.pdf
Please Explain CompletelyWhat defines the beginning of the hepatic.pdfPlease Explain CompletelyWhat defines the beginning of the hepatic.pdf
Please Explain CompletelyWhat defines the beginning of the hepatic.pdf
 
Plant Diversity II –     Seed Plants1. Explain how the rise in pro.pdf
Plant Diversity II –     Seed Plants1. Explain how the rise in pro.pdfPlant Diversity II –     Seed Plants1. Explain how the rise in pro.pdf
Plant Diversity II –     Seed Plants1. Explain how the rise in pro.pdf
 
17) Fill out the following table Structure Pili or Fimbriae Flagella.pdf
17) Fill out the following table Structure Pili or Fimbriae Flagella.pdf17) Fill out the following table Structure Pili or Fimbriae Flagella.pdf
17) Fill out the following table Structure Pili or Fimbriae Flagella.pdf
 
Mark and I.isa registered for Physics 101 class. Mark attends class 6.pdf
Mark and I.isa registered for Physics 101 class. Mark attends class 6.pdfMark and I.isa registered for Physics 101 class. Mark attends class 6.pdf
Mark and I.isa registered for Physics 101 class. Mark attends class 6.pdf
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 

Recently uploaded (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 

Write a program in Java that reads a set of doubles from a file, sto.pdf

  • 1. 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 list = new ArrayList(); 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));
  • 2. } } else{ System.out.println("Input file does not exist"); } } } Output: The numbers in reverse order: 10.1 98.3 45.6 27.3