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...

  • 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...

  • 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...

  • Write a complete Java program in a file caled Module1Progrom.java that reads all the tokens from...

    Write a complete Java program in a file caled Module1Progrom.java that reads all the tokens from a file named dota.txt that is to be found in the same directory as the running program. The program should ignore al tokens that cannot be read as an integer and read only the ones that can. After reading all of the integers, the program should print all the integers back to the screen, one per line, from the smalest to the largest. For...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

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