Error in deleting an expression in a sentence using replace - Java - java

I am actually trying to delete the following expression:
?? ur [Ljava.lang.String;??V?? {G xp t ?
from the following sentence:
?? ur [Ljava.lang.String;??V??{G xp t ?/store/sd/Android/data/map/files/exApp
I am using this Java code in order to proceed so:
public String convertByteToString(byte[] bytes){
String str=new String(bytes,StandardCharsets.UTF_8);
String newstr=str.replace("[Ljava.lang.String;??V??{G xp t ?);
return newstr;
}
However, it gives me the following result:
?? ur[Ljava.lang.String;??V??{G xp t ?}/store/sd/Android/data/map
/files/exApp
Which is not correct at all because the expression has actually not been deleted as I wanted.
The weirdest thing is that when I am copying-pasting the string output from my NetBeans console to Notepad++, I obtain:
Do I have to include all these symbols in my Java code ?

In fact the 1st string is not in the 2nd, take a look at the image below..
you have a weird sign in the 2nd String
Just paste those in some TextEditor like Notepadd++ so you can identify it...

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.

VSCode not reading Cyrillic from console

Well, here is the problem, I have started using VScode, and I can't read from console cyrillic characters.
My code:
import java.util.Scanner;
class App {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in, "UTF-8");
String word = input.nextLine();
System.out.println(word);
}
}
Now when I enter any cyrillic string it will print empty string back to me. If i write something like
System.out.println("Привет"); //cyrillic symbols
It will print "Привет", which is fine. So I am guessing it has something to do with reading the string rather than outputing it.
chcp command gives Active code page: 65001
I have tried setting encoding and without it, but it doesn't seem to work, is there something I missed?
Thanks in advance
I've tested the code on my machine and got the same result: nothing shown;
You can see, when run it in external Window PowerShell or Command Prompt, the result is different but still not shown correctly:
When we change the encode style to GBK(936), the cyrillic characters can be displayed correctly:
When it comes to changing integrated terminal encoding style in vscode and execute code again, it still shows nothing:
About these different results between external Command Prompt and integrated terminal in VS Code, I've put a github request. And I'm doing some research, if any useful imformation i get, i will update you.

How to pass hieroglyphs as an argument to the main method from command line

I met some interesting question on which I was not able to find an answer. Does anybody know how to pass hieroglyphs as an argument to the main method from command line?
Below there is some pseudo code which will help to test suggested solution:
public class Test {
public static void main(String args[]) {
if ("香港政府".equals(args[0])) {
System.out.println("Match");
}
}
}
So question is how to call Test.class with an argument so the application prints Match to the console? args[0] can be transformed before passing to the if statement.
Thanks in advance.
After some additional research I was able kinda figured it out. So guys who commented on question were very near to the answer.
Encoding which I tried to find was 936. But it doesn't mean that you will be able to run chcp 936 if you OS locale is other than chinese. Once you will try to run it on other locale than chinese:
chcp 936
You will get following error:
Invalide code page
For making it working you have to change region. FOllowing steps will be needed:
Start - COntrol panel
Select "Region and Language"
Select "Administrative" and click "Change system locale..."
Select "Chinese (Simplified, PRC)" and reboot laptop
After restart when you will run chcp you will see following output Active code page: 936. Now you are ready to execute command line with hieroglyphs.

class parameter missing final quote

I have the following code
public class MyClass {
public MyClass (String myString){
myFlag=myString
}
ProcessBuilder pb = new ProcessBuilder("Path to my application", "variousflags", myFlag)
Process p = pb.start();
}
When I run
myClass("worddocument.doc")
pb doesn’t start. In debugging MyClass, I noticed that:
myFlag ="worddocument.doc
without the final quote. For a flag to work in the ProcessBuilder, it has to within quotes.
I have to include the line
myFlag= myFlag.concat(""");
which gives me the error message in Netbeans "unclosed string literal". How can get rid of the neatbeans error, or even better, how can I get the final quote back?
Thanks
stringFlag= stringFlag.concat(""");
will cause a problem since your ide thinks you close the String at the second quotation mark.
Try to escape the second quotation mark.
stringFlag= stringFlag.concat("\"");
The issue has disappeared now :-) It must have been a bug in netbeans or java...
Unfortunately, I can't reproduce the issue, and I am not sure exactly when it fixed itself.
If encountering the same problem, I would suggest:
1-restarting computer,
2-restarting Netbeans,
3-commenting out
myFlag= myFlag.concat("\"");

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

Categories

Resources