473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using a linkedQueue properly.

16
hello and thank you for clicking on my question!

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
  1. / $Id: jroff.java,v 1.2 2011-01-20 21:20:03-08 - - $
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5. import static java.lang.System.*;
  6.  
  7. class jroff{
  8.  
  9.  
  10.    static final String STDIN_NAME = "-";
  11.  
  12.    static void scanfile (String filename, Scanner infile) {
  13.  
  14.  
  15. int flag=0;
  16.     linkedqueue <String> wordq = new linkedqueue <String> ();
  17.     String mm="null";
  18.       for (int linenr = 1; infile.hasNextLine(); ++linenr) {
  19.          String line = infile.nextLine();
  20.             if(line.trim().length()==0) {
  21.                 out.printf("the line is blank .sp1"); 
  22.             }
  23.         String[] words = line.split ("\\s+");
  24.             if (words.length > 0 && words[0].startsWith (".")) {
  25.                 //wordq.remove ();
  26.                 //mm=wordq.remove ();
  27.                 flag=1;
  28.                 //out.printf("medic is sspy!");
  29.             out.printf("you have initilaized a command of some sort");
  30.             commands.cmd command = commands.cmd_map.get (words[0]);
  31.             if (command == null) {
  32.                auxlib.warn (filename, linenr, words[0],
  33.                      "invalid command");
  34.             }else {
  35.                command.exec (words);
  36.             }
  37.          }else if(flag==1){
  38.             int counter=0;
  39.             int width=50;
  40.                 for (String word: words) { 
  41.             //for (String test: line.split ("\\s+")) {
  42.                 if (word.length () == 0) continue;
  43.                wordq.insert (word);
  44.                wordq.remove ();
  45.                mm=wordq.remove ();
  46.            // }
  47.  
  48.          // buffer of sorts
  49.              if(counter!=0){
  50.                     counter=counter+1+mm.length();
  51.                 }
  52.                 if(counter==0) {
  53.                     counter=mm.length();
  54.                 }
  55.                 if(counter>width) {
  56.                     out.printf("%n");
  57.                     counter=mm.length();
  58.                 }
  59.                 printparagraph(mm,width,counter);
  60.  
  61.             }
  62. out.printf("%n");
  63.          }
  64.       }
  65.  
  66.    }
  67.  
  68.    public static void main (String[] args) {
  69.  
  70.       linkedqueue <String> words = new linkedqueue <String> ();
  71.       linkedqueue wordqueue = new linkedqueue ();
  72.       if (args.length == 0) {
  73.         out.printf("blank slate, nothing special in here, run normally");
  74.          scanfile (STDIN_NAME, new Scanner (in));
  75.       }else {
  76.          for (String filename : args) {
  77.             if (filename.equals (STDIN_NAME)) {
  78.                 out.printf("so you are utilizing that - thing, good job!");
  79.                scanfile (STDIN_NAME, new Scanner (in));
  80.             }else {
  81.                try {
  82.                   Scanner scan = new Scanner (new File (filename));
  83.                   scanfile (filename, scan);
  84.                   scan.close();
  85.                }catch (IOException error) {
  86.                   auxlib.warn (error.getMessage());
  87. out.printf("their is no file...why would you expect there to be?");
  88.                }
  89.             }
  90.          }
  91.       }
  92.    }
  93.  
  94.  static void printparagraph(String word,int width,int charc) {
  95. //out.printf("TESTING TESTING READ ALL ABOUT IT");
  96.         if(charc==1+word.length()) {
  97.             out.printf("%s",word);
  98.         }
  99.         boolean first=true;
  100.         if(charc==0 ||charc ==word.length()) {
  101.             first=true;
  102.         }
  103.         if(charc !=0 && charc !=word.length()) {
  104.             first=false;
  105.         }
  106.         if(charc >width) {
  107.             first=true;
  108.         }
  109.         if(first==true) {
  110.             out.printf("%s",word);
  111.         }
  112.         if(charc <= width && first==false && charc!=1+word.length()) {
  113.             out.printf(" ");
  114.             out.printf ("%s", word);
  115.         }
  116. //new method perhaps
  117.         if(word=="!"||word=="."||word=="?") {
  118.             out.printf("%s",word);
  119.             out.printf("  ");
  120.         }
  121.  
  122. /*if counter==0. print new word
  123. if filled add to counter
  124. if over width then print 2 spaces
  125.  
  126.  
  127. */
  128.     }
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. }
  137.  
  138.  
  139.  
