In: Computer Science
1. Write a queue client, "LineNum," that takes an integer command line argument “n” and prints the nth string from the first string found on standard input. [MO6.2]
Please note that you would need to use a queue to implement it for full credit. You should add the strings inputted by the user to a queue using the enqueue method. Then you should remove and return "n" strings from the queue using the dequeue method. The nth string that is returned by the dequeue method should then be printed out.
This is the code I have, but i keep getting errors.
public class LineNum { // public class LineNum
public static void main(String[] args) { // main method
int n = Integer.parseInt(args[0]); // variable n parses integers
from args
Queue<String> queue = new Queue<String>(); // new
String queue
//LinkedList<java.lang.String> Dequeue = new
LinkedList();
while (!StdIn.isEmpty()) { // while standard input is empty
String item = StdIn.readString();
// String item read string from standard input
if (!item.equals("-")) // if item equal - then ebqueue item
queue.enqueue(item);
else if (!queue.isEmpty()) // otherwise if queue is empty
StdOut.println(queue.dequeue() + " "); // print dequeue + a
space
}
StdOut.println(queue);
for (int i = 1; i < n; i++) // for loop i=1, <n, increments
i
queue.dequeue(); // dequeues element on each loop until i = n
// StdOut.print(queue.size() + " left on queue)");
// StdOut.println("The dequeue contains: " +queue.dequeue() + "
");
StdOut.println(n+ " from the first is " +queue.dequeue());
// Prints number from first and current element in dequeue
}
}
these are the errors i'm getting:
\introcs> javac LineNum.java
LineNum.java:5: error: cannot find symbol
queue<String> elements = new LinkedList<>(); // new
String queue
^
symbol: class queue
location: class LineNum
LineNum.java:5: error: cannot find symbol
queue<String> elements = new LinkedList<>(); // new
String queue
^
symbol: class LinkedList
location: class LineNum
LineNum.java:11: error: cannot find symbol
queue.enqueue(item);
^
symbol: variable queue
location: class LineNum
LineNum.java:12: error: cannot find symbol
else if (!queue.isEmpty()) // otherwise if queue is empty
^
symbol: variable queue
location: class LineNum
LineNum.java:13: error: cannot find symbol
StdOut.println(queue.dequeue() + " "); // print dequeue + a
space
^
symbol: variable queue
location: class LineNum
LineNum.java:16: error: cannot find symbol
StdOut.println(queue);
^
symbol: variable queue
location: class LineNum
LineNum.java:18: error: cannot find symbol
queue.dequeue(); // dequeues element on each loop until i = n
^
symbol: variable queue
location: class LineNum
LineNum.java:21: error: cannot find symbol
StdOut.println(n+ " from the first is " +Queue.dequeue());
^
symbol: variable Queue
location: class LineNum
8 errors
CODE
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main { // public class LineNum
public static void main(String[] args) { // main method
int n = Integer.parseInt(args[0]); // variable n parses integers from args
Queue<String> queue = new LinkedList<String>(); // new String queue
//LinkedList<java.lang.String> Dequeue = new LinkedList();
Scanner sc = new Scanner(System.in);
while(true) {
String item = sc.nextLine();
// String item read string from standard input
if (!item.equals("-")) // if item equal - then ebqueue item
queue.add(item);
else
break;
}
System.out.println(queue);
if (n > queue.size()) {
System.out.println("not enough elements...");
return;
}
for (int i = 1; i < n; i++) // for loop i=1, <n, increments i
queue.remove(); // dequeues element on each loop until i = n
// StdOut.print(queue.size() + " left on queue)");
// StdOut.println("The dequeue contains: " +queue.dequeue() + " ");
System.out.println(n+ " from the first is " +queue.remove());
// Prints number from first and current element in dequeue
}
}