Ignoring comment line in text file while being read

Ignoring comment line in text file while being read

Page 1 of 1

12 Replies - 7639 Views - Last Post: 09 August 2010 - 05:10 PM Rate Topic: -----

#1 Zibang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-August 10

Ignoring comment line in text file while being read

Posted 07 August 2010 - 03:18 PM

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

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

} 
 


The text file being read...

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

Is This A Good Question/Topic? 0
  • +

Replies To: Ignoring comment line in text file while being read

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Ignoring comment line in text file while being read

Posted 07 August 2010 - 03:30 PM

Haven't tested it, but try:
if(line.trim().charAt(0) == '#')
{
  continue;
}



The continue statement will skip the rest of the loop, so it should do the trick...and charAt(0) should take the first character which according to you should be "#".
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: Ignoring comment line in text file while being read

Posted 07 August 2010 - 03:31 PM

if(line.trim().startsWith("#"));
{} 



Is that a typo or how it is actually in your editor? The semi colon afterward negates the if check, nothing will happen. If it wasn't there the immediate brackets with zero contents also does "nothing".
Was This Post Helpful? 0
  • +
  • -

#4 Zibang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-August 10

Re: Ignoring comment line in text file while being read

Posted 07 August 2010 - 04:34 PM

View PostKYA, on 07 August 2010 - 02:31 PM, said:

if(line.trim().startsWith("#"));
{} 



Is that a typo or how it is actually in your editor? The semi colon afterward negates the if check, nothing will happen. If it wasn't there the immediate brackets with zero contents also does "nothing".


that was a typo... and I thought since the startsWith # it will do nothing and skip to the next line
Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Ignoring comment line in text file while being read

Posted 07 August 2010 - 04:41 PM

Quote

and I thought since the startsWith # it will do nothing and skip to the next line


No, it will simply fall through to the next statement.

What you want is:

if (line.trim().startsWith("#"))
{
    continue; // continue while() loop
}


Was This Post Helpful? 1
  • +
  • -

#6 Zibang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-August 10

Re: Ignoring comment line in text file while being read

Posted 07 August 2010 - 05:05 PM

Figured it out
  while((line2=reader.readLine())!=null)
         {
        
             if(line2.trim().startsWith("#"))
                {
                }
                else
                {
                  String[] sLine = line2.trim().split("\t");
                  String name = sLine[1];
                  String species = sLine[2];
     
                  addAnimal( new Animal(name,species,this) );  
               }
         }
          
       reader.close();
       System.out.println("*** Finished Reading "+fname+" ***");


Just needed to put the rest of the code below in an else
Thank you for your help
Was This Post Helpful? 0
  • +
  • -

#7 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Ignoring comment line in text file while being read

Posted 07 August 2010 - 05:22 PM

Better to use the "continue" keyword as it is better design ;)
Was This Post Helpful? 3
  • +
  • -

#8 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2045
  • View blog
  • Posts: 4,237
  • Joined: 11-December 07

Re: Ignoring comment line in text file while being read

Posted 08 August 2010 - 04:45 PM

There are two nice solutions to this. My favourite is the one suggested above:

if(line2.trim().startsWith("#")) {continue;}
//Here goes the rest of your processing.


Some people have an aversion to the continue keyword. They would code it like this:

if(!line2.trim().startsWith("#")) {
    //Here goes the rest of your processing.
}


Here I've used ! to avoid the empty block in your code.
Was This Post Helpful? 2
  • +
  • -

#9 YasuoDancez  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 135
  • Joined: 30-September 09

Re: Ignoring comment line in text file while being read

Posted 09 August 2010 - 02:01 AM

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


If you put this line inside your while loop you won't have to do two checks.
Here you are checking with the if and your while loop already does a check for line != null.
Was This Post Helpful? 0
  • +
  • -

#10 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2045
  • View blog
  • Posts: 4,237
  • Joined: 11-December 07

Re: Ignoring comment line in text file while being read

Posted 09 August 2010 - 03:44 AM

I'm not sure about that. From the look of his code, it seems he wants to handle the first line differently from the rest. i.e. print it out instead of creating an Animal object.
Was This Post Helpful? 0
  • +
  • -

#11 YasuoDancez  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 135
  • Joined: 30-September 09

Re: Ignoring comment line in text file while being read

Posted 09 August 2010 - 06:15 AM

If he wanted to handle it differently, then wouldn't it seem better to create different methods/decompose it more?
Was This Post Helpful? 0
  • +
  • -

#12 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2045
  • View blog
  • Posts: 4,237
  • Joined: 11-December 07

Re: Ignoring comment line in text file while being read

Posted 09 August 2010 - 06:36 AM

Maybe, but that's an entirely separate issue.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Ignoring comment line in text file while being read

Posted 09 August 2010 - 05:10 PM

View Postbcranger, on 07 August 2010 - 06:22 PM, said:

Better to use the "continue" keyword as it is better design ;)

and it saves one level of identetion avoiding a pair of {}
lot easier to read
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1