Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm tring to search a word in my stack from a txt file but when ever I run the program it seem as if it's

I'm tring to search a word in my stack from a txt file but when ever I run the program it seem as if it's not searching at all. However inorder for this program to work you need to creat your own random file that matches P4Input.txt with only letter no numbers so that you can search in the file. JAVA

import java.util.*; import java.nio.file.*; import java.io.*; import java.text.*;

public class P4Program{ private DecimalFormat formatter; // Formation for the numbers private String fileName; private String word; private String outputFile; // File name for the Output file private String outputString; private final String LS = System.lineSeparator(); private int count; private double startTime; // Wall clock starting time for int private double endTime; // Wall clock ending time for int private double startSystemTime; // CPU clock startindg time for int private double endSystemTime; // CPU clock starting time for float private double lapsedTime_SLL; private double lapsedTime_LL; private double lapsedTime_SC; private double lapsedTime_SLL_Que; private double lapsedTime_LL_Que; private double lapsedTime_SLL_ForCpu; private double lapsedTime_LL_ForCpu; private double lapsed_SC_ForCpu; private double lapsed_SLL_Que_ForCpu; private double lapsedTime_LL_Que_ForCpu; private double lapsed_DL; private double lapsed_DL_ForCpu; private final double NON_SECONDS = 1_000_000_000.0; private final double MILLI_SECONDS = 1_000.0;

/** * Instantiate all the data structures we'll be testing. */ public P4Program(){ formatter = new DecimalFormat("0.0000"); fileName = null; word = null; outputFile = "P4Output.txt"; count = 1; }// end of constructor public void mainMenu(){ Scanner scan = new Scanner(System.in); SimpleLinkedListStack list = new SimpleLinkedListStack (); P4Program loadList = new P4Program(); //System.out.println("Search word is..." + searchWord()); //System.out.println("Please enter the Input file name... "); //fileName = scan.nextLine(); //outputToFile(); //loadList.linkedStackFromFile(); //loadList.stackFromFile(); //loadList.linkedListFromFile_Queue(); //loadList.simpleLinklistFromFile(); //loadList.simpleLinkedlistFromFile_Queue(); //loadList.doublyLinkedFromFile(); } public String searchWord(){ Scanner scan = new Scanner(System.in); SimpleLinkedListStack list = new SimpleLinkedListStack (); LinkedStack listOne = new LinkedStack(); Stack listTwo = new Stack(); DoublyLinkedList listThree = new DoublyLinkedList(); SimpleLinkedListQueue listFour = new SimpleLinkedListQueue(); LinkedQueue listFive = new LinkedQueue(); System.out.println("Please enter the search word... "); word = scan.nextLine(); LinkedStack temp = listOne; SimpleLinkedListStack tempOne = list; Stack tempTwo = listTwo; DoublyLinkedList tempThree = listThree; LinkedQueue tempFour = listFive; SimpleLinkedListQueue tempFive = listFour; while (!temp.isEmpty()) { if (temp.pop() == word){ System.out.print("The word from linked stack is " + word); } } /**else if(tempOne.pop() == word){ System.out.print("The word from simple linked list is " + word); } else if(tempTwo.pop() == word){ System.out.print("The word from stack class is " + word); } else if(tempThree.printList() == word){ System.out.print("The word from stack class is " + word); } else if(tempFour.dequeue() == word){ System.out.print("The word from stack class is " + word); } else if(tempFive.dequeue() == word){ System.out.print("The word from stack class is " + word); } else{ System.out.println("No such word from the list"); } }*/ System.out.println(" search word... " + word); return word; }

public void linkedStackFromFile(){ lapsedTime_LL = 0.0; lapsedTime_LL_ForCpu = 0.0; String content = new String(); int count = 1; File file = new File("P4Input.txt"); LinkedStack list = new LinkedStack(); startTime = System.currentTimeMillis(); startSystemTime = System.nanoTime(); try { Scanner sc = new Scanner(new FileInputStream(file)); while (sc.hasNextLine()){ content = sc.nextLine(); list.push(content); } sc.close(); }catch(FileNotFoundException fnf){ fnf.printStackTrace(); } catch (Exception e) { e.printStackTrace(); System.out.println(" Program terminated Safely..."); } LinkedStack temp = list; while (!temp.isEmpty()) { System.out.print("Node " + (count++) + " : "); System.out.println(temp.pop()); } /**while (!temp.isEmpty()) { String found = temp.pop(); if ( found == word){ System.out.println("The word from linked stack is " + word); } }*/ endTime= System.currentTimeMillis(); endSystemTime = System.nanoTime(); lapsedTime_LL = (endTime - startTime)/MILLI_SECONDS; lapsedTime_LL_ForCpu = (endSystemTime - startSystemTime)/NON_SECONDS; }

public static void main (String [] arg)throws IOException{

P4Program mainClass = new P4Program(); mainClass.mainMenu(); mainClass.searchWord(); mainClass.linkedStackFromFile(); //mainClass.stackFromFile(); //mainClass.linkedListFromFile_Queue(); //mainClass.simpleLinklistFromFile(); //mainClass.simpleLinkedlistFromFile_Queue(); //mainClass.outputToFile(); //mainClass.doublyLinkedFromFile(); } }

import java.io.*; import java.util.*; public class LinkedStack{ private int n; // size of the stack private Node first; // top of stack

// helper linked list class private class Node { private Item item; private Node next; }

/** * Initializes an empty stack. */ public LinkedStack() { first = null; n = 0; }

/** * Is this stack empty? * @return true if this stack is empty; false otherwise */ public boolean isEmpty() { return first == null; }

/** * Returns the number of items in the stack. * @return the number of items in the stack */ public int size() { return n; }

/** * Adds the item to this stack. * @param item the item to add */ public void push(Item item) { Node oldfirst = first; first = new Node(); first.item = item; first.next = oldfirst; n++; }

/** * Removes and returns the item most recently added to this stack. * @return the item most recently added * @throws java.util.NoSuchElementException if this stack is empty */ public Item pop() { if (isEmpty()) throw new NoSuchElementException("Stack underflow"); Item item = first.item; // save item to return first = first.next; // delete first node n--; return item; // return the saved item } }

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

Students also viewed these Databases questions

Question

What are the primary responsibilities of the Federal Reserve Board?

Answered: 1 week ago

Question

What is a moving-average crossover rule?

Answered: 1 week ago

Question

Outline psychological and social-cultural triggers of aggression.

Answered: 1 week ago

Question

What is the preferred personality?

Answered: 1 week ago