Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a file of (unsorted) integers. Input them into a list so the list is sort

ID: 3884223 • Letter: C

Question

Create a file of (unsorted) integers. Input them into a list so the list is sorted. Have at least 10 integers in your input file, but make your program work for any input file – meaning don’t use a for loop. CAN NOT USE COLLECTIONS OR COMPARATOR. Create a file of (unsorted) integers. Input them into a list so the list is sorted. Have at least 10 integers in your input file, but make your program work for any input file – meaning don’t use a for loop. CAN NOT USE COLLECTIONS OR COMPARATOR. Create a file of (unsorted) integers. Input them into a list so the list is sorted. Have at least 10 integers in your input file, but make your program work for any input file – meaning don’t use a for loop. CAN NOT USE COLLECTIONS OR COMPARATOR.

Explanation / Answer

ANSWER:

This program will work java 8 version only.

import java.io.BufferedReader;

import java.io.DataInputStream;

import java.io.FileInputStream;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

import java.util.stream.Collectors;

public class SortingMain {

@SuppressWarnings("resource")

public static List<Integer> readDataFromFile() throws Exception {

FileInputStream fstream = new FileInputStream("resource/inputfile.txt");

DataInputStream in = new DataInputStream(fstream);

BufferedReader br = new BufferedReader(new InputStreamReader(in));

String strLine;

List<Integer> inputList = new ArrayList<Integer>();

// Reading data from file and store in ArrayList

while ((strLine = br.readLine()) != null) {

inputList.add(Integer.valueOf(strLine));

}

return inputList;

}

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

List<Integer> inputList = readDataFromFile();

System.out.println("Before sorting: ");

System.out.println(inputList);

// Sorting the list using stream

List<Integer> result = inputList.stream().sorted().collect(Collectors.toList());

System.out.println(" After sorting: ");

System.out.println(result);

}

}

Input file:

10
4
8
1
6
9
44
45
47
46
87
99
88

Note: Please give file name correct location. If you get FileNotFound exception

Output:

Before sorting:
[10, 4, 8, 1, 6, 9, 44, 45, 47, 46, 87, 99, 88]
After sorting:
[1, 4, 6, 8, 9, 10, 44, 45, 46, 47, 87, 88, 99]

Let me know any concerns. Thank you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote