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());
}
Related videos on Youtube
Author by
Wayne
Updated on August 21, 2020Comments
-
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 over 10 yearsDo you know, what the
break
statement does? -
AurA over 10 yearsand why are you using parseInt ? that is for getting int value out of a string.
-
Link19 over 10 yearsthere'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 over 10 yearsAnd it would be helpfull, if you say which function call throws the ArrayIndexOutOfBounds exception.
-
Rakesh over 10 yearsCan you post the stacktrace ? I mean the answer is pretty straightforward, but nobody can see a reason for
ArrayIndexOutOfBoundException
-
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 over 10 yearsWell I'm just following the example format, but yes you're right.
-
Link19 over 10 yearsHow 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 over 10 yearsHe wants to read "tab delimited text file" That is what it says in the title
-
Marc over 10 yearsYes 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 arraysplit
and add every element of it in the array. OR he tries to add 12 elements in an array of lenght 10. -
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 over 10 years@EarlGrey Yes, because he has not posted the complete code, neither the stacktrace