Monday 15 August 2011

java - Reading from a text file and recursively merge sorting it -



java - Reading from a text file and recursively merge sorting it -

i know question seems duplicate have looked @ every related question on , on 1 time again , couldn't find reply question. supposed read text file name determined user input. file contains unsorted set of type double numbers, such as:

"0.1212312" "0.0013315" "0.0106026"

i prompted merge sort through recursive means. programme has no problem far compiling goes, , don't run-time errors, programme not respond correctly inputs , can't seem figure out why. inquire senpais' assitance. here code:

import java.io.*; import java.nio.file.path; import java.util.*; import java.lang.*; public class projectassingment3 { public static void main(string[] args) throws ioexception { system.out.println("enter file name please:"); scanner input; input = new scanner(system.in); string in; in = input.nextline(); int count = 0; for(int = 0; < in.length(); i++) { if(character.iswhitespace(in.charat(i)))count++; } while(in.equals(null)||(count>1)) { count=0; for(int = 0; < in.length(); i++) // counts white spaces determine number of arguments in input { if(character.iswhitespace(in.charat(i))) count++; } system.out.println("invalid input! please seek again!"); input = new scanner(system.in); in = input.nextline(); } seek { scanner sc = new scanner(new filereader(new file(in))); list<double> lines = new arraylist<double>(); int =0; while (sc.hasnextline()) { if(sc.nextline().equals(null))//checks if empty lines { system.out.println("empty line encountered!"); system.exit(-1); } else if(!sc.hasnextdouble())//checks if non-double type lines { system.out.println("invalid line encountered" + sc.nextline()); system.exit(-1); } lines.add(double.parsedouble(sc.nextline())); system.out.println(lines.get(i));i++; //prints out original unsorted array. } sc.close(); //unboxes values arraylist onto array of type double double arr[] = new double[lines.size()]; for(int j=0;j<arr.length;j++) { arr[j] = lines.get(j); } mergesort(arr, 0, arr.length - 1); //prints sorted array for(int x=0;x<arr.length;x++) { system.out.println(arr[x]); } } grab (ioexception e) { system.out.print("failed read input file:" + e.getmessage()); } } public static void domerge(double [] numbers, int left, int mid, int right) { double [] temp = new double[25]; int i, left_end, num_elements, tmp_pos; left_end = (mid - 1); tmp_pos = left; num_elements = (right - left + 1); while ((left <= left_end) && (mid <= right)) { if (numbers[left] <= numbers[mid]) temp[tmp_pos] = numbers[left++]; else temp[tmp_pos++] = numbers[mid++]; } while (left <= left_end) temp[tmp_pos++] = numbers[left++]; while (mid <= right) temp[tmp_pos++] = numbers[mid++]; (i = 0; < num_elements; i++) { numbers[right] = temp[right]; right--; } } public static void mergesort(double [] numbers, int left, int right) { int mid; if (right > left) { mid = (right + left) / 2; mergesort(numbers, left, mid); mergesort(numbers, (mid + 1), right); domerge(numbers, left, (mid+1), right); } }

}

and yes, indeed assignment given me, i'm not asking of guys me. genuinely stomped , simply asking clarification!

i appreciate effort set silly question of mine!

when give total path accepts file gives exception.

also throw exception catching it?

one lastly thing, syntax of main public static void main(string[] args), must string[]args parameter, (at to the lowest degree in eclipse), otherwise wont recognize main method

java filereader mergesort

No comments:

Post a Comment