Results 1 to 13 of 13
  1. #1
    sinyi88 is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Question Why can not run?

    PHP Code:
    import java.util.*; import java.text.*; import java.io.*;
    public class wordCount2  // a very simple driver class
    {
      static final String USAGE="Usage:java wordCount2 files(s)";
      static String fi,fo;
      public static void main(String args[])
      {
        WC_Model model = new WC_Model();
        if (args.length != 2) {System.out.println(USAGE); return;}
        if (args.length>0) {fi=args[0];fo=args[1];}
        model.runFile(fi,fo); model.showRanMsg();
      }
    }
    class WC_Model
    {
      static boolean isRpt=false;
      static int t,tok,top,tokenCount;
      static String inFileName,intext,outFileName;
      static int wc[]=new int[500],wordCounts[]=new int[500];
      static String w[]=new String[500],words[]=new String [500];
      // class constructor and initializer
      public WC_Model() {top=0;tokenCount=0;}
      // observer methods for external display of results
      public String getData()
      {
        String data=new String(""), buf=new String(); TextIO io=new TextIO();
        File fi = new File(inFileName); io.openText(fi,"read");
        while ((buf=io.readText())!=null) {data+=buf+"\n";}
        io.closeText("read"); return data;
      }
      public int getTop() {return top;}
      public int getTok() {return tokenCount;}
      public int[] getWordCounts() {return wordCounts;}
      public String[] getWords() {return words;}
      // action method that does the analysis
      public void doCount(String msg)
      {
        String tokDelim=" \t\n\r\f,.;:~!@#$%^&*()+={}[]<>/\"";
        String buf=new String(),token=new String();
        StringTokenizer st; buf=msg;
        st=new StringTokenizer(buf,tokDelim);tokenCount+=st.countTokens();
        while (st.hasMoreTokens()) // make sure there are words to count
        {
          token=st.nextToken().toLowerCase(); // get token & decase
          // now search the array for existing occurance of word
          Boolean found=false;
          for (int idx=0; idx<=top; idx++)
          {
            if (token.equals(words[idx]) ) // is it in list already
               { wordCounts[idx]++; found = true; idx = top + 1;}
          }
          if (found == false)
             {words[top] = token; wordCounts[top] = 1; top++;} // new word
        }
      }
      public void runReport()
      {
        DecimalFormat df = new DecimalFormat("00000"); //fixed posn
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy MMM dd @ hh:mm");
        Date date = new Date(); String rptDate = sdf.format(date);
        TextIO io = new TextIO();
        File fo = new File(outFileName); io.openText(fo,"write");
        for (int idx=0;idx<t;idx++)     // display the results
          {io.writeText(df.format(wc[idx])+"  "+w[idx]);}
        io.writeText("-------------------");
        io.writeText(df.format(tok)+"  words in text!");
        io.writeText(df.format(t)  +"  unique words in text!");
        io.writeText("!"+"wcPlus report created on "+rptDate+"\n");
        io.closeText("write"); isRpt = true;
      }
      public void runFile(String s, String d)
      {
        inFileName=s;outFileName=d; // source and destination
        intext = getData();         // get contents of WS
        doCount(intext);            // let class do the work
        tok = getTok();t=getTop();  // fetch the results
        w = getWords(); wc = getWordCounts();
        runReport();
      }
      public void showRanMsg()
      {
    	if (isRpt) {System.out.println("Report has been created");}
      }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Are you getting any error messages? Is nothing happening at all? Please understand that the more detail you provide, the more specific the question, the greater our ability to help you. Good luck.

  3. #3
    sinyi88 is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Unhappy

    no errors...but it suppose to print out somethings when i run the command right? if i dont misunderstand it...it just print nothing except the USAGE string...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sinyi88 View Post
    no errors...but it suppose to print out somethings when i run the command right? if i dont misunderstand it...it just print nothing except the USAGE string...
    Then you're obviously using it wrong. Tell me,

    1) what conditions will cause the usage string to be displayed?
    2) have you corrected for these conditions?
    3) is this your code? (I already know the answer to this, and so...)
    4) whose code is this and how is it in your possession and what is your goal with it?

  5. #5
    sinyi88 is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    this is not my code...As i trying to learn about io...i need to understand it...i really dont know whether this code got problems or not....i suppose to print this...when i run the command to scan the text from a external text document right?

    PHP Code:
    for (int idx=0;idx<t;idx++)     // display the results
          {io.writeText(df.format(wc[idx])+"  "+w[idx]);}
        io.writeText("-------------------");
        io.writeText(df.format(tok)+"  words in text!");
        io.writeText(df.format(t)  +"  unique words in text!");
        io.writeText("!"+"wcPlus report created on "+rptDate+"\n");
        io.closeText("write"); isRpt = true;
      }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Let's look at what happens when you run the program though. You tell me that the USAGE String is shown, correct? If so, then the reason is obvious from this line in the program:
    Java Code:
    if (args.length != 2) {System.out.println(USAGE); return;}
    So, my next question of course is what parameters are you passing to this program when you run it? Do you pass it the input and output file names?

  7. #7
    sinyi88 is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    PHP Code:
    So, my next question of course is what parameters are you passing to this program when you run it? Do you pass it the input and output file names?
    do u mean enter the command "java wordCount2 filename"?If so,i did...

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    ah... that thick plottens...

    Java Code:
    if (args.length != 2) {System.out.println(USAGE); return;}
    What is the above telling you? Please explain. This will tell you what you have to do (like Fubarable commented).

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    sinyi88 is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Question

    args.length is not equal to 2...means not equal to 2 words?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yep, 2 it needs to have words passed to the program. Your post above only shows one word. If you delve deeper into the code, the first word appears to be the name of the input file and the second the name of the output file.

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Bingo !!! Give the man a cigar !!! yep 2 words... which are actually two names, which represent the names of two files, one an input file and the other an output file.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  12. #12
    sinyi88 is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    i think i know what's wrong already...thanks ya....

  13. #13
    sinyi88 is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •