reading tab delimited textfile java

20,618

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());
    }
Share:
20,618

Related videos on Youtube

Wayne
Author by

Wayne

Updated on August 21, 2020

Comments

  • Wayne
    Wayne over 3 years
    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);
            } 
    
    • EarlGrey
      EarlGrey over 10 years
      Do you know, what the break statement does?
    • AurA
      AurA over 10 years
      and why are you using parseInt ? that is for getting int value out of a string.
    • Link19
      Link19 over 10 years
      there's obviously meant to be a number on the first line for whatever reason. There's no where i can see in the cod eyou have posted that would result in an array index out of bounds. Can you post more code or the stack trace?
    • EarlGrey
      EarlGrey over 10 years
      And it would be helpfull, if you say which function call throws the ArrayIndexOutOfBounds exception.
    • Rakesh
      Rakesh over 10 years
      Can you post the stacktrace ? I mean the answer is pretty straightforward, but nobody can see a reason for ArrayIndexOutOfBoundException
    • Wayne
      Wayne over 10 years
      @EarlGrey i want the scanning to stop after i get a string of ccc. srry its not aaa. im trying to get the value of 10 and skip it when i do the reading.
  • Marc
    Marc over 10 years
    Well I'm just following the example format, but yes you're right.
  • Link19
    Link19 over 10 years
    How do either of you know any of this, you don't know what he's trying to achieve. And none of it answers the question!
  • Rakesh
    Rakesh over 10 years
    He wants to read "tab delimited text file" That is what it says in the title
  • Marc
    Marc over 10 years
    Yes Glen is right I modified my answer What I think is that the array Object is some kind of custom class based on an array. This undelying array is initialized with the first integer. The add function takes the String array split and add every element of it in the array. OR he tries to add 12 elements in an array of lenght 10.
  • EarlGrey
    EarlGrey over 10 years
    @Rakesh That's right, but there seems to be more then just reading. Why does he want to skip some lines? Where does the exception come from?
  • Rakesh
    Rakesh over 10 years
    @EarlGrey Yes, because he has not posted the complete code, neither the stacktrace