ayhan
ayhan

Reputation: 23

How to delete this comma, in a txt file with java, but let this "," comma stay?

Hey Friends i m at a java code to replace commas from a text. I Just want to delete all the usual commas, and let this kind of a comma "," stay.

I start a little bit to code it. But its a little bit frustrated.

Try it with a if and else but doenst work.

public class converter{


   public static void main(String[] args) throws IOException {

       BufferedReader br = new BufferedReader(new FileReader("C://USers//Sei_Erfolgreich//Desktop//convert.txt"));
       String akhi = br.readLine();


       if(akhi.contains(" \",\"")) {

       }else
           akhi.replaceAll(",", "");
   }

}

Upvotes: 1

Views: 660

Answers (4)

ayhan
ayhan

Reputation: 23

This one here works very well

public class converter{

   public static void main(String[] args) throws IOException {
       try {
            BufferedReader br = new BufferedReader(
                    new FileReader("C:/Users/Sei_Erfolgreich/Desktop/convert.txt"));
            String zeile;

            try {
                File newTextFile = new File("C:/Users/Sei_Erfolgreich/Desktop/convert2.txt");
                FileWriter fw = new FileWriter(newTextFile);
                while ((zeile = br.readLine()) != null) {
                    zeile = zeile.replaceAll("\",\"", "\uffff").replaceAll(",", "").replaceAll("\uffff", "\",\"");
                    System.out.println(zeile);
                    fw.write(zeile);
                }

                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
   }
  }

Upvotes: 0

ayhan
ayhan

Reputation: 23

So like this ?

  import java.io.*;

   public class converter{

   public static void main(String[] args) throws IOException {

       BufferedReader br = new BufferedReader(new FileReader("C://USers//Sei_Erfolgreich//Desktop//convert.txt"));
       String akhi = br.readLine();

       akhi = akhi.replaceAll("\",\"", "\uffff")
               .replaceAll(",", "")
               .replaceAll("\uffff", "\",\"");

       File file = new File("C://Users//Sei_Erfolgreich//Desktop//convert2.txt");
       FileWriter fileWriter = new FileWriter(file);
       BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
       bufferedWriter.write(akhi);
       bufferedWriter.close();
   }

Upvotes: 0

Aleksandr Dubinsky
Aleksandr Dubinsky

Reputation: 23505

Read up on regular expressions (supported by String.replaceAll) here.

For example, you can use negative lookahead and lookbehind. These let you specify the rule "match a comma, but not one preceded or followed by a quotation mark." It will look something like this:

string = string.replaceAll("(?<!\"),(?!\")", "");

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533492

One work around is to map the characters you want to keep to something else temporarily.

akhi = akhi.replaceAll("\",\"", "\uffff")
           .replaceAll(",", "")
           .replaceAll("\uffff", "\",\"");

The character \uffff is invalid by definition and shouldn't occur naturally.

Upvotes: 2

Related Questions