I am currently new to Java and have no prior programming experience, I am currently trying to code a solution to a problem proposed during my university interview :)
//Winner rabbit variable to hold the winner of the 'race'
String winner;
winner = yettoracequeue.element();
Background about problem:
- assign the first item in queue to a string variable
- remove first item from queue
- assign the second item in queue to another string variable
- compare both variables and assign the answer to a new variable
I do not understand why yettoracequeue.element() is considered an object when the result is a string, e.g. Rabbit, and hence I am unable to assign it to the String variable that is winner.
TIA :)
Edit:
This is the full code
package queuepart;
import java.util.*;
public class QueuePart {
static String nextline = System.getProperty("line.separator");
public static void main(String[] args) {
//Step 1: Create LinkedList() to assign to yettoracequeue
Queue yettoracequeue = new LinkedList();
//Step 2: add rabbits to queue
int rabbitno = 1;
yettoracequeue.add("Rabbit" + rabbitno);
rabbitno++;
yettoracequeue.add("Rabbit" + rabbitno);
rabbitno++;
yettoracequeue.add("Rabbit" + rabbitno);
rabbitno++;
yettoracequeue.add("Rabbit" + rabbitno);
rabbitno++;
yettoracequeue.add("Rabbit" + rabbitno);
rabbitno++;
yettoracequeue.add("Rabbit" + rabbitno);
System.out.println(nextline + "Items in the queue" + yettoracequeue + nextline);
//Find first item in queue
System.out.println(nextline + "First item in queue is " + yettoracequeue.element());
//Assign First item in queue to racer
String winner = yettoracequeue.element();
}
}
yettoracequeue?Rabbitclass is not aStringQueue<String> yettoracequeue = new LinkedList<>(), see docs.oracle.com/javase/tutorial/java/generics/types.html