SlideShare a Scribd company logo
1 of 2
Download to read offline
In Java:
Write a program that reads a text file that contains a grade (for instance, 87) on each line.
Calculate and print the average of the grades.
Solution
import java.io.BufferedReader;
import java.io.FileReader; // importing filereader
import java.io.IOException; // importing file io exceptions
public class grade {
public static void main(String[] args) {
BufferedReader br = null;
double count=0;
int noOfgrades=0;
try {
String sLine;
br = new BufferedReader(new FileReader("sample.txt")); // reading file using buffer
reader
while ((sLine = br.readLine()) != null) { // rading line by line
noOfgrades=noOfgrades+1; // getting number of grades
int num=Integer.parseInt(sLine); // convert string into integer
count=count+num; // adding each grade to count
}
System.out.println("count:t"+count);
System.out.println("noOfgrades:t"+noOfgrades);
System.out.println("Average of the grades:t"+(count/noOfgrades));
} catch (IOException e) {
e.printStackTrace(); // catch exception
} finally {
try {
if (br != null)br.close(); // closing buffer
} catch (IOException ex) {
ex.printStackTrace();// printing exceptions
}
}
}
}
/********** out file******
fileName:sample.txt
file contains "
23
34
56
7
88
89
52
34
65
"
*/
/************output*************
count: 448.0
noOfgrades: 9
Average of the grades: 49.77
*/

More Related Content

Similar to In JavaWrite a program that reads a text file that contains a gra.pdf

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
 
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
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Don Bosco BSIT
 
Program reads in a file of phone numbers without area codes .pdf
Program reads in a file of phone numbers without area codes  .pdfProgram reads in a file of phone numbers without area codes  .pdf
Program reads in a file of phone numbers without area codes .pdf
nitinarora01
 
Below is my code for a line editor import java.io.BufferedReader;.pdf
Below is my code for a line editor import java.io.BufferedReader;.pdfBelow is my code for a line editor import java.io.BufferedReader;.pdf
Below is my code for a line editor import java.io.BufferedReader;.pdf
alankarshoe84
 

Similar to In JavaWrite a program that reads a text file that contains a gra.pdf (20)

Java file
Java fileJava file
Java file
 
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
 
For this assignment, you will develop starter code. After
For this assignment, you will develop starter code. After For this assignment, you will develop starter code. After
For this assignment, you will develop starter code. After
 
Class notes(week 5) on command line arguments
Class notes(week 5) on command line argumentsClass notes(week 5) on command line arguments
Class notes(week 5) on command line arguments
 
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
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io Streams
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
 
Program reads in a file of phone numbers without area codes .pdf
Program reads in a file of phone numbers without area codes  .pdfProgram reads in a file of phone numbers without area codes  .pdf
Program reads in a file of phone numbers without area codes .pdf
 
Write a java program that reads a number N as input and displays the.pdf
Write a java program that reads a number N as input and displays the.pdfWrite a java program that reads a number N as input and displays the.pdf
Write a java program that reads a number N as input and displays the.pdf
 
ExtraFileIO.pptx
ExtraFileIO.pptxExtraFileIO.pptx
ExtraFileIO.pptx
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
 
Class notes(week 5) on command line arguments
Class notes(week 5) on command line argumentsClass notes(week 5) on command line arguments
Class notes(week 5) on command line arguments
 
In JavaWrite a program that reads a file and counts how many line.pdf
In JavaWrite a program that reads a file and counts how many line.pdfIn JavaWrite a program that reads a file and counts how many line.pdf
In JavaWrite a program that reads a file and counts how many line.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
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
Unit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptxUnit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptx
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
 
Java Notes
Java Notes Java Notes
Java Notes
 
Below is my code for a line editor import java.io.BufferedReader;.pdf
Below is my code for a line editor import java.io.BufferedReader;.pdfBelow is my code for a line editor import java.io.BufferedReader;.pdf
Below is my code for a line editor import java.io.BufferedReader;.pdf
 

More from mumnesh

Problem 1 Create Node class (or use what you have done in Lab4)• .pdf
Problem 1 Create Node class (or use what you have done in Lab4)• .pdfProblem 1 Create Node class (or use what you have done in Lab4)• .pdf
Problem 1 Create Node class (or use what you have done in Lab4)• .pdf
mumnesh
 
