SlideShare a Scribd company logo
1 of 3
Download to read offline
Write a JAVA program that reads a stream of integers from a file and prints to the screen the
range of integers in the file (i.e. [lowest, highest]). You should first prompt the user to provide
the file name. You should then read all the integers from the file, keeping track of the lowest and
highest values seen in the entire file, and only print out the range of values after the entire file
has been read. Importantly, unlike the previous problem, you can make no assumptions about the
contents of the file. If your program cannot read the file, opens an empty file, or encounters a
non- integer while reading the file, your program should output that the file is invalid.
You have been supplied JUnit tests for several invalid files (e.g. non-existent, empty, non-
integer) as well as files that contain only integer values.
Solution
ReadFileNumbers.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFileNumbers {
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the file name: ");
String fileName = scan.next();
File file = new File(fileName);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
if(file.exists()){
try{
Scanner scan1 = new Scanner(file);
if(scan1.hasNext()){
while(scan1.hasNext()){
int n= scan1.nextInt();
if(min > n){
min = n;
}
if(max < n){
max = n;
}
}
System.out.println("highest number is "+max);
System.out.println("lowest number is "+min);
}
else{
System.out.println("File is an empty");
}
}
catch(Exception e){
System.out.println("File is invalid. File has a a non-integers.");
}
}
else{
System.out.println("File does not exist");
}
}
}
Output:
Enter the file name: D: umbers.txt
highest number is 9
lowest number is -17
numbers.txt
3
4
2
-1
1
5
7
-11
-12
-17
6
9
8

More Related Content

Similar to Write a JAVA program that reads a stream of integers from a file and.pdf

Need help with this java code. fill in lines where needed. Also, not.pdf
Need help with this java code. fill in lines where needed. Also, not.pdfNeed help with this java code. fill in lines where needed. Also, not.pdf
Need help with this java code. fill in lines where needed. Also, not.pdf
rishabjain5053
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
C++  - UNIT_-_V.pptx which contains details about File ConceptsC++  - UNIT_-_V.pptx which contains details about File Concepts
C++ - UNIT_-_V.pptx which contains details about File Concepts
ANUSUYA S
 
Use Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdfUse Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdf
f3apparelsonline
 
Change the code in Writer.java only to get it working. Must contain .pdf
Change the code in Writer.java only to get it working. Must contain .pdfChange the code in Writer.java only to get it working. Must contain .pdf
Change the code in Writer.java only to get it working. Must contain .pdf
secunderbadtirumalgi
 
CS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docx
CS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docxCS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docx
CS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docx
annettsparrow
 
Description 1) Create a Lab2 folder for this project2.docx
Description       1)  Create a Lab2 folder for this project2.docxDescription       1)  Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docx
theodorelove43763
 

Similar to Write a JAVA program that reads a stream of integers from a file and.pdf (20)

UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
Need help with this java code. fill in lines where needed. Also, not.pdf
Need help with this java code. fill in lines where needed. Also, not.pdfNeed help with this java code. fill in lines where needed. Also, not.pdf
Need help with this java code. fill in lines where needed. Also, not.pdf
 
JAVA Q2- Write a program that reads strings from the user and writes t.docx
JAVA Q2- Write a program that reads strings from the user and writes t.docxJAVA Q2- Write a program that reads strings from the user and writes t.docx
JAVA Q2- Write a program that reads strings from the user and writes t.docx
 
Python-files
Python-filesPython-files
Python-files
 
Hi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdfHi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdf
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
C++  - UNIT_-_V.pptx which contains details about File ConceptsC++  - UNIT_-_V.pptx which contains details about File Concepts
C++ - UNIT_-_V.pptx which contains details about File Concepts
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Use Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdfUse Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdf
 
Qtp launch
Qtp launchQtp launch
Qtp launch
 
Change the code in Writer.java only to get it working. Must contain .pdf
Change the code in Writer.java only to get it working. Must contain .pdfChange the code in Writer.java only to get it working. Must contain .pdf
Change the code in Writer.java only to get it working. Must contain .pdf
 
Write a program that asks the user for the name of a file. The progr.pdf
Write a program that asks the user for the name of a file. The progr.pdfWrite a program that asks the user for the name of a file. The progr.pdf
Write a program that asks the user for the name of a file. The progr.pdf
 
CS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docx
CS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docxCS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docx
CS101S. ThompsonUniversity of BridgeportLab 7 Files, File.docx
 
Python Lecture 9
Python Lecture 9Python Lecture 9
Python Lecture 9
 
Description 1) Create a Lab2 folder for this project2.docx
Description       1)  Create a Lab2 folder for this project2.docxDescription       1)  Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docx
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing files
 
Java 3 Computer Science.pptx
Java 3 Computer Science.pptxJava 3 Computer Science.pptx
Java 3 Computer Science.pptx
 
IO and threads Java
IO and threads JavaIO and threads Java
IO and threads Java
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
 
Filesin c++
Filesin c++Filesin c++
Filesin c++
 
