[hotjoe.com] HotJoe Java Help Forums
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Visit java.com
Please send email if you are having login problems - see the posts below for more info.
Hotmail and Yahoo! users - please see the Hotmail post or the Yahoo! post for information on lost emails.
Ignoring comments in a text file  XML
Forum Index » Java Programming
Author Message
Zibang

Newbie

Joined: 08/06/2010 19:50:24
Messages: 2
Offline

Ok my problem is ignoring comment lines which start with "#"
I have tried using the startsWith() method but it still reads the line cause an error

Example of my code below

/**
* Reads <tt>Animal</tt> data from a text file and adds them to the zoo. The
* format of the data is specified in the MyZoo coursework assignment.
*
* @param animal the Animal object to be added.
*/
public void readDataFromFile() throws Exception
{

// set up an owner for the FileDialog
JFrame jframe = new JFrame();
jframe.setVisible(true);
// use a Filedialog to select a file to read from
FileDialog fDialog = new FileDialog(jframe, "Read from", FileDialog.LOAD);
fDialog.setFile("*.txt");
fDialog.setDirectory(".");
fDialog.setVisible(true);
String fname = fDialog.getFile();
jframe.dispose();

File inFile = new File(fname);
BufferedReader reader = new BufferedReader(new FileReader(inFile));

String line = reader.readLine();
if(line !=null){System.out.println(line);}

while((line=reader.readLine())!=null)
{
if(line.trim().startsWith("#"));
{}

String[] sLine = line.trim().split("\t");

String name = sLine[1];
String species = sLine[2];

addAnimal( new Animal(name,species,this) );
}

reader.close()

}

and this is the text file content


import shipment of April 12, 2007
# tab separated data
Bird Golden Eagle Eddie
Mammal Tiger Tommy
Mammal Lion Leo
Mammal Elephant Eddie
Bird Parrot Polly
# last one next
Reptile Cobra Colin


stdunbar

Newbie
[Avatar]

Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline

I'm confused by your code. If you read a line that starts with a # character, you should just continue:



This should do what you want

Thanks for using the forums at hotjoe.com
[WWW] [Yahoo!] [ICQ]
 
Forum Index » Java Programming
Go to:   
Powered by JForum 2.1.9 © JForum Team
This site run by Scott Dunbar of Xigole Systems. © 2005-2011 - Scott Dunbar
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners
hotjoe.com, xigole.com, and Scott Dunbar have no affiliation with Oracle