and here is the code for my LINKEDQUEUE
Expand|Select|Wrap|Line Numbers
  1. // $Id: linkedqueue.java,v 1.1 2011-01-20 21:05:43-08 - - $
  2.  
  3. import java.util.NoSuchElementException;
  4. import java.io.*;
  5. import java.util.Scanner;
  6. import static java.lang.System.*;
  7.  
  8. class linkedqueue <item_t> {
  9.  
  10.    //insert(item_t);
  11.    private class node{
  12.       item_t item;
  13.       node link;
  14.    }
  15.  
  16.    //
  17.    // INVARIANT:  front == null && rear == null
  18.    //          || front != null && rear != null
  19.    // In the latter case, following links from the node pointed 
  20.    // at by front will lead to the node pointed at by rear.
  21.    //
  22.    private node front = null;
  23.    private node rear = null;
  24.  
  25.    public boolean empty (){
  26.       return front == null;
  27.    }
  28.  
  29.    public void insert (item_t any) {
  30.       // STUB: Add code here to insert an item_t into the queue.
  31.         if(front==null) {
  32.         node newnode = new node();
  33.         newnode.item =any;
  34.         //out.printf("%sNODE",any);
  35.         front=newnode;
  36.         rear=newnode;        
  37.        }
  38.           else {
  39.         node newnode= new node();
  40.         newnode.item =any;
  41.         newnode.link=front;
  42.         front=newnode;
  43.  
  44.        // out.printf("%sNODE",any);
  45.             }
  46.    }
  47.  
  48.    public item_t remove(){
  49. item_t place= front.item;
  50.         if(front==null){
  51.       front=front.link;
  52.           // out.printf("node?");
  53. }
  54.         if(front==rear){
  55. //out.printf("node?");
  56.         rear=null;
  57.       //  front=null;
  58.         }
  59.  
  60. //out.printf("PORTAL");
  61. //item_t place= front.item;
  62. //out.printf("%sOMGOMG\n",place);
  63. return place;
  64.     }
  65.  
  66.  
  67.  
  68.  
  69.    //   if (empty ()) throw new NoSuchElementException ();
  70.       // STUB: Add code for remove here.
  71.    //   return null; // STUB: Delete this return statement.
  72.  //  }
  73.  
  74. }
  75.  
please help me sold this problem
Apr 23 '11 #1
0 1274

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Matthias S. | last post by:
Hi, I have a class (say Company) which should expose a ReadOnlyCollection property (say EmployeesCol) hosting userdefined types (Employee). Since only the internals of the Company class should...
3
by: john_teague | last post by:
I am using a statement similar to this: using(SqlDataReader dr = ...){ dr.Read(); return Fill(dr); } will the using tag properly destroy the data reader after the return statement?
4
by: A_StClaire_ | last post by:
hi, my poker game does playersActiveInRound = players; at the start of every round where both are vector<Player> (i.e., containers of a user-defined class). the latter vector is everyone...
3
by: Geoff Tanaka | last post by:
Hi, I have created a DIB in C# and I am having a problem using it in a C++ DLL. Basically I have a C++ DLL which requires a DIB handle to be passed into it. I create the DIB using: IntPtr myDIB...
0
by: Bill | last post by:
On an ASP.NET 2.0 server, is there a way I can use urlMappings with Classical ASP pages? The web.config file, in my web root, is this: <?xml version="1.0" ?> <configuration...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
5
by: Olan Meier | last post by:
Hi, I like to define scope in a method to ensure that variables donøt get used more than once, and that things are nicely disposed. I use the Using-keyword most of the time - but what do one use...
53
by: Vicent Giner | last post by:
Hello. I am new to Python. It seems a very interesting language to me. Its simplicity is very attractive. However, it is usually said that Python is not a compiled but interpreted programming...
1
by: shawnwperkins | last post by:
Hi Guys, I'm new to Javascript and have a couple of questions that I hope someone could quickly answer for me. First, I'm trying to modify an example Ajax script to fit my needs. The script...
1
by: Marco van der M | last post by:
Hello everyone, I am trying to make an insert query that inserts information using form values and a query that gets the id from the name of a field in the form. I got a form that shows object...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.