using scanner to read file but skip blank lines into a 2d array

前端 未结 2 1911
日久生厌
日久生厌 2021-01-28 04:01

I am struggling to use scanner class to read in a text file while skipping the blank lines.

Any suggestions?

   Scanner sc = new Scanner(new BufferedRead         


        
相关标签:
2条回答
  • 2021-01-28 04:50

    Try adding this to your code:

    sc.skip("(\r\n)");
    

    It will ignore blank lines. For More information: Scanner.skip()

    0 讨论(0)
  • 2021-01-28 05:01
    while(sc.hasNextLine()) {
        for (int i=0; i<trainingData.length; i++) {
            String[] line = sc.nextLine().trim().split(" ");
    

    You can't just check the scanner once to see if it has data and then use a loop to read the lines of data. You can't assume that you have 48 lines of data just because you define your array to hold 48 lines of data.

    You need to go back to the basics and learn how to read data from a file one line at a time and then you process that data.

    Here is a simple example to get you started:

    import java.util.*;
    
    public class ScannerTest2
    {
        public static void main(String args[])
            throws Exception
        {
            String data = "1 2\n\n3 4\n\n5 6\n7 8";
    
                // First attempt
    
            System.out.println("Display All Lines");
            Scanner s = new Scanner( data );
    
            while (s.hasNextLine())
            {
                String line = s.nextLine();
                System.out.println( line );
            }
    
                // Second attempt
    
            System.out.println("Display non blank lines");
            s = new Scanner( data );
    
            while (s.hasNextLine())
            {
                String line = s.nextLine();
    
                if (line.length() != 0)
                {
                    System.out.println( line );
                }
            }
    
                // Final attempt    
    
            String[][] values = new String[5][2];
            int row = 0;
    
            System.out.println("Add data to 2D Array");
            s = new Scanner( data );
    
            while (s.hasNextLine())
            {
                String line = s.nextLine();
    
                if (line.length() != 0)
                {
                    String[] digits = line.split(" ");
                    values[row] = digits;
                    row++;
                }
            }
    
            for (int i = 0; i < values.length; i++)
                System.out.println( Arrays.asList(values[i]) );
        }
    }
    

    The example uses a String variable to simulate data from a file.

    The first block of code is how you simply read all lines of data from the file. The logic simply:

    1. invokes the hasNextLine() method so see if there is data
    2. invokes the nextLine() method to get the line of data
    3. display the data that was read
    4. repeats steps 1-3 until there is no data.

    Then next block of code simply adds an "if condition" so that you only display non-blank data.

    Finally the 3rd block of code is closer to what you want. As it reads each line of data, it splits the data into an array and then adds this array to the 2D array.

    This is the part of code you will need to change. You will need to convert the String array to an double array before adding it to your 2D array. So change this code first to get it working. Then once this works make the necessary changes to your real application once you understand the concept.

    Note in my code how the last row displays [null, null]. This is why it is not a good idea to use arrays because you never know how big the array should be. If you have less that 5 you get the null values. If you have more than 5 you will get an out of bounds exception.

    0 讨论(0)
提交回复
热议问题