Question

Problem: Use the code I have provided to start writing a program that accepts a large...

Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this.

Input: Each line of input will be one item to be added to your array.

Output: Your output will be a file that is your sorted list.

Sample Input File:

65

3

91

12

Sample Output File:

3

12

65

91

Program

 package Lab02;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class Lab02 {
        int[] list;

        public static void main(String[] args) throws FileNotFoundException {
                new Lab02();
        }

        public Lab02() throws FileNotFoundException {
                readInFile(countLinesInFile());

                sort();

                printFile();
        }

        private void sort() {
                // TODO: YOUR CODE GOES HERE

        }

        private void printFile() throws FileNotFoundException {
                File outFile = new File("answer.txt");
                FileOutputStream outFileStream = new FileOutputStream(outFile);
                PrintWriter outStream = new PrintWriter(outFileStream);

                for (int i = 0; i < list.length; i++) {
                        outStream.println(list[i]);
                }

                outStream.close();
        }

        private int countLinesInFile() {
                Scanner file = null;
                try {
                        file = new Scanner(new File("input.txt"));
                } catch (FileNotFoundException e) {
                        System.out.println("System Error, exiting the program!");
                        System.exit(0);
                }

                int count = 0;
                while (file.hasNextLine()) {
                        if (file.nextLine().equals("#"))
                                break;
                        count++;
                }
                return count;
        }

        private void readInFile(int count) {
                Scanner file = null;
                try {
                        file = new Scanner(new File("input.txt"));
                } catch (FileNotFoundException e) {
                        System.out.println("System Error, exiting the program!");
                        System.exit(0);
                }

                list = new int[count];
                for (int i = 0; i < list.length; i++) {
                        list[i] = file.nextInt();
                }
        }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts,p lease give me comment...

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.PrintWriter;

import java.util.Scanner;

public class Lab02 {

    int[] list;

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

        new Lab02();

    }

    public Lab02() throws FileNotFoundException {

        readInFile(countLinesInFile());

        sort();

        printFile();

    }

    public void sort() {

        for (int i = 0; i < list.length; i++) {

            for (int j = i + 1; j < list.length; j++) {

                if (list[i] > list[j]) {

                    int temp = list[i];

                    list[i] = list[j];

                    list[j] = temp;

                }

            }

        }

    }

    private void printFile() throws FileNotFoundException {

        File outFile = new File("answer.txt");

        FileOutputStream outFileStream = new FileOutputStream(outFile);

        PrintWriter outStream = new PrintWriter(outFileStream);

        for (int i = 0; i < list.length; i++) {

            outStream.println(list[i]);

        }

        outStream.close();

    }

    private int countLinesInFile() {

        Scanner file = null;

        try {

            file = new Scanner(new File("input.txt"));

        } catch (FileNotFoundException e) {

            System.out.println("System Error, exiting the program!");

            System.exit(0);

        }

        int count = 0;

        while (file.hasNextLine()) {

            if (file.nextLine().equals("#"))

                break;

            count++;

        }

        return count;

    }

    private void readInFile(int count) {

        Scanner file = null;

        try {

            file = new Scanner(new File("input.txt"));

        } catch (FileNotFoundException e) {

            System.out.println("System Error, exiting the program!");

            System.exit(0);

        }

        list = new int[count];

        for (int i = 0; i < list.length; i++) {

            list[i] = file.nextInt();

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
Problem: Use the code I have provided to start writing a program that accepts a large...
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
  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • I have a program that reads a file and then creates objects from the contents of...

    I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • Computer Science - Java Explain the code step by step (in detailed order). Also explain what...

    Computer Science - Java Explain the code step by step (in detailed order). Also explain what the program required. Thanks 7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] -...

  • Write a program to create a file named integerFile.txt if it does not exist. Write 100...

    Write a program to create a file named integerFile.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. The random integers should be in the range 0 to 100 (including 0 and 100). Integers should be separated by spaces in the file. Read the data back from the file and display the data in increasing order This is what I have so far: import java.io.File; import java.util.Scanner; import java.io.PrintWriter; public class integerFile {...

  • This is my current output for my program. I am trying to get the output to...

    This is my current output for my program. I am trying to get the output to look like This is my program Student.java import java.awt.GridLayout; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.swing.JFileChooser; public class Student extends javax.swing.JFrame {     BufferedWriter outWriter;     StudentA s[];     public Student() {         StudentGUI();            }     private void StudentGUI() {         jScrollPane3 = new javax.swing.JScrollPane();         inputFileChooser = new javax.swing.JButton();         outputFileChooser = new javax.swing.JButton();         sortFirtsName = new...

  • I have this program that works but not for the correct input file. I need the...

    I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95 output needs to look like: Tom Brady -------------------------- Assignment 1: A Assignment 2: A Assignment 3: E Assignment 4: A Assignment 5: C Final Grade: 82.4 = B The current program: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class main {    public static void main(String[]...

  • JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole...

    JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole I would like it to print the first 3 payments and the last 3 payments if n is larger than 6 payments. Please help! Thank you!! //start of program import java.util.Scanner; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.lang.Math; //345678901234567890123456789012345678901234567890123456789012345678901234567890 /** * Write a description of class MortgageCalculator here. * * @author () * @version () */ public class LoanAmortization {    /* *...

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