Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone try help me on solving this error. I'm trying to write my results to outputfile but it's not writing anything to the output

Can someone try help me on solving this error.

I'm trying to write my results to outputfile but it's not writing anything to the output file. Can you debug the code and see where I have the error.

Inorder for it to work you might wanna creat a random file with either numbers only or a file with words only for testing

import java.util.*; import java.nio.file.*; import java.io.*; public class P5Program { private ArrayList listInt; // List of integer numbers private ArrayList stringList; //List of String words private String fileName; // File name for the Output file private String outputString; // Output String private String outputFile; // File name for the Output file private final String LS = System.lineSeparator(); private double startSystemTime; // CPU clock startindg time for int private double endSystemTime; // CPU clock starting time for float private double startTime; // Wall clock starting time for int private double endTime; private int cpuTimeSelectionSort; private int cpuTimeBubbleSort; private int cpuTimeCollection; private int cpuTimeSelectionSortInt; private int cpuTimeBubbleSortInt; private int cpuTimeCollectionInt; private int wallClockSelectionSort; private int wallClockBubbleSort; private int wallClockCollectionSort; private int wallClockSelectionSortInt; private int wallClockBubbleSortInt; private int wallClockCollectionSortInt; public P5Program() { listInt = new ArrayList(); stringList = new ArrayList(); fileName = null; outputFile = "P5Output.txt"; } public static void main (String[] args) throws IOException { P5Program sort = new P5Program(); sort.mainMenu(); sort.outputToFile(); } public void mainMenu(){ Scanner scan = new Scanner(System.in); int operation = 0; System.out.println("Please enter a valid Input file name with .txt ... "); fileName = scan.nextLine(); System.out.println("Choose the operation you want to perform on those below. "); System.out.println("Operation one is to sort an ArrayList of Strings. " + "Operation two is to sort an ArrayList of Intergers. "); operation = scan.nextInt(); if(operation == 1){ readTheFilesForString(); // Timing selction sort for an Arralist of String startTime = System.currentTimeMillis(); startSystemTime = System.nanoTime(); selectionSort(); endTime = System.currentTimeMillis(); endSystemTime = System.nanoTime(); wallClockSelectionSort = (int)(endTime - startTime); cpuTimeSelectionSort = (int)(endSystemTime - startSystemTime); // Timing bubble sort for an Arralist of Strings startTime = System.currentTimeMillis(); startSystemTime = System.nanoTime(); bubbleSort(); endTime = System.currentTimeMillis(); endSystemTime = System.nanoTime(); wallClockBubbleSort = (int) (endTime - startTime); cpuTimeBubbleSort = (int) (endSystemTime - startSystemTime); // Timing collection sort in an Arraylist of Strings startTime = System.currentTimeMillis(); startSystemTime = System.nanoTime(); collectionSortString(); endTime = System.currentTimeMillis(); endSystemTime = System.nanoTime(); wallClockCollectionSort = (int) (endTime - startTime); cpuTimeCollection = (int) (endSystemTime - startSystemTime); } else if (operation == 2){ readTheFilesForInt(); // Timing selction sort for an Arraylist of Integers startTime = System.currentTimeMillis(); startSystemTime = System.nanoTime(); selectionSortInt(); endTime = System.currentTimeMillis(); endSystemTime = System.nanoTime(); wallClockSelectionSortInt = (int) (endTime - startTime); cpuTimeSelectionSortInt = (int) (endSystemTime - startSystemTime); // Timing bubble sort for an Arraylist of Integers startTime = System.currentTimeMillis(); startSystemTime = System.nanoTime(); bubbleSortInt(); endTime = System.currentTimeMillis(); endSystemTime = System.nanoTime(); wallClockBubbleSortInt = (int) (endTime - startTime); cpuTimeBubbleSortInt = (int) (endSystemTime - startSystemTime); // Timing selection sort sort for an Arraylist of Integers startTime = System.currentTimeMillis(); startSystemTime = System.nanoTime(); selectionSortInt(); endTime = System.currentTimeMillis(); endSystemTime = System.nanoTime(); wallClockSelectionSortInt = (int) (endTime - startTime); cpuTimeSelectionSortInt = (int) (endSystemTime - startSystemTime); // Timing collection sort for an Arraylist of Integers startTime = System.currentTimeMillis(); startSystemTime = System.nanoTime(); collectionSort(); endTime = System.currentTimeMillis(); endSystemTime = System.nanoTime(); wallClockCollectionSortInt = (int) (endTime - startTime); cpuTimeCollectionInt = (int) (endSystemTime - startSystemTime); } else{ System.out.println("Please choose a valid operation. "); } }// end of mainMenu public void selectionSort(){ int minValue = 0; int startScan; int index; String temp; /**ArrayList arr = new ArrayList(); arr.add("C"); arr.add("D"); arr.add("A"); arr.add("R"); arr.add("S"); arr.add("O");*/ for( startScan = 0; startScan < stringList.size(); startScan++){ stringList.set( minValue, stringList.get(startScan)); for( index = startScan + 1; index < stringList.size(); index++){ if((stringList.get(index)).compareToIgnoreCase(stringList.get(startScan))<0){ temp = stringList.get(startScan); stringList.set( startScan, stringList.get(index)); stringList.set(index , temp); } } System.out.println(stringList.get(startScan) + " "); } }// end of selection sort public void selectionSortInt(){ int minValue = 0; int startScan; int index; int temp; /**ArrayList arr = new ArrayList(); arr.add(8); arr.add(5); arr.add(2); arr.add(3); arr.add(6); arr.add(1);*/ for( startScan = 0; startScan < listInt.size(); startScan++){ listInt.set( minValue, listInt.get(startScan)); for( index = startScan + 1; index < listInt.size(); index++){ if((listInt.get(index)) < (listInt.get(startScan))){ temp = listInt.get(startScan); listInt.set( startScan, listInt.get(index)); listInt.set(index , temp); } } System.out.println(listInt.get(startScan) + " "); } }// end of selectionSortInt public void bubbleSort(){ int startScan; int index; String temp; /**ArrayList arr = new ArrayList(); arr.add("C"); arr.add("D"); arr.add("A"); arr.add("R"); arr.add("S"); arr.add("O");*/ for( startScan = 0;startScan < stringList.size();startScan++){ for( index = startScan + 1;index < stringList.size();index++){ if((stringList.get(index)).compareToIgnoreCase(stringList.get(startScan))<0){ temp = stringList.get(startScan); stringList.set( startScan, stringList.get(index)); stringList.set(index , temp); } } System.out.println(stringList.get(startScan) + " "); } }// end of bubbleSort public void bubbleSortInt(){ int startScan; int index; int temp; /**ArrayList arr = new ArrayList(); arr.add(5); arr.add(7); arr.add(3); arr.add(2); arr.add(4); arr.add(8);*/ for( startScan = 0;startScan < listInt.size();startScan++){ for(index = startScan + 1;index arr = new ArrayList(); arr.add(5); arr.add(7); arr.add(3); arr.add(2); arr.add(4); arr.add(8);*/ Collections.sort(listInt); for( index = 0; index arr = new ArrayList(); arr.add("C"); arr.add("D"); arr.add("A"); arr.add("R"); arr.add("S"); arr.add("O");*/ Collections.sort(stringList); for( index = 0; index

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions