Author Topic: A couple Java questions  (Read 3061 times)

royalmurder

  • *
  • Posts: 61
    • View Profile
    • Ian Merryweather
A couple Java questions
« on: 2011-12-05 12:09:18 »
Hey guys,

I got bored of trawling Google attempting to find succint answers to these questions, and my lecturer seems incapable of replying to his emails so....
1:- would fileStream.split(","); be sufficient to get words out of a .csv file? if not, what is the most painless way of doing so?

2:- what would be the most painless way of getting data out of an xml file?

in both of these cases, I need to assign the words to a String, inside a for loop.

Thanks for the help (;

Bosola

  • Fire hazard!
  • *
  • Posts: 1752
    • View Profile
    • My YouTube Channel
Re: A couple Java questions
« Reply #1 on: 2011-12-05 18:53:23 »
Firstly - just because CSV stands for 'comma separated values' doesn't mean the values are *actually* separated by commas. There's all sorts of delimiters - common alternatives include full stops, tabs, carriage returns and semicolons.

If you want to handle CSV files, there should already be a library around to do it. As for fileStream.split(","), I don't know enough about Java to tell you if this is the best method (in both senses) available.

Secondly, for XML, I'd probably likewise find a library. Failing that, it depends how I'd expect to seek data in the file. You could probably model an XML file like a list of lists of dictionaries (key / value pairs). But seeking the resultant data might not be tremendously efficient.

Thirdly, IIRC, the syntax of the for-each loop in Java is
Code: [Select]
for (String x: myList) {
    someObject.someMethod(x);
}

royalmurder

  • *
  • Posts: 61
    • View Profile
    • Ian Merryweather
Re: A couple Java questions
« Reply #2 on: 2011-12-06 11:59:04 »
cheers. I was hoping there would be an easier method, but I guess using an external library is pretty much as easy as it gets. thanks :D

nfitc1

  • *
  • Posts: 3011
  • I just don't know what went wrong.
    • View Profile
    • WM/PrC Blog
Re: A couple Java questions
« Reply #3 on: 2011-12-06 13:24:20 »
I work with java as a profession (right now) so I can give a little advice if it's not too late.

1. A java.io.BufferedReader object can read data line-by-line. Once you have that data br.readLine() will actually output a single string terminated by /l, /r, or /n. Once you have that string you can .split(",") them up and get all the words you need:

Code: [Select]
import java.io.*;

class FileRead
{
public static void main(String args[])
{
try
{
FileReader freader = new FileReader(<filename>);
BufferedReader br = new BufferedReader( freader );
String strLine;

while ()
{
strLine = br.readLine();
strLine.split(","); //do whatever you need with this.
}
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
finally
{
try
{
freader.close();
}
catch( IOException ioe )
{
System.err.println("Error: " + ioe.getMessage());
}
try
{
br.close();
}
catch( IOException ioe )
{
System.err.println("Error: " + ioe.getMessage());
}
}
}
}

I probably just did a problem on your homework for you, didn't I? ;)

As for you second question, I am currently working on a method to do just that. Definitely look into SAX/StAX readers. They are probably the easiest XML readers to use. I'll draw your attention to the XMLStreamReader to start you off. It's up to you to do what you need after that.

royalmurder

  • *
  • Posts: 61
    • View Profile
    • Ian Merryweather
Re: A couple Java questions
« Reply #4 on: 2011-12-06 13:58:11 »
I probably just did a problem on your homework for you, didn't I? ;)

close, but no cigar.
I'm responsible for the file input section of a group project, and the module lecturer just assumes we're much better at java than we are, and doesn't provide any help... and the java lecturer, as I said, is very uncommunicative XD

cheers though! any help is appreciated :D and, as I prefer writing my "own" code, this is great.
just a question though; by leaving the while block with no condition, you are ensuring that the catch method is called?

nfitc1

  • *
  • Posts: 3011
  • I just don't know what went wrong.
    • View Profile
    • WM/PrC Blog
Re: A couple Java questions
« Reply #5 on: 2011-12-06 17:36:00 »
...by leaving the while block with no condition, you are ensuring that the catch method is called?

Yes. Most readers don't have an "at end of stream" determinate so you either have to keep track of the size or use some fuzzy logic to figure it out when it happens. An IOException occurs when you try to .readline() past the Reader's stream so the catch will catch that. What you could do instead is have multiple catches:

Code: [Select]
catch (IOException ioe)
{
if ( ioe instanceof FileNotFoundException )
{
//Can be thrown by the FileReader constructor and will be caught by the IOException catch since it's a subclass
//This needs to be handled
}
else
{
//Everything should be fine otherwise.
}
}
catch (Exception e)
{
//Any other exceptions
System.err.println("Error: " + e.getMessage());
}

That way any "bland" IOException will signal the end of the stream while any other exception gets caught and you're not ignoring any important exceptions.