Knute Snortum wrote:Suppose you have a line of data like this:
California, Oregon, Washington
You have the names of three states in the US separated by a comma and a space. In this situation your separator would be ", ".
@Anam: Except that that's
not where the problems with CSVs end, because the data could be:
"Modesto, California", "Eugene, Oregon", "Edmonds, Washington"
ie, the data
includes the delimiter you want to use.
Which is just one reason that TAB-delimiting (which is also a form of "csv") is quite popular - because text data is far less likely to contain a TAB than a comma. Another one is a "vertical bar", viz:
Modesto, California|Eugene, Oregon|Edmonds, Washington
because it's quite "visual".
So, since it appears you're allowed to choose your own format, my suggestion would be to choose a delimiter that you know will never appear in your data - but
make sure you document it.
And just FYI, the statement to "split" the line above would be something like:
String[] fields = line.split("[|]");
but you really should read up on the method because there's quite a lot to know about it.
1) The user is allowed to input a csv or a txt file....
OK, well the format of a CSV file is quite well documented, and if you stick to something simple, like a vertical bar (
and -
very important - assume that the data won't contain it), you should be able to handle it quite easily; otherwise, as I said, csv parsing can be quite tricky.
But what's a "txt" file? In general, all it means is that it could contain
any kind of text - although it usually means 7-bit ASCII - but it doesn't tell you anything about the format.
So, unless you KNOW what the format will be in advance, it'll be impossible to break it into anything but "lines".
Do you see the problem? You need to understand the
parameters of your problem before you can solve it.
HIH
Winston