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.
Related
I'm having issues reading a Float in from an external data file (.txt)
I've read a number of different posts about this error, and common problems, and I still don't know what I'm doing wrong. The advice varies, some people say use ".nextLine()", some people say use ".next()", I've tried a bunch of these combinations and am still having problems.
Here's the relevant section:
public void read_file(Interface iface) throws FileNotFoundException {
int readCount = 0;
File file = new File(""); \\FILE PATH REDACTED
Scanner reader = new Scanner(file);
reader.useDelimiter(",|\\n");
while(reader.hasNext()){
String type = reader.next();
float rate = reader.nextFloat();
String desc = reader.next();
//Some irrelevant code follows
Text file:
Test1
10.00
Test2
I get the following exception:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextFloat(Scanner.java:2496)
at ReadFile.read_file(ReadFile.java:20)
at Main.main(Main.java:21)
I conducted a test and it definitely reads in the first line (I was able to print that). So I'm guessing its something to do with the line return at the end of the line after Test1. But I thought that would get handled by the use of the delimiter.
I'm using IntelliJ (some people have suggested that using a ,(comma) instead of a .(period) can sometimes resolve the error - but this seems limited to NetBeans or Eclipse, I forget).
Does anyone have any suggestions?
I think the line breaks you set in 'reader.useDelimiter()' should be consistent with the line breaks in the file. Generally, it is '\r\n' for Windows, '\n' for Unix, and '\r' for Mac.I hope it can help you.
Apparently, you must add this line before or after useDelimiter :
reader.useLocale(Locale.US);
It's required for the scanner to be able to detect floats.
For whatever reason, once it started working, I tried removing the line and it kept working. I reverted back to your original script, and it was still working. I think the Locale settings are persistent or something.
Anyways, if adding this line works, I suggest leaving it like that :)
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...
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
how to eliminate empty new line into source code using regex.?
my basic is java
example :
class ex{
String a="Hello World";
public static void main(String[] args){
System.out.println(a);
}
}
result source code without empty new line :
class ex{
String a="Hello World";
public static void main(String[] args){
System.out.println(a);
}
}
To delete an empty line use:
String result = text.replaceAll("(?im)^\\s*\r?\n", "");
You could do it using:
//read line
if (line.matches("^\\s*$")) {
System.out.println("ignoring empty line");
}
Alternatively, you could use IDE's like eclipse/netbeans/Intellij etc and configure how you would like to indent your code automatically.
Many modifications of a text file (Java or otherwise) can be achieved by using standard tools. Writing a program for eliminating empty lines is a waste of time.
If you are on Linux, run your file through grep:
grep -v '^\s*$' Input.java >Output.java
For Windows, get one of the work-alikes which Google ("grep on windows") will find you. The command, using cmd, would be the same, but the Windows program may come with a GUI, which might make it even simpler.
We'd like to have the version of the java source code, in our case the svn $Id$ string, embedded in the generated class file.
We'd like to be able to determine this information from a static inspection of the class file, preferably by running the strings or what command.
A naive attempt to declare a private final static String variable set to this value inside each class didn't result in a legible string embedded in the class file.
You said ... preferably by running the strings or what command ...
Let's assume you have something like this in your code:
private static final String SVN_ID =
"$Id: SvnIdDemo.java 1081 2008-09-30 19:03:23Z john $";
The following Perl one-liner ...
$ perl -nwe 'print "$1\n" if /.*(\$Id:[^\$]+\$).*/' SvnIdDemo.class
... prints the SVN ID string to STDOUT.
$Id: SvnIdDemo.java 1081 2008-09-30 19:03:23Z john $
You could add a method to the bottom of each class with a predefined name. Say you used:
public String extractChaimGeretzVersionNumber() {
return "$Id$";
}
Then you find the version with a program that loads the class and, via reflection, calls the magic method and retrieves the version.
You would either have to have code that inserted the method to the .java files before building the .class and .jar files OR you could have a build step that checked that the magic method was already in every one of them. Fail the build if not found.