Javanoob33
Javanoob33

Reputation: 89

java Removing a line with a certain element from text file

I am trying to remove all lines with 0.0 as its printing to a text file.

This is what it outputs,

4177516040935072 14.94

4270931484327447 7.54

4308546496189733 7.1

4421407386000242 0.0

4436391685645089 0.0

This is what I want it to output

4177516040935072 14.94

4270931484327447 7.54

4308546496189733 7.1

This is my code:

Collection<Passenger> c = passengers.values();
        Iterator<Passenger> itr = c.iterator();
 

        try {
                
            while (itr.hasNext()) { 
        
                //System.out.println(itr.next().getBillAmount());
                BufferedWriter writer = new BufferedWriter(new FileWriter(fileOutput,true));
            writer.append(itr.next().toString());
             writer.newLine();
             writer.close();
            }
            
            
            
            
        } catch (IOException e3) {
            e3.printStackTrace();
        }
        
    }

Upvotes: 1

Views: 186

Answers (6)

Gregosaurus
Gregosaurus

Reputation: 611

You can try this :

 while (itr.hasNext()) { 
        if (!itr.next().toString().contains(" 0.0"))
        {
          //System.out.println(itr.next().getBillAmount());
          BufferedWriter writer = new BufferedWriter(new FileWriter(fileOutput,true));
          writer.append(itr.next().toString());
          writer.newLine();
          writer.close();
        }
 }

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94469

Split the value and check if any of the tokens contain 0.0. Also declare the BufferedWriter outside of the loop and also close it outside of the loop. Also use the write() method on BufferedWriter instead of append().

        BufferedWriter writer = new BufferedWriter(new FileWriter(fileOutput,true));
        while (itr.hasNext()) { 

            List<String> values = Arrays.asList(itr.next().toString().split(" "));

            if(!values.contains("0.0")){
                writer.write(itr.next().toString());
                writer.newLine();
            }
         }
         writer.close();

Upvotes: 1

user2366923
user2366923

Reputation:

I prefer :

    if (!String.Contains(" 0.0") {
          System.Out.println(...);
    }

with the reason that if we use endsWith(); maybe we have a space at the end of the string

Upvotes: 1

Dropout
Dropout

Reputation: 13866

int i = 0;
for(String line : lines)
{
    for(String word : line.split(" "))
    {
        if(word).equals("0.0")
        {
            lines.remove(i);
        }
    }
    i++;
}

You can use simply

if(line.contains("0.0"))
{
    lines.remove(i);
}

if it can be anywhere in the line. Or

if(line.endsWith("0.0"))
{
    lines.remove(i);
}

if it's always on the end of the line.

Upvotes: 0

NickJ
NickJ

Reputation: 9559

The other answers will work, but it would be best if you have access to the source code for the Passenger class. If the passenger class has 2 fields which both get written to the String result in toString, then it would be best to access the field whose value to want to check via its getter.

I have also re-arranged your code a bit so that you are not closing and re-opening your BufferedWriter every line

Example:

BufferedWriter writer = new BufferedWriter(new FileWriter(fileOutput,true));

while (itr.hasNext()) { 
    Passenger p = itr.next();
    if (p.getSomeField() != 0.0) {
      writer.append(p.toString());
      writer.newLine();
    }
}

writer.close();

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 209002

You can use String.endsWith() method

if (!String.endsWith(" 0.0") {
    // do something
}

Upvotes: 0

Related Questions