Re: read and show CSV-files
Jeff Higgins wrote:
Tobias Wendorff wrote:
Hi there,
I'm into JAVA programming for some days only, but I've read
many tutorials and documents. It's very great, but my cell-phone
(it's a Siemens S65) can't view all the multi-media things :-(
I would like to read the content of a CSV-file and print it out
on the screen. I don't care, it's embedded in the midlet or
on the mobile itself. I know how to handle this :-)
The CSV looks like this:
surname;firstname;street
Doe;John;nobody knows
I would like to see this on the mobile:
Firstname: John
Surname: Doe
Street: nobody knows
Does anybody have an example for this? I don't want a finished
code - but of course, I wouldn't say no :-)
Best regards,
Tobias
Commented stuff I'm not real sure about.
//Connection c =Connector.open(file://records.dat);
//DataInputStream dis = new DataInputStream(c.openInputStream());
//String data = dis.readUTF();
String[] records = data.split("\n");
String[] fields = new String[3];
for(int i=0;i<records.length;i++)
{
fields = records[i].split(";");
}
//Form.append("Firstname: " + fields[1]);
//Form.append("Surname: " + fields[0]);
//Form.append("Street: " + fields[2]);
Oops! no String.split method!
import java.util.Vector;
public class testString
{
public static void main(String[] args)
{
String data =
"Doe;John;Bywater Road" +
"\n" +
"Atkins;Rae Dawn;Tinpan Alley" +
"\n" +
"Phillips;Michelle;Bright Boulevard";
String[] records = split(data, "\n");
for(int ri = 0; ri < records.length; ri++)
{
String[] fields = split(records[ri], ";");
System.out.println("Firstname: " + fields[1]);
System.out.println("Surname: " + fields[0]);
System.out.println("Street: " + fields[2] + "\n");
}
}
// Split method from Substance
//
<http://forum.java.sun.com/thread.jspa?threadID=646861&messageID=3809007>
static private String[] split(String original, String separator)
{
Vector nodes = new Vector();
int index = original.indexOf(separator);
while(index>=0)
{
nodes.addElement( original.substring(0, index) );
original = original.substring(index+separator.length());
index = original.indexOf(separator);
}
nodes.addElement( original );
String[] result = new String[ nodes.size() ];
if( nodes.size()>0 )
{
for(int loop=0; loop<nodes.size(); loop++)
result[loop] = (String)nodes.elementAt(loop);
}
return result;
}
}