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 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 Filedatatxt;
Scanner fileInput new ScannermyFile;
int n fileInput.nextInt;
You will have to wrap that in a trycatch but can quickly confirm that n Use the variable n to create an array of size then use a loop to read in each number using fileInput.nextInt Note: You shouldn't just make an array of 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 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 reiterate 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 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 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 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:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
