Question: Can someone fix my Java code and tell what I am doing wrong to get the median value from a text file that includes lines

Can someone fix my Java code and tell what I am doing wrong to get the median value from a text file that includes lines of ints? Please ONLY MODIFY the BODY of the public int Problem7. Do not modify the headers, and I need to keep the main method and public ProblemSeven methods in tact for my assignment. Do not change from scanner to BufferedReader! Explain what you did as I need to understand this.

public class ProblemSeven {

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

new ProblemSeven();

}

public ProblemSeven() throws FileNotFoundException {

int problemSolution = Problem7(new File("Problem7.txt"));

System.out.println("Your number was " + problemSolution);

}

public int Problem7(File file) throws FileNotFoundException {

LinkedList listInt = new LinkedList();

try {

Scanner scanner = new Scanner(file);

while (scanner.hasNextLine()) {

String value = scanner.nextLine();

listInt.add(Integer.parseInt(value));

System.out.println(value);

}

scanner.close();

} catch (Exception ex) {

ex.printStackTrace();

}

int[] intArray;

intArray = listInt.stream().toArray();

Arrays.sort(intArray);

int middle = intArray.length/2;

int median = 0;

if (intArray.length%2 == 1)

median = intArray[middle];

else

median = (intArray[middle-1] + intArray[middle]) / 2;

return median;

}}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!