read from a file

How do I read this : 14.05.2007 from a file and display it

14

05

2007?

Thank you!

[113 byte] By [denisaa] at [2007-11-27 11:13:06]
# 1

> 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.

~

yawmarka at 2007-7-29 13:58:47 > top of Java-index,Java Essentials,Java Programming...
# 2

> > 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.

jGardnera at 2007-7-29 13:58:47 > top of Java-index,Java Essentials,Java Programming...
# 3

I don`t know how split works :D

denisaa at 2007-7-29 13:58:47 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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())...

~

yawmarka at 2007-7-29 13:58:47 > top of Java-index,Java Essentials,Java Programming...
# 5

> 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.

~

yawmarka at 2007-7-29 13:58:47 > top of Java-index,Java Essentials,Java Programming...
# 6

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

denisaa at 2007-7-29 13:58:47 > top of Java-index,Java Essentials,Java Programming...
# 7

> 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.

warnerjaa at 2007-7-29 13:58:47 > top of Java-index,Java Essentials,Java Programming...
# 8

> 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("\\.");

~

yawmarka at 2007-7-29 13:58:47 > top of Java-index,Java Essentials,Java Programming...
# 9

It works

Thank you boys

denisaa at 2007-7-29 13:58:48 > top of Java-index,Java Essentials,Java Programming...
# 10

> It works

> Thank you boys

Gald it works for you. You're welcome, girl(?).

~

yawmarka at 2007-7-29 13:58:48 > top of Java-index,Java Essentials,Java Programming...