Question

Write a java program to read from a file “input4_02.txt” an integer N (0? N ?...

Write a java program to read from a file “input4_02.txt” an integer N (0? N ? 100) and then read N double numbers from this file to an array. Write to file “output4_02.txt” this array but in the reverse order and also the sum of all elements in the array. For example File: “input4_02.txt” 5 1.30 2.22 4.00 17.60 3.14

File “output4_02.txt”

3.14 17.60 4.00 2.22 1.30

Sum = 28.26

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

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Scanner;

public class ReverseArray {

public static void main(String[] args)throws Exception {

// TODO Auto-generated method stub

Scanner scn=new Scanner(new File("C:/Users/ss/Desktop/input4_02.txt"));

//Read the entire input line

String str=scn.nextLine();

System.out.println(str);

//split it seperated by space and store in string array

String arr1[]=str.split(" ");

//Parse first string as int specifying the size of the array

int n=Integer.parseInt(arr1[0]);

float arr[]=new float[n];

float sum=0;

  

for(int i=0;i<n;i++){

//Parse each string as float and store it in float array

arr[i]=Float.parseFloat(arr1[i+1]);

sum+=arr[i];

}

for(int i=0;i<n/2;i++){

float temp=arr[i];

arr[i]=arr[n-1-i];

arr[n-1-i]=temp;

}

try{

FileWriter fr = new FileWriter("C:/Users/ss/Desktop/output4_02.txt");

BufferedWriter br = new BufferedWriter(fr);

PrintWriter out = new PrintWriter(br);

for(int i=0; i<n; i++){

out.write(arr[i]+" ");

}

out.println();

out.write("Sum = "+new Float(sum).toString());

out.close();

  

  

}

  

catch(IOException e){

System.out.println(e);

}

}

}

//Output Screen shots

02-Notepad File Edit Format View Help 5 1.30 2.22 4.00 17.60 3.14 DEY 9:13 AM 4 4/24/2018output4_02 File Edit Format View Help 13.14 17.64.0 2.22 1.3 p.1.17.626.Ο 2.22 1. sum = 28.26 DEV 9:13 AM I..CY4) will 4/24/2

In case you face any doubts do mention in the comments and do not forget to like the answer if it helped you :).

Add a comment
Know the answer?
Add Answer to:
Write a java program to read from a file “input4_02.txt” an integer N (0? N ?...
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
  • 1. write a java program using trees(binary search treee) to read integers from input file( .txt)...

    1. write a java program using trees(binary search treee) to read integers from input file( .txt) and add +1 to each number that you read from the input file. then copy result to another (.txt) file. example: if inpu File has numbers like 4787 79434 4326576 65997 4354 the output file must have result of 4788 79435 4326577 65998 4355 Note: there is no commas in between the numbers. dont give images or theory please.

  • Arrays and reading from a file USE C++ to write a program that will read a...

    Arrays and reading from a file USE C++ to write a program that will read a file containing floating point numbers, store the numbers into an array, count how many numbers were in the file, then output the numbers in reverse order. The program should only use one array and that array should not be changed after the numbers have been read. You should assume that the file does not contain more than 100 floating point numbers. The file name...

  • Write a C++ that read a list of numbers from a .txt file into array. The...

    Write a C++ that read a list of numbers from a .txt file into array. The first digit on the list will be array size, while the second number on the list will be our first number in our array list. Example: t1.txt file contain list off input number: 5, 3, 6, 10, 43, 23. notice the first number is 5 which will need to be use as an array size and the rest of the numbers are in the...

  • Python DESCRIPTION Write a program that will read an array of integers from a file and...

    Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....

  • write a java program Accept a positive integer n from keyboard and then create an array...

    write a java program Accept a positive integer n from keyboard and then create an array or arraylist containing n random elements within the range [-n,n). Print out the random array or arraylist, and then find out and print out the number of inversions and the maximum subarray (index range of the maximum subarray along with the maximum subarray sum) in the array or arraylist using divide and conquer, respectively. For example, suppose we accept integer 6 from keyboard, then...

  • JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file...

    JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to...

  • Here is the Prompt for problem 1: Write a C++ program that reads in a test file. The first numbe...

    Here is the Prompt for problem 1: Write a C++ program that reads in a test file. The first number in the file will be an integer, and will indicate the amount of decimal numbers to follow. Once you have the read the numbers from the file into an array then compute the following properties on the set of numbers: the sum, the average (mean), the variance, the standard deviation, and the median. Don’t try to do the entire program...

  • Please write in java program Write an application that generates 100 random integers between 1 and...

    Please write in java program Write an application that generates 100 random integers between 1 and 75 and writes them to a file named "file_activity.txt". Read the data back from the file and display the following: The sum of the numbers in the file The average of the numbers in the file The numbers in the file in increasing order To sort an array: int[] arr = new int[20]; Arrays.sort(arr); -- this sorts an array To sort an ArrayList: ArrayList<Integer>...

  • Java question Given an array of integer numbers, write a linear running time complexity program in...

    Java question Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A itf ATO] + A[1] + +A[iI-1] Ali+1]+ Ali+2] +... + A[n-1]; where 0 <i< n-1 Similarly, 0 is an stability index if (A[1] A[2]A[n-1]) 0 and n-1 is an stability index if (A[0] A[1]+... A[n-21) 0 Example:...

  • 2. Write a program that reads N integer numbers of array from the user and then...

    2. Write a program that reads N integer numbers of array from the user and then displays the sum, max number and the numbers of the array use four subroutines ( Read_Array(), Calculate_Sum(), Find_Max() and Display_Array()). Here is sample output Please enter number of elements: 4 Please enter the required numbers: 50 100 150 20 The entered numbers are: 50 100 150 20 Sum is : 320 Max is 150 by Mpsi ,sum and max to call subroutine and retrieve...

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