I coded an enum type which brings up the following Syntax errors when I run my created JUnit test for it:
java.lang.Error: Unresolved compilation problems:
Syntax error, insert "enum Identifier" to complete EnumHeaderName
Syntax error, insert "EnumBody" to complete EnumDeclaration
Syntax error, insert "}" to complete ClassBody
My enum type has static functions which for a particular String, returns an enum constant. Here is some of my code of the enum type:
public enum MusicType {
ACCIDENTAL, LETTER, OCTAVE, REST, DUR, CHORD, TUPLET;
public static MusicType is_accidental(String a){
if (a=="^" | a=="_"|a=="=")
return ACCIDENTAL;
else return null;
}
}
The rest of my static functions are very similar (i.e. is_letter, is_octave, etc.), although some use input.matches(regex) function instead of checking to see if an input it equals a particular string.
Here is the beginning of the JUnit test which tests the function dealing with the accidental constant:
public class MusicTypeTest {
#Test
public void accidentalTest(){
String sharp = "^";
String flat = "_";
String natural = "=";
assertEquals(MusicType.ACCIDENTAL, MusicType.is_accidental(sharp));
assertEquals(MusicType.ACCIDENTAL, MusicType.is_accidental(flat));
assertEquals(MusicType.ACCIDENTAL, MusicType.is_accidental(natural));
}
}
The other functions in my JUnit test which test all the enum static functions are coded similarly. I cannot figure out why I have these syntax errors (this is my first time coding an enum type). I've been coding in Eclipse and have not found any missing "}"s as of yet. I don't know if this has anything to do with the way I've written the test or the way I've declared my variables. Does anyone know why I have these syntax errors?
I had this same problem with Eclipse. It was a misleading syntax error message. It was due to a misplaced ";" after an annotation.
Double check your code ignoring the message.
I was getting this error while writing an Android app. All my brackets were closed; I was following an example from a different site. I ended up selecting the entire text for my code, cutting, saving, and pasting the code back. The error went away. It's very possible that Eclipse got stuck...
Both the enum type and the class that you have just posted have two opening braces ({) and only one closing brace (}). If I had to guess, I'd say you need to put one more closing brace at the end of each of these files.
Related
Eclipse was warning me that a local variable randInt might not be initialized (it was). So I added the first line:
#SuppressWarnings("all")
return randInt;
The warning went away, but I'm getting two new errors on the first line:
Syntax error: insert "enum Identifier" to complete EnumHeaderName, and
Syntax error: insert "EnumBody" to complete BlockStatement
What on earth? It's surprisingly hard to find information about #SuppressWarnings. Is there a more precise way of getting rid of this specific warning than using "all"?
Minimal, complete, verifiable example:
public class SuppressTest {
public int cut() {
int randInt = 0;
#SuppressWarnings("all")
return randInt;
}
}
You can't insert #SuppresWarnings on a return statement. In java 8 you can annotate only classes, methods/constructors, fields, parameters and (new in java 8) local variables.
So in your case java can't parse what you have written. Move the #SuppressWarnings at the method level.
I encountered an interesting problem with macro definition in pig. Here's the code:
DEFINE Func(src) RETURNS dst{
$dst = GROUP $src ALL;
DUMP $dst;
}
raw = LOAD 'data';
grp = Func(raw);
And when execute those codes in grunt shell, there will be an error:
ERROR 1200:... Failed to parse macro 'Func'. Reason:... Syntax error, unexpected symbol at or near 'DUMP'
But, where did I make the mistake? O.O
I found that someone has already reported the bug(if we call that a bug: link) a long time ago, but it seems the Pig developing team is not planing to solve that. :(
My class currently looks like this:
package controller;
public class TestSprites {
}
When I try to run it, the console spits out this:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on tokens, ( expected instead
The left-hand side of an assignment must be a variable
Syntax error, insert ")" to complete Expression
Syntax error, insert "AssignmentOperator Expression" to complete Assignment
Syntax error, insert ";" to complete Statement
at controller.TestSprites.<init>(TestSprites.java:55)
at controller.TestSprites.main(TestSprites.java:131)
I honestly have no idea what's going on. It was doing the same thing earlier when I actually had code in the class, so I took it all out, saved it, refreshed, exited and reopened eclipse. If it makes a difference, the code it was triggering on before was:
panel = new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
for(SpriteObject j : sprites) {
j.draw(g);
}
}
};
I also fail to see the issue there. Why is this happening, and how do I stop it?
I'm writing a parser for a DSL that uses the syntax (nodeHead: nodeBody). The problem is that nodeBody may contain parens, at some cases.
The between operator of JParsec should have been a good solution, yet the following code fails:
public void testSample() {
Parser<Pair<String,String>> sut = Parsers.tuple(Scanners.IDENTIFIER.followedBy(Scanners.among(":")),
Scanners.ANY_CHAR.many().source()
).between(Scanners.among("("), Scanners.among(")"));
sut.parse("(hello:world)");
}
It does not fail when I change ANY_CHAR to IDENTIFIER, so I assume the issue here is that the second parser in the tuple is too greedy. Alternatively, can I make JParsec apply the between parsers before it applies the body?
Any ideas are very much appriciated.
At the time I was asking, seems like there was no way to do that. However, a github fork-and-pull later, there is: reluctantBetween().
Big thanks to #abailly on the fast response.
If the syntax rule is that the last character will always be ")", you could probably do:
static <T> Parser<T> reluctantBetween(
Parser<?> begin, Parser<T> parser, Parser<?> end) {
Parser<?> terminator = end.followedBy(eof());
return between(begin, terminator.not().next(parser).many(), terminator);
}
I'm very new to Android development (have some Obj-C experience with Cocoa Touch though). I was testing my first Android app as I encountered these syntax errors:
Syntax error on token "100000", invalid VariableDeclaratorId
Syntax error on token "11", delete this token
Syntax error on token "2", delete this token
Syntax error on token "5000", invalid VariableDeclaratorId
Syntax error on token "61", invalid VariableDeclaratorId
Syntax error on token "69", invalid VariableDeclaratorId
When I double clicked them, they appeared to be in the file R.java and I have no idea how they are caused.
public static final class drawable {
public static final int 100000=0x7f020000;
public static final int 11ba=0x7f020001;
public static final int 2values=0x7f020002;
public static final int 5000=0x7f020003;
public static final int 61=0x7f020004;
public static final int 69=0x7f020005;
.....
It would be great if someone can tell how this is caused.
[updates]
MarsAtomic suggested that it is caused by not following the naming conventions for Android resources and perhaps having rawables named "5000", "69", which is, as a matter of fact, true in this case.
After changing the names, these exceptions didn't occur anymore.
But I would still like to know why having images in numeral names would trigger this. Thanks.
My comment has disappeared somehow, but... every language has naming conventions. Conventions are often just based on some semi-arbitrary heuristic because you have to have rules (e.g. it's slightly more clear that 'a1' denotes a variable name, whereas a variable named '1' could be confused for a value).
You can accept this answer as the canonical answer or not -- personally, I'm not bothered who gets what credit.
After changing the names, this exceptions didn't occur anymore.
But I would still like to know why this is happening.
Because the names of those resources are used as field names in an auto-generated Java source file (R.java), and they aren't valid Java field names. Java field names must start with a letter. Details on the characters allowed in field names here in the JLS.
(Really, MarsAtomic should be getting the credit for this, but doesn't seem to have seen the reposted question.)
You cannot declare variable names starting with numbers.
R.class is auto-generated, if you change something in it,it won't work correctly.
Thy to Clean and Build your Project.
If you get an error in
public static final class drawable {
the most common mistake is a named .png file in your res folder starting with a number.
For example 1_pic.png, 2015_22_22pic.png and so on.
Right way: pic_1.png
Among all folders located inside \ res \ and look for a file with the name different from the others, as 00000002. If you find, just delete it. Then click the name of your project in eclipse and then press the F5 key to refresh it. Thus the error will disappear and your R file will be updated.
Take a look at the RES directories. You might have a file with the numerical sequence mentioned above, eg 00000002. To solve the problem, simply remove it from the folder.