Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading tab delimited textfile java

10
aaa aaa aaa
bbb bbb bbb
ccc ccc ccc
ddd ddd ddd

I have a textfile that Im trying to read with tab delimiters. whenever i read the file, i get an arrayindexoutofbound error after the 10. i search online and found that i have to add a -1 behind the \t but i still get the same error.

 try{
        Scanner scan = new Scanner(new File("1.txt"));
        String line="";
        int readline = Integer.parseInt(scan.nextLine());//

        while (scan.hasNextLine())
        {
            line = scan.nextLine();

            if(line.equals("ccc"))
            {  
                break;
            }
        String[] split=line.split("\t");

            array.add(split);
        } 
like image 905
Wayne Avatar asked Jun 18 '26 15:06

Wayne


1 Answers

If you are use Scanner here no need to split, you can use next() here as follows

    Scanner sc=new Scanner(new FileReader("D:\\test.txt"));
    while (sc.hasNextLine()){
        System.out.println(sc.next());
    }
like image 186
Ruchira Gayan Ranaweera Avatar answered Jun 20 '26 04:06

Ruchira Gayan Ranaweera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!