Java expects an "AnnotationName" after a method declaration - java

I am coding a game in Java, and must create a method inside an enum type that returns whether the player may fill a bottle with liquid or not. The method checks whether the player's inventory contains a bottle, and whether the player is near a liquid.
In the method declaration, I am getting an error that says I need to put an "AnnotationName" after it. I do not know what an "AnnotationName" is, and on all the other methods I have created in the file there are no such errors.
There is also another error at the bottom of the enum on the closing bracket. It says "Please insert '}' to complete ClassBody". I have checked all the curly brackets in the file and they all have matches. I think this error must be tied to the above "AnnotationName" error. I have included the method below, as well as the enum type the method is a part of.
The method by itself:
//the problematic method
#Override
public boolean isAvailable() {
//create var
boolean bottleInInventory = false;
//loop thru junk sublist
for (int i = 0; i < Data.player.sizeOfSubList(0); i++) {
//if current item is a Bottle, bottleInInventory = true; otherwise do nothing
if (Data.player.getItem(ItemType.JUNK, i).getClass() == Bottle.class) {
//set var to true
bottleInInventory = true;
}
//repeat loop until a Bottle is found (or not found)
}
//check if player is near a liquid AND if they have a Bottle in their inventory
//if they satisfy both conditions, return true
try {
if (Data.nearLiquid.get(0) != null && bottleInInventory) { //if index 0 has nothing in it, there must'nt be anything in the other indexes either.
return true;
}
} catch (IndexOutOfBoundsException e) { //if the player is not near any liquids this will happen during the above if statement.
return false;
}
return false; //if player is near a liquid and not in possesion of a Bottle
}
The whole enum:
FILL_BOTTLE("Fill Bottle") {
//this method is not complete
#Override
public void opCode() {
boolean multipleBottles; //if there is more than one bottle or not
ArrayList<Bottle> bottlesInInventory = new ArrayList<Bottle>(); //output of loop
Item currentItem; //used only inside loop
for (int i = 0; i < Data.player.sizeOfSubList(0); i++) { //loop counts how many bottles you have
currentItem = Data.player.getItem(ItemType.JUNK, i);
if (currentItem.getClass() == Bottle.class) {
bottlesInInventory.add((Bottle) currentItem); /* currentItem will always be assigned to a Bottle here,
* but we still need to cast because the
} * variable type is an Item. */
}
//multipleBottles is set to true if the bottlesInInventory list has more than one item in it
if (bottlesInInventory.size() > 1) {
multipleBottles = true;
}
System.out.println("You are currently able to fill your bottle with " + Data.nearLiquid.size() + " liquids near you");
System.out.println();
System.out.println("Here they are:");
for (int i = 0; i < Data.nearLiquid.size(); i++) {
System.out.println((i + 1) + ") " + Data.nearLiquid.get(i).getName());
}
int liquid = Data.intPrompt("Which liquid would you like to fill your bottle with? ");
}
//the problematic method
#Override
public boolean isAvailable() {
//create var
boolean bottleInInventory = false;
//loop thru junk sublist
for (int i = 0; i < Data.player.sizeOfSubList(0); i++) {
//if current item is a Bottle, bottleInInventory = true; otherwise do nothing
if (Data.player.getItem(ItemType.JUNK, i).getClass() == Bottle.class) {
//set var to true
bottleInInventory = true;
}
//repeat loop until a Bottle is found (or not found)
}
//check if player is near a liquid AND if they have a Bottle in their inventory
//if they satisfy both conditions, return true
try {
if (Data.nearLiquid.get(0) != null && bottleInInventory) { //if index 0 has nothing in it, there must'nt be anything in the other indexes either.
return true;
}
} catch (IndexOutOfBoundsException e) { //if the player is not near any liquids this will happen during the above if statement.
return false;
}
return false; //if player is near a liquid and not in possesion of a Bottle
}
},
I've tried adding curly brackets, annotations (#Override), and researched on AnnotationNames but not had any luck. I can't find any documentation on what an AnnotationName is, and the curly brackets and extra annotations just created more errors.
Edit:
Here is a link to the entire file as well as a link to the github repo with my code.
Option.java
Jump The Volcano

Related

For if loop wont loop: any pointers to why that might be?

This is supposed to loop 24 times; it does not, and I'm pretty confused as to why. Please help me various Kenobis out there :
private boolean simpleMove(Board bd)
{
int b = rn.nextInt(3);
for (int i = 0; i < 24; i++) {
if (bd.isVacant(i) && rul.isLegalMove(tigerLocs[b], i)) {
bd.swap(tigerLocs[b],i);
bd.setTiger(i);
tigerLocs[b] = i;
System.out.println(i);
return true;
}
else {
System.out.println(i);
}
}
System.out.println("invalid");
return false;
As the comments point out your loop will execute a maximum of 24 times.
But the return statement inside the if statement may cause it to return 'early'.
It looks like it's some kind of board game thing.
The board appears to have 24 'squares' and it makes the first legal move and returns true.
If it fails to find a legal move, it returns false.
I can't confirm the logic overall but that rationale seems sound:
If there's a move available, take it and return true.
If no move is available, make no move and return false.
If you expected it to continue, even after finding a "valid" move, then simply store the fact that a valid move has been found. This can be done in a separate boolean variable:
private boolean simpleMove(Board bd) {
int b = rn.nextInt(3);
boolean valid = false; // until proven otherwise below
for (int i = 0; i < 24; i++) {
if (bd.isVacant(i) && rul.isLegalMove(tigerLocs[b], i)) {
bd.swap(tigerLocs[b],i);
bd.setTiger(i);
tigerLocs[b] = i;
valid = true;
}
System.out.println(i); // why output HERE when we have a return value?
}
if (!valid) {
System.out.println("invalid"); // why output HERE when we have a return value?
}
return valid;
}
It's unclear if multiple "valid" moves could be found, and whether that would be a problem when you "swap" or not. If there is only ever one possible move, then there would be no need to continue iterating with the for loop; simply return in the body like you were doing.

Why does for readelements() fro loop not print out?

So I've been stuck trying to get my array list to print out in the right order but it keeps printing the original input i inserted backwards for some reason, i've tried reading the array in reverse order but it doesn't work either.
public static void Add()
{
System.out.println("You may now enter your virtual diary entry...");
System.out.println("You may END the program at any time by typing in endp...\n");
boolean loop = true;
while(loop)
{
String Stop = Cons.nextLine();
if (Stop.equals("endp")| Stop.equals(""))
{
readelements();
break;
} else {
for (int i =0 ; i <= Notes.size(); ) {
Notes.add(i, Stop);
i++;
break;
}
}
}
}
public static void readelements()
{
if (Empty()) {
Empty();
}
for(int i =0; i < Notes.size(); i++) {
System.out.println(i + " = " + Notes.get(i).toString());
Notes.toString();
}
}
In your else block, you break after one iteration (when i = 0) so you're always running Notes.add(0, Stop). This prepends Stop to Notes, so Notes will be in reverse order. Removing the break will cause you to insert duplicate elements into Notes (note that you're looping but always inserting Stop). Try changing your entire else block to just Notes.add(Stop);. This will add the current value of Stop to the end of Notes and should fix your problem.

Im not getting white this iteration doesent work?

When I output this album1.findSongInAlbum() method, no matter what I input I get "not working" or if I enter right name, I get album song found, and again also Not working.
public boolean findSongInAlbum(String songName) {
for (int i = 0; i < this.albumContainingListOfSongs.size(); i++) {
if (songName.equals(albumContainingListOfSongs.get(i).getTitle1())) {
System.out.println("Album song found: " + albumContainingListOfSongs.get(i).toString());
} else if (!songName.equals(albumContainingListOfSongs.get(i).getTitle1())) {
System.out.println("Not workin");
return false;
}
}
return true;
}
output is supposed to be one or another, either found song, or not found song, but never both.
Your logic is wrong.
If you find a match, you should return true immediately.
If you don't find a match, you should stay in the loop, and not return false. Only return false after the loop is done.
This should work:
public boolean findSongInAlbum(String songName) {
for (int i = 0; i < this.albumContainingListOfSongs.size(); i++) {
if (songName.equals(albumContainingListOfSongs.get(i).getTitle1())) {
System.out.println("Album song found: " + albumContainingListOfSongs.get(i).toString());
return true;
}
}
System.out.println("Album song not found");
return false;
}

Controlling a linked list queue from my user/main class, not working

Okay so I need to create queue(contains enqueue, dequeue, and isEmpty methods), queueNode(stores names, setters and getters), and queueUser(Main) classes. User enters one name at a time. Once 3 names have been entered, each new name entered will kick the front of the queue to the screen. This process will continue until the user enters "quit" as the name. Once this happens, all the remaining names will be displayed. I have everything but my user/main class. I can't seem to get it to work properly. It accepts 3 names, and after the third name is entered, displays the first name that was entered. Problem is, it stops there. I need it to keep going. I have a feeling I need a different loop, but I am just stuck right now. No help from my instructor, nothing in my textbook, and I have hit a brick wall. Any help or suggestions is greatly appreciated. I am still fairly new to Java and totally new to data structures.
queueUser
import java.util.Scanner;
public class queueUser {
public static void main(String[] args){
queue qName = new queue();
Scanner input = new Scanner(System.in);
int limit = 3;
String name;
String stop = "quit";
boolean flag = false;
do{
for(int i = 0; i < limit; i++){
System.out.println("Please enter one name: ");
name = input.nextLine();
if(!name.equalsIgnoreCase(stop)){
qName.enqueue(name);
flag = true;
}else{
flag = false;
while(!qName.isEmpty()){
System.out.println(qName.dequeue());
}
}
}
System.out.println(qName.dequeue());
}while(!flag);
}
}
queue
public class queue{
queueNode front, rear;
int count = 0;
public queue(){
front = null;
rear = null;
}
public boolean isEmpty(){
boolean empty = false;
if(front == null){
empty = true;
}else{
empty = false;
}
return empty;
}
public void enqueue(String pName){
queueNode node = new queueNode(pName);
if(isEmpty()){
front = node;
}else{
rear.setNext(node);
}
rear = node;
}
public String dequeue(){
String firstName = null;
if(isEmpty()){
System.out.println("Queue is empty!");
}else{
firstName = front.getName();
front = front.getNext();
}
return firstName;
}
}
queueNode
public class queueNode {
private String name;
private queueNode next;
public queueNode(String pName){
name = pName;
}
public void setNext(queueNode pNext){
next = pNext;
}
public queueNode getNext(){
return next;
}
public void setName(String pName){
name = pName;
}
public String getName(){
return name;
}
}
(Edit: your professor)
In your loop code you are saying this:
Give me three names, but I'll stop if any the most recent name given was "quit".
After you give me these three names, I will tell you whats on top of the queue.
Now do it all over again.
You are not removing from the queue when you repeat the loop until you have put in an additional 3 names. Even then, you will only dequeue the top, leaving 4 in the queue.
First and foremost, you have a variable int count in your queue class that is never used. If you use that, it will GREATLY improve the solution here. When something is enqueued, increment the count variable. Decrement when dequeued. Add a method that will return the count variable. Then just do this:
boolean flag = false;
do {
System.out.println("Please enter one name: ");
name = input.nextLine();
if(name.equalsIgnoreCase("stop")) flag = true;
if (!flag) qName.enqueue(name);
if (qName.getCount() > 3) System.out.println(qName.dequeue());
} while (!flag);
for (int x = 0; x < 3; x++) System.out.println(qName.dequeue());
If your instructor wants it otherwise, then your code should be as follows:
boolean flag = false;
int count = 0;
do {
System.out.println("Please enter one name: ");
name = input.nextLine();
if (name.equalsIgnoreCase("stop")) flag = true;
if (!flag) {
count++;
qName.enqueue(name);
}
if (count > 3) {
count--;
System.out.println(qName.dequeue());
}
} while (!flag);
for (int x = 0; x < 3; x++) System.out.println(qName.dequeue());
EDIT: All of these apply to your QueueUser class.
Make sure you CamelCase your class names.
The do-while loop is redundant since the flag starting value is false. You can accomplish the same thing by just making it a while loop (unless that was a requirement by your professor to have one).
while(!flag) {
//do your stuff
}
Completely get rid of the for loop.
The problem with your original code is that the flag values are switched around, the flag is already false so there is no need to declare that in the do-while loop. If you wanted to leave this redundant portion in it would need to be flag = false.
if(!name.equalsIgnoreCase(stop)){
qName.enqueue(name);
flag = true; //<-----SHOULD BE FALSE if you don't want to break the loop (is redundant)
}else {
flag = false //<-------CHANGE TO TRUE
Set the limit variable to 0 (or rename it count and set it to 0) to satisfy the count functionality your professor wants and add an if statement in your while loop to dequeue when it reaches 3.
count++
if(count >= 3) {
System.out.println(qName.dequeue());
}

I stuck with get out from while loop in method

I am writing program that asks user to type seven names of product.
what I try to do is if there is duplicate, then repeat the method.
I used while loop but I stuck.
If I put a,b,c,d,e,f,g at the first time, method ends and moves on to next method.
But if I typed a,a,b,c,d,e,f, program repeats same method and even if I type a,b,c,d,e,f,g, it gets in infinite loop.
here is my codes.
in main....
purchasedList.setShopList();
in purchasedList class...
public void setShopList() {
Scanner keyboard = new Scanner(System.in);
// print out description.
System.out.println("\n- Only have one entry of any type in the item list.");
System.out.println("- The name of items cannot be longer than 16 characters.");
System.out.println("\nType seven products.");
boolean sameNames = true;
while (sameNames == true) {
for (int i=0; i<7; i++) {
String n = keyboard.nextLine();
name.add(n);
name.set(i,name.get(i).toUpperCase());
}
sameNames = checkName();
}
}
// accessor.
public ArrayList<String> getShopList () {
return name;
}
// check duplicate.
public boolean checkName() {
Set<String> uniqueName = new HashSet<String>();
boolean foundName = false;
for (int i=0; i<7; i++) {
if (!uniqueName.add(name.get(i))) { // check duplicate
foundName = true;
}
}
if (foundName == true) {
System.out.println("※ There is a duplicate. Try it again.");
return true;
} else {
return false;
}
}
my checkName() method is fine because in my last project it worked.
in my last project, I put while loop in main like this
public static void main(String[] args) {
PurchasedList purchasedList = new PurchasedList();
.
.
.
boolean sameNames = true;
boolean tooLong = true;
while (sameNames == true || tooLong == true) {
System.out.println("\nType seven products.");
purchasedList.setShopList();
sameNames = purchasedList.checkName();
tooLong = purchasedList.checkLength();
}
but this time, because my professor wants me to make all operations are done within a method, so I try to fix.
I tried to solve it by myself in last 8 hours, but I could not get the solution.
Please help me.
Thank you.
add this line.
if (foundName == true) {
System.out.println("※ There is a duplicate. Try it again.");
-> name = new ArrayList<String>();
return true;
right now you are adding new names to the end of the array, and then setting them to upper case in the start of the array.
for (int i=0; i<7; i++) {
String n = keyboard.nextLine(); //Say I type in g on my second try
name.add(n); //This add g to the end of the array
name.set(i,name.get(i).toUpperCase()); //And this sets name[0] to G.
}
this means your name array is getting longer, instead of resetting.
Do you ever clean name? It appears you just keep adding to it, so the previous entrances are still there in loop's next round. And therefore there will always be a duplicate if you use same input as before (the order will not matter).
This change should do it:
while (sameNames == true) {
name = newArrayList <String>();
for (int i=0; i<7; i++) {
String n = keyboard.nextLine();
name.add(n);
name.set(i,name.get(i).toUpperCase());
}
sameNames = checkName();
}
So new name ArrayList is created every time. (Garbage collector will take care of the old ones, if necessary.) If name is already created somewhere else, then think if you really need it there - as far as I see you use it to gather input, and this happens in the method setShopList() so it appears you do not need it earlier than this.

Categories