Python-FileHandling.pptx
Python-FileHandling.pptxPython-FileHandling.pptx
Python-FileHandling.pptx
 

More from archanadesignfashion

What two difficulties arise in taking simplicity straightforwardly a.pdf
What two difficulties arise in taking simplicity straightforwardly a.pdfWhat two difficulties arise in taking simplicity straightforwardly a.pdf
What two difficulties arise in taking simplicity straightforwardly a.pdf
archanadesignfashion
 
What happens if the dividing zygote accidental separates into two A.pdf
What happens if the dividing zygote accidental separates into two  A.pdfWhat happens if the dividing zygote accidental separates into two  A.pdf
What happens if the dividing zygote accidental separates into two A.pdf
archanadesignfashion
 
What are stored in the Autodesk Inventor History Tree When extrudin.pdf
What are stored in the Autodesk Inventor History Tree  When extrudin.pdfWhat are stored in the Autodesk Inventor History Tree  When extrudin.pdf
What are stored in the Autodesk Inventor History Tree When extrudin.pdf
archanadesignfashion
 
Alan Homes serves on the board of directors of Flynn Company. The pr.pdf
Alan Homes serves on the board of directors of Flynn Company. The pr.pdfAlan Homes serves on the board of directors of Flynn Company. The pr.pdf
Alan Homes serves on the board of directors of Flynn Company. The pr.pdf
archanadesignfashion
 
q2.4) Listed are elements of the financial statements discussed in t.pdf
q2.4) Listed are elements of the financial statements discussed in t.pdfq2.4) Listed are elements of the financial statements discussed in t.pdf
q2.4) Listed are elements of the financial statements discussed in t.pdf
archanadesignfashion
 
Programming code in C must be C loops reque.pdf
Programming code in C  must be C loops reque.pdfProgramming code in C  must be C loops reque.pdf
Programming code in C must be C loops reque.pdf
archanadesignfashion
 
Please help! Both answers must be in long paragraph form1. Explain.pdf
Please help! Both answers must be in long paragraph form1. Explain.pdfPlease help! Both answers must be in long paragraph form1. Explain.pdf
Please help! Both answers must be in long paragraph form1. Explain.pdf
archanadesignfashion
 
If the Social Security retirement system was a private retirement sy.pdf
If the Social Security retirement system was a private retirement sy.pdfIf the Social Security retirement system was a private retirement sy.pdf
If the Social Security retirement system was a private retirement sy.pdf
archanadesignfashion
 
I need answer to those questions please ASAP Im stuck D Question 1.pdf
I need answer to those questions please ASAP Im stuck D Question 1.pdfI need answer to those questions please ASAP Im stuck D Question 1.pdf
I need answer to those questions please ASAP Im stuck D Question 1.pdf
archanadesignfashion
 

More from archanadesignfashion (20)

Blood typing is often used as evidence in paternity cases in court. .pdf
Blood typing is often used as evidence in paternity cases in court. .pdfBlood typing is often used as evidence in paternity cases in court. .pdf
Blood typing is often used as evidence in paternity cases in court. .pdf
 
Write a very simple Note taking using JavaFX ( adding three function.pdf
Write a very simple Note taking using JavaFX ( adding three function.pdfWrite a very simple Note taking using JavaFX ( adding three function.pdf
Write a very simple Note taking using JavaFX ( adding three function.pdf
 
What two difficulties arise in taking simplicity straightforwardly a.pdf
What two difficulties arise in taking simplicity straightforwardly a.pdfWhat two difficulties arise in taking simplicity straightforwardly a.pdf
What two difficulties arise in taking simplicity straightforwardly a.pdf
 
what is true about assetsSolutionAssets are those which are u.pdf
what is true about assetsSolutionAssets are those which are u.pdfwhat is true about assetsSolutionAssets are those which are u.pdf
what is true about assetsSolutionAssets are those which are u.pdf
 
What happens if the dividing zygote accidental separates into two A.pdf
What happens if the dividing zygote accidental separates into two  A.pdfWhat happens if the dividing zygote accidental separates into two  A.pdf
What happens if the dividing zygote accidental separates into two A.pdf
 
What are stored in the Autodesk Inventor History Tree When extrudin.pdf
What are stored in the Autodesk Inventor History Tree  When extrudin.pdfWhat are stored in the Autodesk Inventor History Tree  When extrudin.pdf
What are stored in the Autodesk Inventor History Tree When extrudin.pdf
 
What are three factors contributing to resistanceCharacterize car.pdf
What are three factors contributing to resistanceCharacterize car.pdfWhat are three factors contributing to resistanceCharacterize car.pdf
What are three factors contributing to resistanceCharacterize car.pdf
 
Alan Homes serves on the board of directors of Flynn Company. The pr.pdf
Alan Homes serves on the board of directors of Flynn Company. The pr.pdfAlan Homes serves on the board of directors of Flynn Company. The pr.pdf
Alan Homes serves on the board of directors of Flynn Company. The pr.pdf
 
Two genes (A and B) are located 10cM apart on the X chromosome. A wom.pdf
Two genes (A and B) are located 10cM apart on the X chromosome. A wom.pdfTwo genes (A and B) are located 10cM apart on the X chromosome. A wom.pdf
Two genes (A and B) are located 10cM apart on the X chromosome. A wom.pdf
 
q2.4) Listed are elements of the financial statements discussed in t.pdf
q2.4) Listed are elements of the financial statements discussed in t.pdfq2.4) Listed are elements of the financial statements discussed in t.pdf
q2.4) Listed are elements of the financial statements discussed in t.pdf
 
