Home > Net >  How to change hyphen sign for question mark in java program?
How to change hyphen sign for question mark in java program?

Time:06-18

I'm utilizing this line codes

String string = "Some usefull information − don't know what happens with my output";
System.out.println(string);
String str2verify = driver.findElement(By.xpath("//someWellFormXpath")).getText();
Assert.assertEquals(str2verify , "Some usefull information − don't know what happens with my output");

And I'm getting this in my console, so if I want to use equals function doesn't work.

Output

Some usefull information ? don't know what happens with my output
expected [Some usefull information ? don't know what happens with my outputS] but found [Some usefull information − don't know what happens with my output]
java.lang.AssertionError: expected [Some usefull information ? don't know what happens with my outputS] but found [Some usefull information − don't know what happens with my output]

CodePudding user response:

This is the process:

  • You write some text. In an editor. That is showing strings to you.
  • You save your file. files are bytes, not characters, so your editor is applying a charset encoding to do this. Which one? Your editor will know, you didn't mention which one you use so I can't tell you.
  • Javac reads your file. files are bytes, but javac needs characters, so javac is applying a charset encoding to do this. Which one? "The platform default", unless you use the -encoding parameter / the tool you are using that calls javac has a way to tell it which -encoding parameter to use.
  • Javac emits class files. These are byte based so this doesn't require encoding.
  • Your java JVM runs your class file. As part of running, a string is printed to standard out.
  • System.out refers to 'standard out'. These things are, on pretty much every OS, a stream of bytes. Meaning, when you send strings there, the JVM first encodes your string using some charset encoding, then it goes to standard out.
  • Something is connected to the other end of standard out and sees these bytes. These convert the bytes back to a string, also using some encoding.
  • The characters are sent to the font rendering engine on your OS. Even if the character 'survived' all those conversions back and forth, it is possible your font doesn't have a glyph for it. The intent is clearly for that character to be an emdash (a dash that is as long as the letter 'm' - the standard 'minus' character is an ndash, not the same thing; that one is a bit shorter).

Count em up - that's like 6 conversions. They all need to be using the same charset encoding. So, check that your editor and javac agree on what charset encoding your source file is in. Then, check that the console thing that is showing the string is in agreement with standard out (which should be 'platform default', whatever that might be), then, check if the font you use has emdash.

PrintStream ps = new PrintStream(System.out, true, "UTF-8");

Then write to ps, not System.out - that's how you can explicitly force some charset to be used when writing to output.

CodePudding user response:

It turns that em dash doesn't have a representation in cp-1252 charset encoding, so at the end I have to change to UTF-8 all my files in the project to be able to save this character.

It was a pain in the brain this encoding issue.

Thanks for all the suggestions friends.

  • Related