Hi, please I need help with the correct answer to the above multiple.pdf
Hi, please I need help with the correct answer to the above multiple.pdfHi, please I need help with the correct answer to the above multiple.pdf
Hi, please I need help with the correct answer to the above multiple.pdf
mumnesh
 
For Programming Embedded SystemsQ-07 Whcih variable and condition.pdf
For Programming Embedded SystemsQ-07 Whcih variable and condition.pdfFor Programming Embedded SystemsQ-07 Whcih variable and condition.pdf
For Programming Embedded SystemsQ-07 Whcih variable and condition.pdf
mumnesh
 
Discuss the dangers associated with hydrogen sulfide in the petro.pdf
Discuss the dangers associated with hydrogen sulfide in the petro.pdfDiscuss the dangers associated with hydrogen sulfide in the petro.pdf
Discuss the dangers associated with hydrogen sulfide in the petro.pdf
mumnesh
 
CommunicationsSystems have four basic properties (holism, equi-fi.pdf
CommunicationsSystems have four basic properties (holism, equi-fi.pdfCommunicationsSystems have four basic properties (holism, equi-fi.pdf
CommunicationsSystems have four basic properties (holism, equi-fi.pdf
mumnesh
 
Abraham Shine recently opaned his own accounting fiem on April 1, whi.pdf
Abraham Shine recently opaned his own accounting fiem on April 1, whi.pdfAbraham Shine recently opaned his own accounting fiem on April 1, whi.pdf
Abraham Shine recently opaned his own accounting fiem on April 1, whi.pdf
mumnesh
 
14. Suppose I have collected the names of people in several separate.pdf
14. Suppose I have collected the names of people in several separate.pdf14. Suppose I have collected the names of people in several separate.pdf
14. Suppose I have collected the names of people in several separate.pdf
mumnesh
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
mumnesh
 

More from mumnesh (20)

Poisson distribution] The number of data packets arriving in 1 sec at.pdf
Poisson distribution] The number of data packets arriving in 1 sec at.pdfPoisson distribution] The number of data packets arriving in 1 sec at.pdf
Poisson distribution] The number of data packets arriving in 1 sec at.pdf
 
Problem 1 Create Node class (or use what you have done in Lab4)• .pdf
Problem 1 Create Node class (or use what you have done in Lab4)• .pdfProblem 1 Create Node class (or use what you have done in Lab4)• .pdf
Problem 1 Create Node class (or use what you have done in Lab4)• .pdf
 
One of your friends has several brothers and sisters, each quite dif.pdf
One of your friends has several brothers and sisters, each quite dif.pdfOne of your friends has several brothers and sisters, each quite dif.pdf
One of your friends has several brothers and sisters, each quite dif.pdf
 
Malpighiaceae luonanthaceae Ochnaceae Flacourtiaceae O Monilophytes G.pdf
Malpighiaceae luonanthaceae Ochnaceae Flacourtiaceae O Monilophytes G.pdfMalpighiaceae luonanthaceae Ochnaceae Flacourtiaceae O Monilophytes G.pdf
Malpighiaceae luonanthaceae Ochnaceae Flacourtiaceae O Monilophytes G.pdf
 
