Need help in finding where to start looking to fix my code. When reading large files it gets stuck

Voz zzz :

I'm trying to read a large text file of about 7516 lines of text. When I read a smaller file (9 lines) the program works fine. But with the large file it seems to get caught in something and just keeps running without anything actually happening.I'm not sure where to start looking for the issue.

The code reads a text file and then turn it into an array. Then it passes the array into a shuffle and writes it into another text file. Or at least that's what I want it to do.

package Rn1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Read2 {
    static void randomShuffle( int arr[], int n) 
    { 
// Creating a object for Random class 
        Random r = new Random(); 
// Start from the last element and swap one by one. We don't 
// need to run for the first element that's why i > 0 
        for (int i = n-1; i > 0; i--) { 

// Pick a random index from 0 to i 
            int j = r.nextInt(i+1); 

// Swap arr[i] with the element at random index 
            int temp = arr[i]; 
            arr[i] = arr[j]; 
            arr[j] = temp; 
} 
// Prints the random array 

System.out.println(Arrays.toString(arr)); 
} 


    public static String readString(String file) {
        String text = "";
        try {
            Scanner s = new Scanner(new File(file));
            while(s.hasNext()) {
                text = text + s.next() + " ";
            }
        }
        catch(FileNotFoundException e) {
            System.out.println("Not Found");
        }

        return text;
    }

    public static String[] readArray(String file) {
        //Step 1:
    //Count how many elements in the file (lines)
        //Step 2
        //Create array
        int ctr = 0;
        try {
            Scanner s1 = new Scanner(new File(file));
            while (s1.hasNextLine()) {
                ctr = ctr +1;
                if (s1.hasNext()) {
                s1.next();
                }
            }
            String[] words = new String[ctr];

            Scanner s2 = new Scanner(new File(file));
            for(int i = 0; i < ctr; i = i+1){
                words[i] = s2.next();
        }
        return words;
        }

        catch (FileNotFoundException e) {

        }
            return null;
    }


    public static void main(String[] args) throws Exception 
    { 
        //String text = readString("C:\\Users\\herna\\Desktop\\Test.txt");
        //System.out.println(text);

        String[] words = readArray("C:\\Users\\herna\\Desktop\\ErdosCA.txt");
        int n = words.length;
        int [] arr = new int [n];

        PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
        for (int i=0; i < words.length; i = i + 1 )
        {
            arr[i] = Integer.parseInt(words[i]);

            //writer.println(words[i]);


    }

        //System.out.println(Arrays.toString(arr));
        randomShuffle(arr, n);
        writer.println(Arrays.toString(arr));
        //writer.println(Arrays.toString(words));
        //writer.println("Update*");
        writer.close();


    }

}
Stefan :

The program is also reproducable with small files, for example:

1
2
3

The program enters an endless loop when the last line of the file is empty. The bad part is this:

            Scanner s1 = new Scanner(new File(file));
            while (s1.hasNextLine())
            {
                ctr = ctr + 1;
                if (s1.hasNext())
                {
                    s1.next();
                }
            }

If you are before the empty line, then s1.hasNextLine() is true but s1.hasNext() is false. So you do not read the next element. Then in the next iteration of the loop, s1.hasNextLine() is still true, and thus the loop does never end.

By the way, you should never catch Exceptions without handling them. You should at least output an error message and then call e.printStackTrace().

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=422572&siteId=1