[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

java Count_no_of_words.java error

Hi
   i basically a MS tech programmer and a beginner in java .But as part of some course i have some java exams..I was just trying a program that i got from the material.Program to count no of words in a  file.. is as follows  

// To Count the no of words in a given file

import java.lang.System;
import java.lang.Exception;
import java.io.FileInputStream;
import java.lang.String;
public class Count_no_of_words
{
            public static void main(String s[])
      {
        try
         {
            FileInputStream f = new FileInputStream(s[0]);
            int word1 = 1,c,flag=0,i=0,j=0;
            String ch="",word="",arr1="";
            char ch1;
            String arr[] = new String[100];
            while(i<ch.length())
            {
              ch1 = ch.charAt(i);
              if(ch1=='')
                {
                   arr[j] = arr1;
                  flag = 0;
                   j++;
                  arr1="";
                    }
               else
                    arr1 = arr1 + ch1;
               i++;
            }
              {
             for(i=m+1;i<j;i++)
               if(arr[m].equals(arr[i]))
                  {
                    word1++;
                    arr[i]="";
                  }
               if(arr[m]!="")
                  System.out.println("The no of Times of occurence of the "+arr[m]+"is:"+word);
               word1=1;
            }
            f.close();                         
                              
           }       
          catch(Exception e)
         {
            System.out.println(e.getMessage());
           }            
            
      }

}


when i compile it shows me 3 errors ....

Count_no_of_words.java:21: empty character literal
                  if(ch1=='')
                          ^
Count_no_of_words.java:21: unclosed character literal
                  if(ch1=='')
                           ^
Count_no_of_words.java:29: ')' expected
                        arr1 = arr1 + ch1;

i am not able to debug this can someone help me... where do i specify the filename to read..as i/p argument ?? something like this
c:>java  Count_no_of_words text.txt       Is this Correct ????

Hope someone will help me...
0
jinupt
Asked:
jinupt
  • 6
  • 6
  • 3
  • +2
3 Solutions
 
petmagdyCommented:
use
if( ch.equals(otherChar))
0
 
CEHJCommented:
>>if(ch1=='')

should be

if(ch1==' ')

0
 
girionisCommented:
>  if(ch1=='')

It probably does not let you have an empty character. Try this

if(ch1==' ')
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
petmagdyCommented:
also:
                  arr1 = arr1 + ch1;
should be:
arr1[integerValue] = ch1
0
 
CEHJCommented:
>>
use
if( ch.equals(otherChar))
>>

No - java primitives (char here) don't have methods
0
 
petmagdyCommented:
oops if though it is ch u r right offourse
0
 
petmagdyCommented:
because ch is String i confused it with ch1
0
 
CEHJCommented:
You're missing the variable 'm' too. Having said all this, you might want to change the design as an array is not really suitable when the number of elements is unknown. See

http://www.geocities.com/marcoschmidt.geo/java-word-count.html
0
 
petmagdyCommented:
In generally we have 3 EJB models (Session stafeful, statless and Entity beans), entity beans usually is not part of the cluster as usually s called from Session beans locally, for the session model:

1- If statless it is very simple as no state replication is required it just load distributing between EJB Container instances
2- If statful a replication mechanism similar of the HTTP Session replication is needed
0
 
petmagdyCommented:
oops drop my last comment not for this thread!!!

very funny right???
0
 
CEHJCommented:
LOL
0
 
jinuptAuthor Commented:
i tried as the comments nothing working its showing more errors ....
0
 
CEHJCommented:
import java.lang.System;
import java.lang.Exception;
import java.io.FileInputStream;
import java.lang.String;
public class Count_no_of_words
{
          public static void main(String s[])
    {
      try
       {
          FileInputStream f = new FileInputStream(s[0]);
          int word1 = 1,c,flag=0,i=0,j=0, m=0;
          String ch="",word="",arr1="";
          char ch1;
          String arr[] = new String[100];
          while(i<ch.length())
         {
           ch1 = ch.charAt(i);
           if(ch1==' ')
             {
               arr[j] = arr1;
              flag = 0;
               j++;
              arr1="";
                    }
            else
                 arr1 += ch1;
            i++;
         }
            {
          for(i=m+1;i<j;i++)
            if(arr[m].equals(arr[i]))
              {
                word1++;
                arr[i]="";
              }
            if(arr[m]!="")
               System.out.println("The no of Times of occurence of the "+arr[m]+"is:"+word);
            word1=1;
         }
         f.close();

           }
          catch(Exception e)
       {
          System.out.println(e.getMessage());
          }

    }

}

(But it won't work)
0
 
jinuptAuthor Commented:
if it won't work whats the use my friend.... i am getting confused ..
0
 
CEHJCommented:
>>if it won't work whats the use my friend

See my last comment - you need to change the design
0
 
RuadRauFlessaCommented:
First problem is a stupid one since you have to read from a file in order to actually count the ammount of words within it.

Secondly, that is a stupid example and any study material you got with it should be thrown away immediately cuz it wil teach you verry bad habits.

Thirdly, try to never place any sugnificant code in the main method. That is why there are things such as Classes, Methods, Paramaters and such.

Fourthly, like someone said while(i<ch.length()) won't work since char is a primetive that is inherently a byte and thus an integer type which does not have any methods. besides the length of any char will always be 1 no matter what.

Fith, I take it that you are reading a ASCII file and for that it would be better to use a FileReader instead of a FileInputStream. I know that Input is not a topic to be taken lightly in Java but when reading plain text stick to a plain text reader. This will make everything so much more simple. For instance:
you can then do something like
String buffer = null;
while ((buffer = f.readLine()) != null) {
  //do with the buffer whatever you need to
}
this is much easier for you since you only want to count the ammount of actual words within the file so that brings me to another method within the String class called split(). String.split(String delim) ia a verry usefull method since you can take your string and split it into an array of strings and then use the String[].length property to get the ammount of words within the line. So that would change the code as such.

int wordCount = 0;
BufferedReader f = new BufferedReader(new FileReader(s[0]));
while ((buffer = f.readLine()) != null) {
  //do with the buffer whatever you need to
  String split = buffer.split(" ");
  wordCount += split.length;
}

and voila easy as that you have the ammount of words within the file.
0
 
jinuptAuthor Commented:
this look like s simple methid i tried this but was getting few errors here & there
0
 
girionisCommented:
I think me and CEHJ provided a valid solution although CEHJ got a bit further and gave more information, and other peopel also posted godo comments. I'd say split the points between all participants with more points awarded to CEHJ.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now