Programming code in C must be C loops reque.pdf
Programming code in C  must be C loops reque.pdfProgramming code in C  must be C loops reque.pdf
Programming code in C must be C loops reque.pdf
 
Please find the solution. Thanks 4. As the value of increases fro.pdf
Please find the solution. Thanks 4. As the value of increases fro.pdfPlease find the solution. Thanks 4. As the value of increases fro.pdf
Please find the solution. Thanks 4. As the value of increases fro.pdf
 
Please help! Both answers must be in long paragraph form1. Explain.pdf
Please help! Both answers must be in long paragraph form1. Explain.pdfPlease help! Both answers must be in long paragraph form1. Explain.pdf
Please help! Both answers must be in long paragraph form1. Explain.pdf
 
Life is easy to recognize but difficult to define. According to .pdf
Life is easy to recognize but difficult to define. According to .pdfLife is easy to recognize but difficult to define. According to .pdf
Life is easy to recognize but difficult to define. According to .pdf
 
Let I, J be ideals in a ring R. Prove that I J is also an ideal of R.pdf
Let I, J be ideals in a ring R. Prove that I  J is also an ideal of R.pdfLet I, J be ideals in a ring R. Prove that I  J is also an ideal of R.pdf
Let I, J be ideals in a ring R. Prove that I J is also an ideal of R.pdf
 
Imagine that one of the segments in Drosophila is T2, which will for.pdf
Imagine that one of the segments in Drosophila is T2, which will for.pdfImagine that one of the segments in Drosophila is T2, which will for.pdf
Imagine that one of the segments in Drosophila is T2, which will for.pdf
 
If the Social Security retirement system was a private retirement sy.pdf
If the Social Security retirement system was a private retirement sy.pdfIf the Social Security retirement system was a private retirement sy.pdf
If the Social Security retirement system was a private retirement sy.pdf
 
Identify the following reaction Glucose + Fructose rightarrow Sucros.pdf
Identify the following reaction Glucose + Fructose rightarrow Sucros.pdfIdentify the following reaction Glucose + Fructose rightarrow Sucros.pdf
Identify the following reaction Glucose + Fructose rightarrow Sucros.pdf
 
identify the developmental genes that influence macroevolutionSo.pdf
identify the developmental genes that influence macroevolutionSo.pdfidentify the developmental genes that influence macroevolutionSo.pdf
identify the developmental genes that influence macroevolutionSo.pdf
 
I need answer to those questions please ASAP Im stuck D Question 1.pdf
I need answer to those questions please ASAP Im stuck D Question 1.pdfI need answer to those questions please ASAP Im stuck D Question 1.pdf
I need answer to those questions please ASAP Im stuck D Question 1.pdf
 

Recently uploaded

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 

Write a JAVA program that reads a stream of integers from a file and.pdf

  • 1. Write a JAVA program that reads a stream of integers from a file and prints to the screen the range of integers in the file (i.e. [lowest, highest]). You should first prompt the user to provide the file name. You should then read all the integers from the file, keeping track of the lowest and highest values seen in the entire file, and only print out the range of values after the entire file has been read. Importantly, unlike the previous problem, you can make no assumptions about the contents of the file. If your program cannot read the file, opens an empty file, or encounters a non- integer while reading the file, your program should output that the file is invalid. You have been supplied JUnit tests for several invalid files (e.g. non-existent, empty, non- integer) as well as files that contain only integer values. Solution ReadFileNumbers.java import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadFileNumbers { public static void main(String[] args) throws FileNotFoundException { Scanner scan = new Scanner(System.in); System.out.print("Enter the file name: "); String fileName = scan.next(); File file = new File(fileName); int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; if(file.exists()){ try{ Scanner scan1 = new Scanner(file); if(scan1.hasNext()){ while(scan1.hasNext()){ int n= scan1.nextInt(); if(min > n){ min = n; }
  • 2. if(max < n){ max = n; } } System.out.println("highest number is "+max); System.out.println("lowest number is "+min); } else{ System.out.println("File is an empty"); } } catch(Exception e){ System.out.println("File is invalid. File has a a non-integers."); } } else{ System.out.println("File does not exist"); } } } Output: Enter the file name: D: umbers.txt highest number is 9 lowest number is -17 numbers.txt 3 4 2 -1 1 5 7 -11 -12 -17 6
  • 3. 9 8