Question

1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into an appropriate folder in your drive.

a) Go through the codes then run the file and write the output with screenshot (please make sure that you change the location of the file) (20 points)

b) Write additional Java code that will show the average of all numbers in input.txt file (10 points)

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir pinput1 - Notepad File Edit Format View Help

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The output of the file, after executing the code provided by you:

run: Reading data from keyboard (-1 to quit):: First value: 10 Next value: 12 Next value: 11 Next value: Next value: Next v

========================================================================================

Code after adding the logic to calculate the average of the numbers.


package readingdata;

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


public class ReadingData {


public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
int total=0;
int count=0; // variable count, to count the number of inputs from user
int sentinal=-1;
System.out.println("\"Reading data from keyboard (-1 to quit)::\"");
System.out.println("First value: ");
int value=scan.nextInt();
count++; // increment count by 1, for eact input

while(value!=sentinal)
{
total+=value;
System.out.println("Next value: ");
value=scan.nextInt();
count++;
}
count--;
int average=total/count; // calculate average as total/count

System.out.println("Total: " + total);
System.out.println("Average: " + average); // print average

System.out.println("======================");
System.out.println("Reading data from file (input.txt)...");
File myFile=new File("C:\\Users\\123\\Desktop\\Input.txt");
Scanner sc = new Scanner(myFile);

int sum=0;
count=0; // again intialize count to 0, to count numbers in file

while(sc.hasNextInt())
{
int num=sc.nextInt();
sum+=num;
count++; // intialize count by 1 for each number in file
}
average=sum/count; // calculate average as sum/count

System.out.println("Reading data from file is completed...!!!");
System.out.println("Sum of all integers from file: " + sum);
System.out.println("Average of all integers from file: " + average); // print average from file
}
  
}


======================================================================================

Output after applying the logic for average:

run: Reading data from keyboard (-1 to quit):: First value: 12 Next value: 13 Next value: 4 Next value: Next value: Next va

Add a comment
Know the answer?
Add Answer to:
1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into...
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
  • import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...

    import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file. */ public class StatsDemo { // TASK #1 Add the throws clause public static void main(String[] args) { double sum = 0; // The sum of the numbers int count = 0; // The number of numbers added double mean = 0; // The average of the...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • Hello, Could you please input validate this code so that the code prints an error message...

    Hello, Could you please input validate this code so that the code prints an error message if the user enters in floating point numbers or characters or ANYTHING but VALID ints? And then could you please post a picture of the output testing it to make sure it works? * Write a method called evenNumbers that accepts a Scanner * reading input from a file with a series of integers, and * report various statistics about the integers to the...

  • Can you help me with this code in Java??? import java.util.Scanner; public class Main { public...

    Can you help me with this code in Java??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rows = 3; int columns = 4; int[][] arr = new int[rows][columns]; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { System.out.println("Enter a value: "); arr[i][j] = scan.nextInt(); } } System.out.println("The entered matrix:"); for(int i = 0; i < rows; ++i) { for(int j...

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • Question 15 (1 point) What does this program print? Please make sure your spacing is exact....

    Question 15 (1 point) What does this program print? Please make sure your spacing is exact. public class Test { System.out.print("39 + 3"); System.out.println(39 + 3); } 1 A/ Question 5 (1 point) Consider a file called input.txt that has the following 4 lines: Trace the program when it is 89 lines 78.5 is average. Isn't it? What would be the value of count after the following code snippet executes? Scanner in = new Scanner (new File("input.txt")); int count 0;...

  • Please explain if the following code is actually correct. If the following code correct, please explain...

    Please explain if the following code is actually correct. If the following code correct, please explain why the code works and is also correct. Don’t use * Java’s Integer .toBinaryString(int) in this program./* According to the textbook and the instructor, I am not supposed to use arrays such as int binary[] = new int[25]; I guess this is the reason why this problem is starting to look kind of hard.   Chapter 5 Exercise 37: Java Programming * * (Decimal to...

  • Get doubles from input file. Output the biggest. Answer the comments throughout the code. import java.io.File;...

    Get doubles from input file. Output the biggest. Answer the comments throughout the code. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Lab8Num1 { public static void main(String[] args) { //Declaring variable to be used for storing and for output double biggest,temp; //Creating file to read numbers File inFile = new File("lab8.txt"); //Stream to read data from file Scanner fileInput = null; try { fileInput = new Scanner(inFile); } catch (FileNotFoundException ex) { //Logger.getLogger(Lab10.class.getName()).log(Level.SEVERE, null, ex); } //get first number...

  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

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
ADVERTISEMENT