How does Pb and TCE partition between components of soil (mineral, o.pdf
How does Pb and TCE partition between components of soil (mineral, o.pdfHow does Pb and TCE partition between components of soil (mineral, o.pdf
How does Pb and TCE partition between components of soil (mineral, o.pdf
 
Hi, please I need help with the correct answer to the above multiple.pdf
Hi, please I need help with the correct answer to the above multiple.pdfHi, please I need help with the correct answer to the above multiple.pdf
Hi, please I need help with the correct answer to the above multiple.pdf
 
For which phyla are megaspores a shared trait Bryophyta Monilophyt.pdf
For which phyla are megaspores a shared trait  Bryophyta  Monilophyt.pdfFor which phyla are megaspores a shared trait  Bryophyta  Monilophyt.pdf
For which phyla are megaspores a shared trait Bryophyta Monilophyt.pdf
 
For Programming Embedded SystemsQ-07 Whcih variable and condition.pdf
For Programming Embedded SystemsQ-07 Whcih variable and condition.pdfFor Programming Embedded SystemsQ-07 Whcih variable and condition.pdf
For Programming Embedded SystemsQ-07 Whcih variable and condition.pdf
 
For analysis of Thermodynamic Cycle, we draw T-s or P-h diagram. Why.pdf
For analysis of Thermodynamic Cycle, we draw T-s or P-h diagram. Why.pdfFor analysis of Thermodynamic Cycle, we draw T-s or P-h diagram. Why.pdf
For analysis of Thermodynamic Cycle, we draw T-s or P-h diagram. Why.pdf
 
Fill in the blank. A contingency table relates only continuous rando.pdf
Fill in the blank. A contingency table relates  only continuous rando.pdfFill in the blank. A contingency table relates  only continuous rando.pdf
Fill in the blank. A contingency table relates only continuous rando.pdf
 
Discuss the dangers associated with hydrogen sulfide in the petro.pdf
Discuss the dangers associated with hydrogen sulfide in the petro.pdfDiscuss the dangers associated with hydrogen sulfide in the petro.pdf
Discuss the dangers associated with hydrogen sulfide in the petro.pdf
 
Complete the following paragraph to describe the various reproductive.pdf
Complete the following paragraph to describe the various reproductive.pdfComplete the following paragraph to describe the various reproductive.pdf
Complete the following paragraph to describe the various reproductive.pdf
 
CommunicationsSystems have four basic properties (holism, equi-fi.pdf
CommunicationsSystems have four basic properties (holism, equi-fi.pdfCommunicationsSystems have four basic properties (holism, equi-fi.pdf
CommunicationsSystems have four basic properties (holism, equi-fi.pdf
 
Based on your reading of the GCU introduction and the textbooks, wha.pdf
Based on your reading of the GCU introduction and the textbooks, wha.pdfBased on your reading of the GCU introduction and the textbooks, wha.pdf
Based on your reading of the GCU introduction and the textbooks, wha.pdf
 
Answer using Giardias, Shiga-toxin producing E.coli1.Causative Agn.pdf
Answer using Giardias, Shiga-toxin producing E.coli1.Causative Agn.pdfAnswer using Giardias, Shiga-toxin producing E.coli1.Causative Agn.pdf
Answer using Giardias, Shiga-toxin producing E.coli1.Causative Agn.pdf
 
Abraham Shine recently opaned his own accounting fiem on April 1, whi.pdf
Abraham Shine recently opaned his own accounting fiem on April 1, whi.pdfAbraham Shine recently opaned his own accounting fiem on April 1, whi.pdf
Abraham Shine recently opaned his own accounting fiem on April 1, whi.pdf
 
29. This figure shows the principal coronary blood vessels. Which one.pdf
29. This figure shows the principal coronary blood vessels. Which one.pdf29. This figure shows the principal coronary blood vessels. Which one.pdf
29. This figure shows the principal coronary blood vessels. Which one.pdf
 
14. Suppose I have collected the names of people in several separate.pdf
14. Suppose I have collected the names of people in several separate.pdf14. Suppose I have collected the names of people in several separate.pdf
14. Suppose I have collected the names of people in several separate.pdf
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
 
13. What is the difference between post-consumer recycled materials a.pdf
13. What is the difference between post-consumer recycled materials a.pdf13. What is the difference between post-consumer recycled materials a.pdf
13. What is the difference between post-consumer recycled materials a.pdf
 

Recently uploaded

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
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
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
 
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
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
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
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
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
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
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
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
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
 

In JavaWrite a program that reads a text file that contains a gra.pdf

  • 1. In Java: Write a program that reads a text file that contains a grade (for instance, 87) on each line. Calculate and print the average of the grades. Solution import java.io.BufferedReader; import java.io.FileReader; // importing filereader import java.io.IOException; // importing file io exceptions public class grade { public static void main(String[] args) { BufferedReader br = null; double count=0; int noOfgrades=0; try { String sLine; br = new BufferedReader(new FileReader("sample.txt")); // reading file using buffer reader while ((sLine = br.readLine()) != null) { // rading line by line noOfgrades=noOfgrades+1; // getting number of grades int num=Integer.parseInt(sLine); // convert string into integer count=count+num; // adding each grade to count } System.out.println("count:t"+count); System.out.println("noOfgrades:t"+noOfgrades); System.out.println("Average of the grades:t"+(count/noOfgrades)); } catch (IOException e) { e.printStackTrace(); // catch exception } finally { try { if (br != null)br.close(); // closing buffer } catch (IOException ex) { ex.printStackTrace();// printing exceptions
  • 2. } } } } /********** out file****** fileName:sample.txt file contains " 23 34 56 7 88 89 52 34 65 " */ /************output************* count: 448.0 noOfgrades: 9 Average of the grades: 49.77 */