Question

In a package named "lecture" create an object named "LectureQuestion" with a method named "fileSum" that...

In a package named "lecture" create an object named "LectureQuestion" with a method named "fileSum" that takes a filename as a String and returns an Int which is the sum of the values in the file • The input file will contain multiple lines each with multiple integer values separated by the '#' character • Return the sum of all of the integer values in the file • You may assume that the file exists and is properly formatted Submit a zip file of your project to AutoLab: File > Export to zip file

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

package lecture;

import java.io.BufferedReader;
import java.io.FileReader;

public class LectureQuestion {

   public static void main(String[] args) {
       // Call function with filename
       int sum = fileSum("sumdata.txt");
       System.out.println("Sum of data in files: "+sum);
   }
  
   public static int fileSum(String filename) {
       int sum = 0;
       try {
           // Open file to read
           BufferedReader br = new BufferedReader(new FileReader(filename));
           String line = "";
          
           while((line=br.readLine())!=null) {
               // Split the line usig delimiter #
               String data[] = line.split("#");
               for(int i = 0;i<data.length;i++) {
                   // Add values to sum after converting into Integer
                   sum+=Integer.parseInt(data[i]);
               }
           }
       } catch (Exception e) {
           System.out.println(e.getMessage());
       }
       // Return sum
       return sum;
   }
}

INPUT FILE-

OUTPUT-

Add a comment
Know the answer?
Add Answer to:
In a package named "lecture" create an object named "LectureQuestion" with a method named "fileSum" that...
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
  • Java arrays Create method named repeatedN that takes two integer parameters named range and n and...

    Java arrays Create method named repeatedN that takes two integer parameters named range and n and returns an integer array Method should return an integer array that has numbers 0 - range repeated in order n times If range is less than or equal to 0; return an array that has 0 repeated n times Create a second method named printArray that takes an integer array as a parameter. The method should print out every element on the same line...

  • JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named...

    JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named StringParser with the following: ApublicstaticmethodnamedfindIntegerthattakesaStringandtwocharvariables as parameters (in that order) and does not return anything. The method should find and print the integer value that is located in between the two characters. You can assume that the second char parameter will always follow the firstchar parameter. However, you cannot assume that the parameters will be different from...

  • You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must...

    You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must include all the following described methods. Each of these methods should be public and static.Write a method named getFirst, that takes an Array of int as an argument and returns the value of the first element of the array. NO array lists. Write a method named getLast, that takes an Array of int as an argument and returns the value of the last element...

  • Write a Java program that contains a method named ReadAndFindMax. The argument to this method is...

    Write a Java program that contains a method named ReadAndFindMax. The argument to this method is the filename (of String type). The method opens the file specified in the method argument filename and then reads integers from it. This file can contain any number of integers. Next, the method finds the maximum of these integers and returns it. The main method must first ask the user to enter the filename; then call ReadAndFindMax method with the entered filename as an...

  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • Create the header file named “Complex.h” that contains the following class: The class Complex represents a...

    Create the header file named “Complex.h” that contains the following class: The class Complex represents a complex number which is a number of the form a + bi where a and b are real numbers and i2 = −1. The class should contain: Private double field named real. Private double field named imaginary. Public default constructor that assigns 1 to real and 0 to imaginary. Public overloaded constructor that takes a double as a parameter named real. It assigns real...

  • Create a class named HW that has the main method. Leave the main method empty for...

    Create a class named HW that has the main method. Leave the main method empty for now. • Create a method named allPairs that takes an integer array parameter named a that contains only positive integers and does not return anything. The method should print out all pairs of numbers that have a sum that is prime. If there are no pairs that have a sum that is prime, print out "No pairs". Note that in the first example, (2,...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • We want to create and then initialize an array named A to contain the integers: 4,...

    We want to create and then initialize an array named A to contain the integers: 4, 5, 6, 7. There are multiple ways this can be done. From the list below, select all of the correct ways to create and initialize A. int size = 4; int [] A = new int [size]; A[0] = 4; A[1] = 5; A[2] = 6; A[3] = 7; int [] A = new int [4];       A[0] = 4;       A[1] = 5;...

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