I am trying to impliment a linkedquue for the first time and I'm having some troubles.
What I want:
I want to have my linkedQ read in words, then store them untill I use a command prompt ".".
for example, I typing in a sentence and it is stored, then I later typing in a"."and the words that are stored are then printed out.
What I have:
I think my Q is working properly. I can get the words to be stored and then printed out, but I cant isolate it to just the "." command. If I do a dot command , it instead started printing out what I type in, but it does not print out the old words I stored.
below is my main programs code :
Expand|Select|Wrap|Line Numbers
- / $Id: jroff.java,v 1.2 2011-01-20 21:20:03-08 - - $
- import java.io.*;
- import java.util.Scanner;
- import static java.lang.System.*;
- class jroff{
- static final String STDIN_NAME = "-";
- static void scanfile (String filename, Scanner infile) {
- int flag=0;
- linkedqueue <String> wordq = new linkedqueue <String> ();
- String mm="null";
- for (int linenr = 1; infile.hasNextLine(); ++linenr) {
- String line = infile.nextLine();
- if(line.trim().length()==0) {
- out.printf("the line is blank .sp1");
- }
- String[] words = line.split ("\\s+");
- if (words.length > 0 && words[0].startsWith (".")) {
- //wordq.remove ();
- //mm=wordq.remove ();
- flag=1;
- //out.printf("medic is sspy!");
- out.printf("you have initilaized a command of some sort");
- commands.cmd command = commands.cmd_map.get (words[0]);
- if (command == null) {
- auxlib.warn (filename, linenr, words[0],
- "invalid command");
- }else {
- command.exec (words);
- }
- }else if(flag==1){
- int counter=0;
- int width=50;
- for (String word: words) {
- //for (String test: line.split ("\\s+")) {
- if (word.length () == 0) continue;
- wordq.insert (word);
- wordq.remove ();
- mm=wordq.remove ();
- // }
- // buffer of sorts
- if(counter!=0){
- counter=counter+1+mm.length();
- }
- if(counter==0) {
- counter=mm.length();
- }
- if(counter>width) {
- out.printf("%n");
- counter=mm.length();
- }
- printparagraph(mm,width,counter);
- }
- out.printf("%n");
- }
- }
- }
- public static void main (String[] args) {
- linkedqueue <String> words = new linkedqueue <String> ();
- linkedqueue wordqueue = new linkedqueue ();
- if (args.length == 0) {
- out.printf("blank slate, nothing special in here, run normally");
- scanfile (STDIN_NAME, new Scanner (in));
- }else {
- for (String filename : args) {
- if (filename.equals (STDIN_NAME)) {
- out.printf("so you are utilizing that - thing, good job!");
- scanfile (STDIN_NAME, new Scanner (in));
- }else {
- try {
- Scanner scan = new Scanner (new File (filename));
- scanfile (filename, scan);
- scan.close();
- }catch (IOException error) {
- auxlib.warn (error.getMessage());
- out.printf("their is no file...why would you expect there to be?");
- }
- }
- }
- }
- }
- static void printparagraph(String word,int width,int charc) {
- //out.printf("TESTING TESTING READ ALL ABOUT IT");
- if(charc==1+word.length()) {
- out.printf("%s",word);
- }
- boolean first=true;
- if(charc==0 ||charc ==word.length()) {
- first=true;
- }
- if(charc !=0 && charc !=word.length()) {
- first=false;
- }
- if(charc >width) {
- first=true;
- }
- if(first==true) {
- out.printf("%s",word);
- }
- if(charc <= width && first==false && charc!=1+word.length()) {
- out.printf(" ");
- out.printf ("%s", word);
- }
- //new method perhaps
- if(word=="!"||word=="."||word=="?") {
- out.printf("%s",word);
- out.printf(" ");
- }
- /*if counter==0. print new word
- if filled add to counter
- if over width then print 2 spaces
- */
- }
- }
Expand|Select|Wrap|Line Numbers
- // $Id: linkedqueue.java,v 1.1 2011-01-20 21:05:43-08 - - $
- import java.util.NoSuchElementException;
- import java.io.*;
- import java.util.Scanner;
- import static java.lang.System.*;
- class linkedqueue <item_t> {
- //insert(item_t);
- private class node{
- item_t item;
- node link;
- }
- //
- // INVARIANT: front == null && rear == null
- // || front != null && rear != null
- // In the latter case, following links from the node pointed
- // at by front will lead to the node pointed at by rear.
- //
- private node front = null;
- private node rear = null;
- public boolean empty (){
- return front == null;
- }
- public void insert (item_t any) {
- // STUB: Add code here to insert an item_t into the queue.
- if(front==null) {
- node newnode = new node();
- newnode.item =any;
- //out.printf("%sNODE",any);
- front=newnode;
- rear=newnode;
- }
- else {
- node newnode= new node();
- newnode.item =any;
- newnode.link=front;
- front=newnode;
- // out.printf("%sNODE",any);
- }
- }
- public item_t remove(){
- item_t place= front.item;
- if(front==null){
- front=front.link;
- // out.printf("node?");
- }
- if(front==rear){
- //out.printf("node?");
- rear=null;
- // front=null;
- }
- //out.printf("PORTAL");
- //item_t place= front.item;
- //out.printf("%sOMGOMG\n",place);
- return place;
- }
- // if (empty ()) throw new NoSuchElementException ();
- // STUB: Add code for remove here.
- // return null; // STUB: Delete this return statement.
- // }
- }