Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions

Question

What is the growth rate of GDP per capita?

Answered: 1 week ago