Question: Objective: Read the data in data.txt Download data.txtinto an array. Sort the array and output it to the screen. Details: The data.txt file has been

Objective: Read the data in data.txt Download data.txtinto an array. Sort the array and output it to the screen.
Details: The data.txt file has been formatted to contain a series of numbers separated by a newline character. The first number is the number of elements in the array which you can verify (there are 15 lines in the file not counting the first line).
TIP: You can actually use the Scanner object that you are already familiar with! Instead of passing System.in you can pass a File object and the Scanner will read from it as though the user is typing numbers into the console. Here is some code to get you started:
File myFile = new File("data.txt");
Scanner fileInput = new Scanner(myFile);
int n = fileInput.nextInt();
You will have to wrap that in a try/catch, but can quickly confirm that n =15. Use the variable n to create an array of size 15 then use a loop to read in each number using fileInput.nextInt(). Note: You shouldn't just make an array of 15, but use a variable so that your code works on other files with more numbers in them.
Use breakpoints or System.out.println to confirm you have correctly read in the array of 15 values.
Once that works, use one of the sorting algorithms discussed in the lecture to sort the array or use a sorting method we did not cover, but can be detailed online. The point is that the data becomes sorted from smallest to largest. Note: For the most part this can be copy and pasted from the slides. Or you can implement from scratch.
After this output the sorted array.
To re-iterate the steps you should follow are:
Download data.txt Download data.txtinto your project directory. This would be similar to where triangle.txt appeared for the Homework 3 assignment.
In your main method open data.txt and read in the first number. Use this number to create an array of that size.
In the example I used 15, but when grading your homework I may alter data.txt to confirm your code can work with arrays beyond that size.
Read the rest of the file into the array you created.
Sort the array using one of the methods covered in class. Or a different method if you want to challenge yourself.
Note: You are not allowed to use a built in sorting method. You must create your own.
Output the sorted array to the screen using a loop and System.out.println
Extra Credit (3 points): Implement a static method that performs the sorting algorithm. Remember when you alter an array within a method it alters it outside of the method, so you can pass the array in, sort it, then see it is sorted back in the main method.
data.txt file:
15
1
20
35
42
61
1511
96
24
11
1021
241
78
991
5
0
81

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 Programming Questions!