12 Replies - 11149 Views - Last Post: 18 February 2012 - 06:18 AM Rate Topic: -----

#1 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

reading in a text file and skipping blank lines?

Posted 17 February 2012 - 11:03 AM

I'm using a scanner to read in a text file and I want it to skip any blank lines it sees and go on to the next one. here's what i have so far

       
            while (!scanner.nextLine().equals("\n")){
                String[] columns = scanner.nextLine().split(" = ");



this is still giving me problems when it encounters a blank line.. the text file looks like this
Hello
There
//this is the blank line i want to skip
Soup



Is This A Good Question/Topic? 0
  • +

Replies To: reading in a text file and skipping blank lines?

#2 thanny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-January 12

Re: reading in a text file and skipping blank lines?

Posted 17 February 2012 - 11:22 AM

use buffered reader instead, which has a method called readLine, which essentially reads a line, if there's nothing in it i believe it returns null, so you can check the return value to decide whether or not to skip the line.
Was This Post Helpful? 0
  • +
  • -

#3 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: reading in a text file and skipping blank lines?

Posted 17 February 2012 - 11:27 AM

Ok so they way you have it setup now is that it will actually stop if it hits a blank line. Read in the line, check to see if it is null, if not then performed code else do nothing

/* Changed this to an arraylist since you do not know how man lines there will be. Use the arrayListName.add() method to insert into ArrayList*/
ArrayList<String> columns = new ArrayList<String>();
String line; // variable to hold the line
while(/* While scanner has next line */)
{
    line = scanner.nextLine();
    if( /* line is not equal to null */)
        /* perform code */
}

Make Sense?

Edit - thanny you do not need to switch to bufferedReader Scanner will do it perfectly fine and most people find Scanner easier to use anyways. Both will accomplish it so no reason to switch over to BuferedReader

This post has been edited by Fuzzyness: 17 February 2012 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1400
  • View blog
  • Posts: 3,108
  • Joined: 05-April 11

Re: reading in a text file and skipping blank lines?

Posted 17 February 2012 - 12:04 PM

When you are using the Scanner, it doesn't include the line separator in the String returned by nextLine().

Also you are reading in two lines, one for testing if it is a blank line, and then you read the next line. So you are actually skipping a line.

		Scanner scanner = new Scanner(string);
		
		while (scanner.hasNextLine()) {
			String line = scanner.nextLine();
			
			if (!line.equals("")) {
				System.out.print(line);
			}
		}



Also the nextLine() will never return null (Fuzzyness)

This post has been edited by CasiOo: 17 February 2012 - 12:05 PM

Was This Post Helpful? 1
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: reading in a text file and skipping blank lines?

Posted 17 February 2012 - 12:09 PM

Yeah sorry, I say compare it to null and I know it is a String so in my head I know I have to compare it to "" not null; but he might not know what I meant by it though. Should have been more clear, thank you for clearing that up! :^:
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2733
  • View blog
  • Posts: 11,513
  • Joined: 20-September 08

Re: reading in a text file and skipping blank lines?

Posted 17 February 2012 - 02:57 PM

You might want to remove and superfluous whitespace too

String line = scanner.nextLine().trim();

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: reading in a text file and skipping blank lines?

Posted 17 February 2012 - 03:01 PM

String line = scanner.nextLine().trim();
if(line.length() == 0) ignore it
Was This Post Helpful? 0
  • +
  • -

#8 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: reading in a text file and skipping blank lines?

Posted 17 February 2012 - 04:06 PM

Sorry for the late reply guys but I've been trying each of your suggestions and I can't seem to get it to work with the way I have it set up. Here's my code right now
Scanner scanner = new Scanner(new FileReader("testing.txt"));
while (scanner.hasNextLine()){
         String[] columns = scanner.nextLine().split(" = ");
         try{



i have a try catch right after taking in the next line and splitting the line into 2 columns. the txt file looks like this
A = 10
B = 9
C = 3
//etc.



the way you guys showed me is with a String, but I have 2 columns of string arrays. I'm getting really frustrated at this lol.. is there a way to do it when i have an array instead of just a string?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: reading in a text file and skipping blank lines?

Posted 17 February 2012 - 04:15 PM

You will have to split your 2 operations if you want to ignore blank lines
Scanner scanner = new Scanner(new FileReader("testing.txt"));
while (scanner.hasNextLine()){
         String line = scanner.nextLine().trim();
         if(line.length() == 0) continue;
          String[] columns = scanner.nextLine().split(" = ");
         if(columns.length != 2)  continue;
         try{



Was This Post Helpful? 0
  • +
  • -

#10 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: reading in a text file and skipping blank lines?

Posted 17 February 2012 - 04:30 PM

View Postpbl, on 17 February 2012 - 04:15 PM, said:

You will have to split your 2 operations if you want to ignore blank lines
Scanner scanner = new Scanner(new FileReader("testing.txt"));
while (scanner.hasNextLine()){
         String line = scanner.nextLine().trim();
         if(line.length() == 0) continue;
          String[] columns = scanner.nextLine().split(" = ");
         if(columns.length != 2)  continue;
         try{



Sorry I'm kinda new at this but what's the point of "String line = scanner.nextLine().trim()"? Right now al i have is that "String[] columns scanner.nextLine().split(" = ");". If i initialized that String line, what do i do with it?
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: reading in a text file and skipping blank lines?

Posted 17 February 2012 - 04:53 PM

You can verify:
1) that is not empty (that was your original post question)
2) that it is valid, 2 String seperated by a = sign
Was This Post Helpful? 0
  • +
  • -

#12 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: reading in a text file and skipping blank lines?

Posted 17 February 2012 - 05:04 PM

View Postpbl, on 17 February 2012 - 04:53 PM, said:

You can verify:
1) that is not empty (that was your original post question)
2) that it is valid, 2 String seperated by a = sign

ah ok i understand what you're saying now.. the line worked I believe. my program is still getting a bunch of errors but that's a different issue... I think it's time to give up after my 20 hours in this program.. thanks for everyone's help
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2733
  • View blog
  • Posts: 11,513
  • Joined: 20-September 08

Re: reading in a text file and skipping blank lines?

Posted 18 February 2012 - 06:18 AM

Quote

String[] columns = scanner.nextLine().split(" = ");


should be

String[] columns = line.split("\\s*=\\s*");

This post has been edited by g00se: 18 February 2012 - 06:19 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1