Java Programming Help
Hi I really need some assistance right now. I am currently writing a code to do External Sorting for a very huge text file of randomly generated words. I am stuck at how I should read the text up to a certain amount and store it. Example pretend I just have 10 words for now. I am trying to store 2 of the words into an array string and then store another two into another array string. Then each array string I would write them out to a temp file. I am currently stuck at reading up to a certain amount. I have tried .readline but since all the words are only separated by a space it didnt help. I try using scanner but not really sure how to do it so it can store 2 into a string array then the next two in another string array. Please Help in what kinda approach I should go for this. I will appreciate it a lot
Space separated isn't a problem. The scanner can be used with just the .next() method which works on any whitespace by default.
What is the need for the two arrays? As I read this now, you are just collecting four words into two separate arrays, and then just writing them to a file. Why not just write them to a file directly?
If you need arrays, than I'd suggest a multidimensional array of [x] would be easier to work with. This way you can keep spawning temporary arrays of size 2, increase the size of the outer array and push it on. Now you don't need to keep track of multiple arrays.
As for determining, that's done with a simple counter. Since you'll be using a while(scannerobject.hasNext()) to keep the scanner reading, you can simply use an integer to keep counting the number of .next calls. That can be used to determine if you need to create a new array or not via modulus operation.
Store 2 in each array is only an example. My ultimate goal is to read an extremely large text file and store parts of the file into different array so I can sort each individually and then merge.
With Scanner I have tried that, I have tried doing this
B would be the number of times I have to run thru this to store all my items. So it would be the total number of element in my file divided by the number of elements i want in each file.
int j = 0;
Scanner scanner = new Scanner(in);
store[j] = scanner.next();
But when I did this and ran it. It only store the first two elements in my file and dont seem to read the others.
I'm still not sure I follow what you're doing here.
The termination of the inner loop can be caused by either no hasNext tokens, j exceeding the size of pages, or i exceeding the size of B. Given what is occurring within the for loop, that would peg my suspicion on the i exceeding that of B.
What's the purpose of this outer for loop? I'm still not seeing a need for multiple arrays either, if you plan on merging them in the end, keeping separate arrays will just complicate the sorting algorithms.