This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 6 years ago.
I just created a plugin that enables donors to vote on weather and time, there were no errors showing up in my IDE (Eclipse), but when I try to run it on my plugin test server, it gives CommandException, and I can't figure out what its problem is.
Here is my code:
http://pastebin.com/ksbfRDfT
Here is the Exception:
http://pastebin.com/fiKb4Vz3
I do need to hurry, because I am doing this for VoxelMC, and it needs to be done quick.
EDIT: The first command (startvote) is working now, however the second command is giving the same exception now. Am I not seeing something? I changed the old link to code to a new link to the code.
Bukkit commands work as follows:
/command args[0] args[1] args[2] args[3] args[4] args[5]
The exception is caused because you're using args[1], which corresponds to the second parameter in the command you typed in. You're most likely looking for args[0].
Another thing, you're using the == operator for String comparison, you should use String.equals(String);
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I am reasonably experienced with programming, but not specifically in Java. I am coming across the following error when working in eclipse. My code is the following:
I have used the debug function, and it reports that carbonPrefix is pent, but that carbon stays at 0 throughout. Like I said, I am a novice to Java and Eclipse, so I may not be using the debug function to it's full extent.
For anybody that's interested, this the start of code where you input the name of an alkane and it tells you the formula. It worked in Javascript and I'm just trying to translate it into Java.
Thank you all so much!
you have to use
carbonPrefix.equals("pent");
in java == operator used to compare two object references and the method equals() is used to compare two strings to determine whether they are equal or not.
This question already has answers here:
Sonarqube, "String contains no format specifiers" when logging constant String message
(2 answers)
Closed 4 years ago.
I am using what felt like correct Java code in order to trace sections of a program:
LOG.debug("a simple string with no formatters nor placeholders.");
However, SonarLint in Eclipse is showing the following report on that line:
String contains no format specifiers.
The matching rule, S3457, reads "Printf-style format strings should be used correctly (squid:S3457)".
I am using logback and the LOG variable in the code above is of type org.slf4j.Logger.
I'd like to clear this report, and in the meantime, learn something.
Can someone enlighten me as to what is wrong in this? Or is it a case of Sonar being too strict in applying that rule?
Otherwise, is it considered bad practice to output a log message without outputting values?
I think this might be an issue with the latest version of SonarQube.
I've found the following report, where the following code produced a false positive of the same issue you encountered:
log.trace("Connection closed");
The SonarQube representative has said that this is a known issue and that they should have a fix ready for it.
This question already has answers here:
Java : parse java source code, extract methods
(2 answers)
Closed 7 years ago.
I am working on a small program to compare two Java files. My goal is to compare the two files so that I can see what functions were added and deleted from one file to another (like a simple version control program). I am running into issues on how I should be handling these files. My current approach is to use a Scanner and use:
while(scanner.hasNext()) {
String function = scanner.next("((public|private|protected|static|final|native|synchronized|abstract|threadsafe|transient)+\\s)+[\\$_\\w\\<\\>\\[\\]]*\\s+[\\$_\\w]+\\([^\\)]*\\)?\\s*\\{?[^\\}]*\\}?");
System.out.println(function);
}
However this is not getting me any results for a file that I know has functions in it. Any tips or ideas on how to approach this?
You could use ANTLR Java grammar https://github.com/antlr/grammars-v4/blob/master/java8/Java8.g4 to get a full-blown Java parser and then use it to extract any information you need about Java files.
This question already has answers here:
How does Java resolve a relative path in new File()?
(7 answers)
Closed 7 years ago.
I was given a piece of code that has this line:
GetBytes getInput = new GetBytes("myText.txt");
Which obviously reads a text file and tries to get its input.
I am using jdk1.8.0_20 on Windows 8 running inside eclipse.
In which folder should I put a file named myText.txt?
First:
GetBytes getInput = new GetBytes("myText.txt");
You forgot the last parenthesis there.
Second:
It goes in the same folder as your class is in.
GetBytes is a class that was given to you, I presume? The code in that class should specify which folder it looks in. Usually, the default is the same folder as the class. Since GetBytes is a custom class, it could be anywhere, though. If you can't figure it out, post the code for the GetBytes class.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
EDIT: Thank you, PakkuDon
instead of using "==" I must use ".Equals()"!
I'm trying to implement a chat-command system though I've encountered troubles in differentiating between standard chat and command chat..
Focusing on just one command for now, `highlight
the output whenever I type in `highlight is:
highlight
`highlight
Here's my code:
String cmd = InMessage.message.substring(0, 10);
System.out.println(cmd);
System.out.println("`highlight");
if( cmd == "`highlight" )
{
... cancel chat packet and proces command
}
and yet the if statement returns false.
What's going on here? Anything I've done is wrong?
You have a number of problems. Firstly, that substring is going to throw an exception if the user types something that's less than 10 characters. Secondly, you're using == where equals would work better. But you don't need either of these things. You could just use
if (InMessage.message.startsWith("`highlight")) {
// whatever
}