简体   繁体   中英

read from txt.file and print out its 2d digits using java

I am new in java and i am writing a simple program to read a file and print out only digits. so, if there are some text, the program should omit them. But in my program if there are some texts or blank line, it stores in array 0; Here is my code

import java.io.*;
import java.util.*;

public class FileR {
    public static void main(String[] args) throws Exception {
        int[][] desktop;
        int rows = getTotalRow();
        desktop = new int[rows][];
        InputStream is;
        is = new FileInputStream("hello.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = "";
        try {
            int row = 0;

            while ((line = br.readLine()) != null) {
                String[] nums = line.split("\\s*,\\s*");
                desktop[row] = new int[nums.length];
                for (int col = 0; col < nums.length; col++) {
                    nums[col] = nums[col].replaceAll("[^0-9]", "");
                    if (!(nums[col].isEmpty())) {
                        int n = Integer.parseInt(nums[col].trim());
                        desktop[row][col] = n;

                    }


                }
                row++;
            }
            print(desktop);

        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }

    public static int getTotalRow() throws IOException {
        LineNumberReader reader = null;
        try {
            reader = new LineNumberReader(new FileReader("hello.txt"));
            while ((reader.readLine()) != null) ;
            return reader.getLineNumber();
        } catch (Exception ex) {
            return -1;
        } finally {
            if (reader != null)
                reader.close();
        }
    }


    public static void print(int[][] value) {
        for (int i = 0; i < value.length; i++) {
            for (int j = 0; j < value[i].length; j++) {
                System.out.print(value[i][j] + " ");

            }
            System.out.println();
        }
    }
}

my txt file:

tntnynm
thttn
rrbr


{2, 2, 2, 2},
{0}
{2, 0, 0, 0, 0, 0, 0, 0, 0, 2},
{2, 0, 1, 0, 3, 0, 4, 0, 0, 2},
{2, 0, 0, 0, 3, 0, 4, 0, 0, 2},
{2, 0, 0, 0, 3, 0, 4, 2},
{2, 0, 0, 0, 0, 0, 0, 0, 0, 2},
{2, 0, 0, 0, 0, 0, 0, 0, 0, 2},
{2, 0, 0, 0, 0, 0, 0, 0, 0, 2},
{2, 0, 0, 0, 0, 0, 0, 0, 0, 2},
{2, 0, 0, 0, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
kjnevlknv
dkvlkdv
kldvlkn

the output should prints out only its 2d array containing digits and ignoring the texts, but instead of that it prints out 0 like below:

   0 
0 
0 
0 
0 
2 2 2 2 
0 
2 0 0 0 0 0 0 0 0 2 
2 0 1 0 3 0 4 0 0 2 
2 0 0 0 3 0 4 0 0 2 
2 0 0 0 3 0 4 2 
2 0 0 0 0 0 0 0 0 2 
2 0 0 0 0 0 0 0 0 2 
2 0 0 0 0 0 0 0 0 2 
2 0 0 0 0 0 0 0 0 2 
2 0 0 0 0 0 2 
2 2 2 2 2 2 2 2 2 2 
0 
0 
0 

Unfortunately, i have no idea how to fix them, please help PS Let me know if you have any question

You have an int (primitive type) array. So, array values are zero at the beginning.

You can make desktop an Integer [][] and when printing, skip null values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM