Unicode Printing in Java- abstract characters - java

I'm making this program that prints Unicode characters. It looks something like this:
public class test {
public static void main(String[] args) {
System.out.println('\u00A5');
System.out.println((char) 0x00A5);
System.out.println((char) (Integer.parseInt("00A5", 16)));
System.out.println('\u261E');
System.out.println((char) 0x261E);
System.out.println((char) (Integer.parseInt("261E", 16)));
}
}
The output looks like:
¥
¥
¥
?
?
?
Why does the latter half print question marks?
I can understand the program printing a Japanese character, but when I change it to \u261E, I can't recognize it. Any help?

If you want to apply the changes specific to your project then:
Go to your project properties -> change Text file encoding to UTF-8
OR
if you want to apply it to all projects globally then:
got to Window -> Preferences -> General -> Workspace : Text file encoding
Note:
If you are using some other IDE, you should have similar option there too.

Got the issue. This is an UTF-8 encoding. So you have to set encoding style as "UTF-8" in eclipse.
Change it Here :
Window -> Preferences -> General -> Workspace : Text file encoding

Related

Inserting line break after method name in Java method declaration

public MyClass aVeryLongMethod(
String args1, String args2, String args3, String args4, String args2)
throws Exception
{
myMethod();
I have an eclipse style java formatter (on VSCode). What are the indentation settings for the following?
line break after the method name aVeryLongMethod(
Double indent for hanging lines in method declaration (8 spaces before args1)
There is no specified line indentation in Java. That means unline Python, in Java indentations are not syntactically counted. White spaces are ignored by the lexer.
Anyway, it is important to have a better indentation for clean code. You can use the VS Code formatted to format your indentations. If I have such a case, I used to the following syle.
public MyClass aVeryLongMethod(
String args1,
String args2,
String args3,
String args4,
String args2
) throws Exception {
myMethod();
}
On Eclipse, the java formatter can be configured as follows (Java -> Code style -> Formatter -> edit Active Profile). The line width affects the final result I guess. Profile can be exported also.
Open Command Palette, there's Java: Open Java Formatter Settings with Preview, where you can customize your formatting style. Turn to the option Wrapping, you can set the code max length.
In my following example, the length to the end of function name is 32, so if i set the max length as 32, turn back to .java file, right click and choose Format Document, please see the effect:
Have a try.

How to automatically escape unicode characters in Java property files using Gradle?

I'm translating a Java application by using a ResourceBundle with various *.properties files. Now I like to have a Gradle task or want to modify a task to automatically escape any unicode character by replacing it with its ASCII representation, something like Java's native2ascii tool does.
This is what I'm done so far with my build file, but the output remains unescaped:
import org.apache.tools.ant.filters.EscapeUnicode
tasks.withType(ProcessResources) {
filesMatching('**/*.properties') {
println "\t-> ${it}"
filter EscapeUnicode
}
}
Any help is appreciated.
You can do it providing the additional copy specification for properties files, this way:
import org.apache.tools.ant.filters.EscapeUnicode
tasks.withType(ProcessResources).each { task ->
task.from(task.getSource()) {
include '**/*.properties'
filter(EscapeUnicode)
}
}

How to print IPA characters with mvn:exec?

I've the following simply program that prints the IPA word ˈabsəluːt [1]. Unfortunately executing this program with mvn:exec prints the word as ?abs?lu?t[2]. How to make it print it correctly, i.e. as in [1]?
package dp4j.encodingtest;
public class App {
public static void main(String[] args) {
String s = "ˈabsəluːt";
System.out.println(s);
FileUtils.writeStringToFile(new File("s.txt"), s);
}
}
The mvn exec:exec command:
mvn "-Dexec.args=-classpath %classpath dp4j.encodingtest.App"
-Dexec.executable=C:\\jdk1.7.0_25\\bin\\java.exe exec:exec
Even printing the word to the s.txt file doesn't print it is incorrectly as in [2].
The issue is with the application where that word is displayed/printed, in this case, the console. Nothing can be done from java / maven except to make sure your java source code encoding is UTF-8 (as string literal is used).
If you are running it from an IDE, then try to change the console font from IDE's options/preferences to ‘Lucida Sans’ as this font has partial support for IPA extensions or some other available fonts with IPA support.

Syntax error on token "Invalid Character", delete this token

I am not sure why is it giving this error. Braces seem to be right. Another thing is that the same program works in Windows-eclipse but not in eclipse for Mac. What could be the reason?
import java.util.Vector;
public class Debug
{
private int something = 0;
private Vector list = new Vector();
public void firstMethod()
{
thirdMethod(something);
something = something + 1;
}
public void secondMethod()
{
thirdMethod(something);
something = something + 2;
}
public void thirdMethod(int value)
{
something = something + value;
}
public static void main(String[] args)
{
Debug debug = new Debug();
debug.firstMethod();
debug.secondMethod();
}
}
Ah, ok - it's probably a control-Z or other unprintable character at the end of the file that is ignored in Windows but not on the Mac. You copied the source from Windows to the Mac. Delete the last few characters and re-enter them - I think it will go away. I don't do Mac, though - I'm just guessing.
I had the same problem importing my projects from mac to linux Slackware.
Mac OSX creates some temporary files with the same name of the files in folders (._filename) in all folders.
Usually these files are invisible in Mac OSX, but in the other OSs no.
Eclipse can find these files and tries to handle like sources (._filename.java).
I solved deleting these files.
Only way i could resolve this problem was press Ctrl+A to select all text of file then Ctrl+C to copy them then delete file and create new class with intellij idea then Ctrl+P to paste text in new file. this resolve my problem and compiler never show error after do this solution.
It can happen when we copy and paste .It happens when there may be some character which is unrecognized in one platform but recognized in other.
I would suggest don't copy rather try to write the entire code by yourself. It should work
I got the same error when I imported a project I created in a Mac, to Windows. As #Massimo says Mac creates ._filename,java files which eclipse running in windows consider as source files. This is what causes the problem.
They are hidden files, which you can see when you select the option, "Show hidden files and folders" under folder options in Windows machine. Deleting these files solves the problem.
I got this message trying to call a subjob from a tRunJob component. In the tRunJob I had both checked "transmit whole context" AND listed individual parameters in the parameters/values box. Once I removed the additional parameters it worked.
There are probably hidden characters in the line. If you move your cursor through the characters and your cursor doesn't move in one character, that means there is an invalid character in the line. Delete those and it should work. Also try copying and pasting the line to an hex editor and you will see the invalid characters in it.
i face this problem many times in eclipse . What i found is that select all code - cut it using Ctrl + x and then save the file and again paste the code using Ctrl + V . This works for me many times when i copy the code from another editor.
I also faced similar issue while copying the code from one machine to another.
The issue was with Space only you need to identify the red mark in your eclipse code.
On Windows, if you copy the source to Notepad - save the file (as anything), ensuring ASCI encoding is selected - the character will be converted to a question-mark which you can then delete - then copy the code back to Eclipse.
In eclipse right click on the file -> Properties -> resources
In Text file encoding select US-ASCII
This way you will see all the special char, you can then find & replace
And then format the code

Remove source file comments using IntelliJ?

Is there a plugin or tool in IntelliJ that will strip all comments out of your source .java files? I've read about an ANT task that can do this.. was looking to do the same from within the IDE. Alternatively a TextPad plugin would work as well..
You can use the "Replace" (or "Replace in Path" if you want to remove comments in multiple files) in the regular expression mode and then use this regular expression in the "Text to find" field:
(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/|[ \t]*//.*)
and replace it with an empty string. Then press "All" to apply this replacement to the entire file or all the selected files. This will remove all block comments and line comments from your file. If you want only block comments to be removed, use this regex instead:
(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)
And if you want to just remove line comments, you can use this regex:
([ \t]*//.*)
However, I should warn that this works only %99.99 of times. You might have a string variable defined in your file like:
String myStr = "/** I am not a comment */";
This regex will turn this to:
String myStr = "";
Late to the party but there is "Structural Search and Replace Dialogs" option that can be used to search different kind of comments and replace them
Go to Edit => Find => Replace Structurally...
Enter one of below in "Search Template:"
Single line
// $CommentContent$
Multi line
/*
$CommentContent$
*/
Javadoc
/**
$CommentContent$
*/
Leave the "Replacement Template:" blank
Select appropriate Scope
Click 'Find' and then 'Replace all'
you Should see no more comments
Note: that this might mess up formatting so you will have to reformat affected code
Press ctrl + r and in first textbox type //.*\n and then press replace all button. Then reformat the file by ctrl + alt + l.
The Comment Java Preprocessor allows to cut all commentaries from Java sources (the /R option)
http://code.google.com/p/java-comment-preprocessor/
Press control + r to open replace and replace all box
type //.* in first box, keep in second box empty and then select Regex. Select replace or replace all
That's it.
Enjoy 😎
My solution is:
find . -name *.java -type f -exec sh -c "perl -0pe 's#/\*(.|\n)*?\*/##g; s|//.*?\n|\n|g' '{}' > temp.java; cat temp.java > {} ; rm temp.java;" \;

Categories

Resources