Class, interface, or enum expected when compiling - java

I sat down and tried to write code for this prompt, and I made this.
However, when I compile it in BlueJ, I get: class, interface, or enum expected.
1) How do I fix this error? It seems like I forgot something, but I cant remember what I forgot.
2) Do you think I fulfilled what the prompt was asking?
Thanks.

I'm not going to do your homework for you but here are some hints:
You need to put all your code inside a class
You need to put the code in the main method. (Search on Google) the method served for an entry point.
You only need one for loop... Loop through the array, if the value equals 0, return the index number. After the loop just return -1.

Related

get return type without execution of method

i have tried to make some web research, but i haven't find any answer, i would like to get immediately the return statement without the execution of the method that give it. (it run before)
public static int totalIncome (String [][]field) {
int sum = 0;
sum += chairPrice (field); // here get the number without run the method
return sum;
}
i can't post all the code of the project i am working, i am stuck to get all the ticket sold and i was thinking this was a good way, but isn't working because it run the method again.
any idea how to do this is welcome.
i remove some method beuse it did't let me post it all, i can't even post the method chairPrice.
StackOverflow say there is too much code in my post, i tried to share all the code by codeshare,
https://codeshare.io/kmRQWV
There is no way, in Java, to say "I already called this method, give me the value that was returned last time." You must explicitly store a variable somewhere to hold the value.
That said, from your code, it doesn't look like you can't call chairPrice because it's already been called before, but because some other state has changed. You will need to figure out the problem in your logic.

Iterating with a for loop in Java

I'm trying to figure out what this is actually called and where I can find the answer for it. I can't understand the difference in
for (Treet treet : treets) { }
I don't know what the different "treet" mean. can someone help me or at least tell me what it's called?
for (Treet treet : treets) { }
This is an enhanced for statement (although often referred to as an enhanced for loop). You are saying that you want to do something for every element in some collection of things. (Well, actually, you're doing nothing here, but never mind).
In English: "For every Treet in treets, do something".
Treet is a type. Somewhere you need a class called Treet defined. If it's in a different package, you need to import it.
treets is either an Iterable<? extends Treet>, meaning you could call treets.iterator() and use the result in the standard hasNext()/next() way; or an array of something which extends Treet. It's something you can iterate through.
treet is a single element from the iterable/array. You can use it only inside the body of the loop.
Thats a forach loop, it iterates through the array/list/... and performs the actions defined in the loop to every element in the list which is referenced by treet.

Looping in java

I know this is weird, I want do this way and I couldn't find a solution which is why asking you guys! I've methods like which I'm calling inside a class as listed below -
//
#Test
Key.chooseNew(0);
Key.navigate_toNew(1);
Key.send_keys_loginNew(2);
Key.logoutNew(3);
How can I run it in a loop in java replacing those numbers or such things doesn't exist?!
You are calling a different method on each line so there is no loop.
The only pattern I can see is:
int i = 0;
Key.chooseNew(i++);
Key.navigate_toNew(i++);
Key.send_keys_loginNew(i++);
Key.logoutNew(i++);
Have a look at Reflection in java. You could save the names of the methods to be called in a list in the order you want them called and then inside the loop you can call the method with the method name using obj.getClass().getMethod() as shown in this post.
Hope this is what you were looking for.

Java Array concept

for(int m=0; m< checkBoxValue.length ; m++)
{
System.out.println("check box in model class"+checkBoxValue[m]+"\n");
}
This loop is to print two values in array. It prints the values But after that it shows array out of bound exception
It seems you're on the wrong track. It's best to set a breaking point on your for loop and debug your code, then go through it step wise. This will reveal where the exception is thrown...
Especially since you say "after that", you might want to review your code after that for loop :-)
Are you sure the Exception is raised here ?
Ohh.. Looks like a mess. The information looks very abstract. You need to be specific, may be you can give more code over here. One possible cause I think of, may be, is Multi-threading.
Only multi-threaded application can do this trick. If so, you better provide synchronization on the origin object of checkBoxValue variable.
Hope that helps....
The code should work fine provided you have done the array initialization correctly.
The posted code should not throw ArrayIndexOutOfBoundsException. Most likely, you are doing something after the loop which accesses an incorrect index of an array.
The only way that the code shown in the question could throw an ArrayIndexOutOfBoundsException is if the toString() method of one of the checkBoxValue[m] objects throws the exception.
Maybe you have overridden the toString() method of the checkBoxValue-class (the array initializer would help identifying this class). Following this theory, the toString() implementation might work fine for the first two elements of the array (they are printed) and may throw an exception for the third element in the array.
This could lead to the error description: This loop is to print two values in array. It prints the values But after that it shows array out of bound exception

Strange java enum bug

I have a really strange enum bug in Java.
for(Answer ans : assessmentResult.getAnswersAsList()) { //originally stored in a table
//AnswerStatus stat = ans.getStatus();
if (ans.getStatus() == AnswerStatus.NOT_ASSESSED) {
assessed = false;
}
}
An answer is an answer to a question on a test. An assessment result is the result a student gets on a test (this includes a collection of answers).
I've debugged the above code, and ans.getStatus() returns AnswerStatus.ASSESSED.
Still, the if line returns true, and assessed is set to false.
But, the thing I think is most strange; When I declare the AnswerStatus stat variable, it works, even if I don't use the stat variable in the if test. Could someone tell me what is going on?.
I've read something about enum bugs in serialization/RMI-IIOP but I don't use that here.
The enum AnswerStatus can be ASSESSED or NOT_ASSESSED.
The getStatus method in class Answer just returns the status, nothing else.
I've debugged the above code, and
ans.getStatus() returns
AnswerStatus.ASSESSED. Still, the if
line returns true, and assessed is set
to false.
But, the thing I think is most
strange; When I declare the
AnswerStatus stat variable, it works,
even if I don't use the stat variable
in the if test. Could someone tell me
what is going on?.
This sounds like the getStatus() method does not always return the same result - how is it implemented?
BTW, what's the point in having an enum with the values ASSESSED, and NOT_ASSESSED? Why not use a boolean isAssessed()?
Solved.
It was the NetBeans debugger that tricked me. It does not pass the if test (although NetBeans says that).
Sorry for the inconvenience :-)
What happens if you use .equals instead of ==?
No answer to your question but a suggestion:
It's always better to place the fix enum value in the first place in the compare statement and the variable part in the second place. Because, if in any circumstance the variable part delivers NULL you won't get a NullPointerException.
In your example it will look like this
...
if (AnswerStatus.NOT_ASSESSED == ans.getStatus())
...
MISTAKE:
Of course I make a mistake and mixed two things with each other. If you use the equals method to compare a fixed enum value with a variable containing this enum it's good to compare the constant enum value with the variable and not vic versa. For example:
write
if (AnswerStatus.NOT_ASSESSED.equals(ans.getStatus()))
instead of
if (ans.getStatus().equals(AnswerStatus.NOT_ASSESSED))
because, this could harm a NullPointerException if ans.getStatus() == null.

Categories