2

I try to remove a space into a string which contains a int type value.

  • I read a .csv file with the scanner methode.
  • I use a Class to set/get the data.
  • I format data into the setter of the class.

Input data example:

String Pu_ht = "1 635,90";

Basic Example:

/**
 * @param Pu_ht the Pu_ht to set
 */
public void setPu_ht(String Pu_ht) {
    this.Pu_ht = Pu_ht.replace(",", ".").replace(".00", "");
}

Tried example:

  /**
 * @param Pu_ht the Pu_ht to set
 */
public void setPu_ht(String Pu_ht) {
    this.Pu_ht = Pu_ht.replace(",", ".").replace(".00", "").replaceAll("\\s+", "");
}

Other example:

 /**
 * @param Pu_ht the Pu_ht to set
 */
public void setPu_ht(String Pu_ht) {
    this.Pu_ht = Pu_ht.replace(",", ".").replace(".00", "").replaceAll(" ", "");
}

Output data example: 1 635.90

I tried a lots of things but nothing work for my case.

Best regards

EDIT:

My code:

 public void requete_pommes() throws IOException, ClassNotFoundException, SQLException {
    // open file input stream
    BufferedReader reader = new BufferedReader(new FileReader(filename));

    // read file line by line
    String line = null;
    Scanner scanner = null;
    int index = 0;
    List<Pommes> pomList = new ArrayList<>();
    boolean firstLine = false;

    while ((line = reader.readLine()) != null) {
        if (!(line.equals(";;;;TOTAL HT"))) {

            if (!(line.equals(";;;;"))) {

                Pommes pom = new Pommes();
                scanner = new Scanner(line);
                scanner.useDelimiter(";");
                while (scanner.hasNext()) {
                    String data = scanner.next();


                    pom.setNumero_compte("21826");
                    if ((index == 0)) {
                        pom.setReference(data);
                    } else if ((index == 1)) {
                        pom.setDesignation(data);
                    } else if ((index == 2)) {
                        pom.setQte(data);
                    } else if ((index == 3)) {
                         if(data.equals("1 635,90")){
                              data = data.replaceAll("\\s","");
                              System.err.println("data: " + data);
                         }
                        pom.setPu_ht(data);
                    } else if ((index == 4)) {
                        pom.setMontant_HT(data);
                    } else {
                        System.out.println("invalid data::" + data);
                    }
                    pom.setNumero_commande("1554");

                    index++;
                }
                index = 0;
                pomList.add(pom);

                requeteCorps = "(( SELECT codea FROM article WHERE tarif7 != 'O' AND tarif8 = 'O' AND pvente > 0 AND  COALESCE(trim( reffou), '') != '' AND reffou = '" + pom.getReference() + "' ), " + pom.getQte() + " , " + pom.getPu_ht() + ", '" + kapiece + "', 'stomag','vendu', getnum('LCK')),";

                ar.add(requeteCorps);
            }

        }

    }
6
  • 2
    please provide an minimal reproducible example Commented Nov 24, 2017 at 8:20
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a minimal reproducible example. Use the "edit" link to improve your question - do not add more information via comments. Thanks! Commented Nov 24, 2017 at 8:20
  • I format data into the setter of the class that is not a good idea. Formatting should be done by the view not by a setter Commented Nov 24, 2017 at 8:21
  • 1
    You are putting up a lot of things, but we would only need: input example, expected output, and what you actually got. Besides: read about java naming conventions. For example you only use _ in SOME_CONSTANT. And please note: the javadoc you have there is really pointless. That is nothing but line noise - and of course: it is lying! Your method is not only setting a value, but also formatting whatever comes in. Rather call it formatAndSet() or something alike. Commented Nov 24, 2017 at 8:21
  • 1
    I cannot reproduce. Both of your attempts work on my computer and yield 1635.90 (without any space). Commented Nov 24, 2017 at 8:54

2 Answers 2

3

The value "1 635,90" probably stems from a locale specific format, and the "space" actually is a non-breaking space, \u00A0. This is done often to prevent in flexible width text representation a line break to happen inside a number.

s = s.replace("\u00A0", "");
Sign up to request clarification or add additional context in comments.

3 Comments

Work like a charm ! Thank you !
Funny that a no-break space doesn’t count as whitespace with regex \s. I can confirm that it doesn’t, so it is necessary to mention it explicitly as in the code in the answer.
@OleV.V. yes, quaint. Explainable as a nbsp glues two text pieces into one: in physics: an antispace.
0
String Pu_ht = "1 635,90";
System.out.println(Pu_ht.replace(",", ".").replace(".00", "").replaceAll("\\s+", ""));

just put the above codes in main method and execute. the output will be 1635.90,then examine your codes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.