read from a file
How do I read this : 14.05.2007 from a file and display it
14
05
2007?
Thank you!
How do I read this : 14.05.2007 from a file and display it
14
05
2007?
Thank you!
> How do I read this : 14.05.2007 from a file and
> display it
> 14
> 05
> 2007?
>
> Thank you!
1) Read the data from the file as a string.
2) Split the string into elements at the periods.
3) Loop through the elements, displaying each element on a new line.
~
> > How do I read this : 14.05.2007 from a file and
> > display it
> > 14
> > 05
> > 2007?
> >
> > Thank you!
>
> 1) Read the data from the file as a string.
> 2) Split the string into elements at the periods.
> 3) Loop through the elements, displaying each element
> on a new line.
>
> ~
It must be opposite day, yawmark ;-)
Try:
1) Read the data from the file as a string.
2) For each line append the string ' "." + line ' to a string.
3) display the result.
> It must be opposite day, yawmark ;-)
?
~
> Try:
>
> 1) Read the data from the file as a string.
> 2) For each line append the string ' "." + line ' to a string.
> 3) display the result.
I don't see how that would read "14.05.2007" from a file and display it "14\n05\n2007". Although this brings to mind a replaceAll() solution (which probably wouldn't be a good suggestion, given that the OP is confused about split())...
~
> I don`t know how split works :D
http://java.sun.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)
Read that, try it out, and let us know if you have a specific question.
~
String tmp = br.readLine();
while(tmp !=null){
String[] ss=tmp.split(".");
System.out.println(ss[0]);
System.out.println(ss[1]);
System.out.println(ss[2]);
}
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
> String tmp = br.readLine();
>while(tmp !=null){
> String[] ss=tmp.split(".");
>System.out.println(ss[0]);
> System.out.println(ss[1]);
>System.out.println(ss[2]);
>
>
>
> Exception in thread "AWT-EventQueue-0"
> java.lang.ArrayIndexOutOfBoundsException: 0
Do some simple debugging:
String tmp = br.readLine();
System.out.println("Here's what I read and am trying to split:" + tmp);
Also re-read that documentation about split. It takes a regular expression, not just a string verbatim. A dot "." means something special.
> String[] ss=tmp.split(".");
The "." is a regular expression meaning "any character". When you split on any (well, every) character, you end up with an empty string array, which is why you get the ArrayIndexOutOfBounds. You'll need to 'escape' the period, like so:
String[] ss=tmp.split("\\.");
~