Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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][2] 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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
    for(int i=0;i<B;i++)
            {
                           
    int j 0;
                           if(
    j<pages){
                    
    Scanner scanner = new Scanner(in);
                    while(
    scanner.hasNext()&&j<pages){
                    
    store[j] = scanner.next();
                    
    System.out.println(store[j]);
                    
    j++;
                    }
                    
    scanner.close();
                }
                      }; 
    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.
    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.

  • #4
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Someone please help

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •