Question: This is a java file and queue question. I want to read a txt file which includes enqueue 1 dequeue 2 peek 3 size 4

This is a java file and queue question. I want to read a txt file which includes

"enqueue 1

dequeue 2

peek 3

size 4" something like this. When there is "enqueue", enqueue into the queue. And "dequeue", "peek" and "size". And I have a project can show you but it's broken. Please help me out.

public class queue { private String fileName = ""; private Queue queue; public queue(String fileNameIn) { fileName = fileNameIn; queue = new LinkedList(); } public void processFile() throws FileNotFoundException { Scanner in = null; try { in = new Scanner(new FileInputStream(fileName)); } catch(FileNotFoundException f) { System.out.println("File doesn't exist"); } while (in.hasNextLine()) { String line = in.nextLine(); Scanner parse = new Scanner(line); parse.useDelimiter(" "); String[] tokens = line.split(" "); String newString = parse.next(); if (newString.equals("enqueue")) { try { while(parse.hasNext()) { queue.enqueue(parse.nextInt()); } } catch(NumberFormatException n)

{ System.out.println("Invalid Argument"); } catch(IllegalStateException i)

{ System.out.println("Illegal State"); } } else if (newString.equals("peek")) { try { while(parse.hasNext() )

{ System.out.println(queue.peek()); } } catch(NumberFormatException n) { System.out.println("Invalid Argument"); } catch(IllegalStateException i) { System.out.println("Illegal State"); } } else if (newString.equals("dequeue")) { try { while(parse.hasNext()) { queue.dequeue(); } } catch(NumberFormatException n) { System.out.println("Invalid Argument"); } catch(IllegalStateException i) { System.out.println("Illegal State"); } } else if (newString.equals("size")) { try { while(parse.hasNext()) { System.out.println(queue.size()); } } catch(NumberFormatException n) { System.out.println("Invalid Argument"); } catch(IllegalStateException i) { System.out.println("Illegal State"); } } else { System.out.println("Invalid Command"); } parse.close(); } in.close(); }

public void printStructure() { while (!queue.isEmpty()) { System.out.println(queue.peek()); //System.out.println(queue.size()); queue.dequeue(); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!