I am trying to read from a text file with integers seperated

I am trying to read from a text file with integers seperated with commas, store them into an arraylist and use mergeSort() to sort the arraylist.

public class ArrayListMergeSort//> { public ArrayListMergeSort() { } //call mergesort on param list public static > ArrayList mergesort(ArrayList items) { //if(debug) //System.out.println(\"domergesort called on arg (\" + ilist.toString() + \")\"); if(items.size() <= 1) { //if(debug) //System.out.println(\"Returning single length list: \" + ilist.toString()); return items; } else { ArrayList left = new ArrayList(); ArrayList right = new ArrayList(); int middle = items.size() / 2; //int division for(int i=0;i> ArrayList merge(ArrayList a, ArrayList b) { ArrayList ret = new ArrayList(); //return list int aIndex = 0, bIndex = 0; //counters of items left in respective lists while(aIndex+1 <= a.size() || bIndex+1 <= b.size()) { if(aIndex+1 <= a.size() && bIndex+1 <= b.size()) { if(a.get(aIndex).compareTo(b.get(bIndex)) <= 0.0) { ret.add(a.get(aIndex)); aIndex++; } else { ret.add(b.get(bIndex)); bIndex++; } } else if(aIndex+1 <= a.size()) { ret.add(a.get(aIndex)); aIndex++; } else if(bIndex+1 <= b.size()) { ret.add(b.get(bIndex)); bIndex++; } } //if(debug) //System.out.println(\"Returning merged array: \" + ret.toString()); return ret; } }

Solution


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {

private int[] array;
private int[] tempMergArr;
private int length;

public static void main(String a[]){
int mysize=0;
try{
Scanner inputFile = new Scanner(new File(\"input.txt\"));
while(inputFile.hasNextLine()){
String line = inputFile.nextLine();
mysize=line.length()/2+1;
}
  
inputFile.close();
}
catch (Exception e) {
e.printStackTrace();
}
int ArrayHere[] = new int[mysize];
int count = 0;
try{
Scanner inputFile = new Scanner(new File(\"input.txt\"));
while(inputFile.hasNextLine()){
String line = inputFile.nextLine();
  
Scanner scanner = new Scanner(line);
scanner.useDelimiter(\",\");
while(scanner.hasNextInt()){
ArrayHere[count]=(scanner.nextInt());
count++;
}
scanner.close();
}
  
inputFile.close();
}
catch (Exception e) {
e.printStackTrace();
}
  
Main mms = new Main();
mms.sort(ArrayHere);
for(int i:ArrayHere){
System.out.print(i);
System.out.print(\" \");
}
}

public void sort(int inputArr[]) {
this.array = inputArr;
this.length = inputArr.length;
this.tempMergArr = new int[length];
doMergeSort(0, length - 1);
}

private void doMergeSort(int lowerIndex, int higherIndex) {

if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
// Below step sorts the left side of the array
doMergeSort(lowerIndex, middle);
// Below step sorts the right side of the array
doMergeSort(middle + 1, higherIndex);
// Now merge both sides
mergeParts(lowerIndex, middle, higherIndex);
}
}

private void mergeParts(int lowerIndex, int middle, int higherIndex) {

for (int i = lowerIndex; i <= higherIndex; i++) {
tempMergArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tempMergArr[i] <= tempMergArr[j]) {
array[k] = tempMergArr[i];
i++;
} else {
array[k] = tempMergArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempMergArr[i];
k++;
i++;
}

}
}

I am trying to read from a text file with integers seperated with commas, store them into an arraylist and use mergeSort() to sort the arraylist. public class A
I am trying to read from a text file with integers seperated with commas, store them into an arraylist and use mergeSort() to sort the arraylist. public class A

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site