简体   繁体   中英

Split parse text file in java

So basically I could read the text file, using BUFFERED READER. After that I Should SPLIT and PARSE it, at this point, I am facing a problem.

Output is: 5 2 9 1 7 3 9 2 10 11 6 3
Exception in thread "main" java.lang.NumberFormatException: For input string: "F:\\Gephi\\number.txt" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at javaapplication17.JavaApplication17.main(JavaApplication17.java:42) C:\\Users\\Asus\\AppData\\Local\\NetBeans\\Cache\\8.2\\executor-snippets\\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

MY CODE IS

package javaapplication17;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;




public class JavaApplication17 {

    public static void main(String[] args) {

        String count = "F:\\Gephi\\number.txt";

        try (BufferedReader br = new BufferedReader(new FileReader(count))) 
        {
            String line;
            while ((line = br.readLine()) != null)
            {
                System.out.println(line);
            }
        } 

        catch (IOException e) 
        {
            e.printStackTrace();

        }


        // What is the Problem in this part? //

        String[] abc = count.split("/t");
        int[] intArray = new int[abc.length];

        for (int i=0; i<abc.length; i++)

        {

        String numberAsString = abc[i];
        intArray[i] = Integer.parseInt(numberAsString);


        }

        System.out.println("Number of integers: " + intArray.length);

        System.out.println("The integers are:");
        for (int number : intArray)

        {

        System.out.println(number);

        }






    }
}

My text file looks like this

5   6   7
5   2   9
1   7   3
9   2   10
11  6   3

The problem is count is a String that contains the value "F:\\\\Gephi\\\\number.txt" . This does not give you the number of rows/columns in the file.

String[] abc = count.split("/t");  // <------ This won't work

So when you do

int[] intArray = new int[abc.length];

you are creating an array with size of abc that is split by \\t which is incorrect.


To solve you issue you can change the try-catch block to the following :

int countOfNum = 0;
try (BufferedReader br = new BufferedReader(new FileReader(count))) {
    String line;
    while ((line = br.readLine()) != null) {
        String[] currLine = line.split("\t");
        System.out.println(Arrays.toString(currLine));
        countOfNum = countOfNum + currLine.length;
    }
} catch (IOException e) {
    e.printStackTrace();
}

System.out.println("Total count of numbers : " +countOfNum);
  • In the above solution we split each line into the variable currLine
  • Next using the Arrays.toString() we print out the values
  • Now using the variable countOfNum we find the total number of elements in the file.

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