2 Replies - 6857 Views - Last Post: 16 February 2010 - 05:51 PM Rate Topic: -----

#1 mudijo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-November 09

Java Word Count

Posted 16 February 2010 - 05:17 PM

I am trying to write a program that will count the words that are separated by blanks in a string. This is what I have so far. I keep getting the following errors -
java:15: '{' expected public class JTextArea
java:16: '{' expected public static void linecount (String fName, BufferedReader in) throws IOException
java:118: '}' expected }


import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class WordCount
public class JTextArea
public static void linecount (String fName, BufferedReader in) throws IOException

{
  long numChar =0;
  long numLine=0;
  long numWords =0;
  String line;
  JTextArea = new textArea(5, 20);
  JScrollPane scrollPane = new JScrollPane(textArea);
  textArea.setEditable(false);

    do
    {
      line = in.readLine();

      if (line != null)
      {
        numChar += line.length();
        numWords += wordcount(line);
        numLine++;
      }
    {
		while(line != null);
		System.out.println("File Name: " + fName);
		System.out.println("Number of characters: " + numChar);
		System.out.println("Number of words: " + numWords);
		System.out.println("Number of Lines: " + numLine);
	}

  public static void linecount(String fileName)
  {
    BufferedReader in = null;

    try
    {
      FileReader fileReader = new FileReader(fileName);
      in = new BufferedReader(fileReader);
      linecount(fileName,in);
     }
    catch(IOException e)
    {
      e.printStackTrace();
    }
  }

  public static long wordcount(String line)
  {
    long numWords = 0;
    int index = 0;
    boolean prevWhiteSpace = true;

    while(index < line.length())
    {
      char c = line.charAt(index++);
      boolean currWhiteSpace = Character.isWhitespace(c);

      if(prevWhiteSpace && !currWhiteSpace)
      {
        numWords++;
      }
      prevWhiteSpace = currWhiteSpace;
     }

    return numWords;
  }

  public static void main(String[] args)
  {
    long numChar = 0;
    long numLine=0;
    String line;

    try
    {
      if (args.length == 0)
      {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        line = in.readLine();
        numChar = line.length();

        if (numChar != 0)
        {
          numLine=1;
        }

        System.out.println("Number of characters: " + numChar);
        System.out.println("Number of words: " + wordcount(line));
        System.out.println("Number of lines: " + numLine);
     }

      else
      {
        for(int i = 0; i < args.length; i++)
        {
          linecount(args[i]);
        }
      }
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java Word Count

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Java Word Count

Posted 16 February 2010 - 05:32 PM

look at that code:
public class WordCount 
public class JTextArea 
public static void linecount (String fName, BufferedReader in) throws IOException 
{


it is really difficult to understand what you are trying to do...
when declaring a public class, most comilers requires a new file for each public class.
anyway, when declaring a class, at least have curling braces in the begining, and at the end:
public class WordCount {//start class
//code
}//end class



JTextArea is a class in java.
are you trying to rewrite it?
maybe it is a mistake and you tried to import the class:
import javax.swing.JTextArea;


so your code will look as:
public class WordCount {
public static void linecount (String fName, BufferedReader in) throws IOException 
{//code..



if I may suggest, i took a look at your code in charge of counting number of words in a given String.
you may want to take a look at the split(String regex) method of the String class:
http://java.sun.com/...va.lang.String)

the method gets a String and "parses" it by the given regex. the method returns an array of String objects, so all you will have to do is get the length of that array.
as simple as:
//the sentence
String st = "ab as sdf asf";
//the array of words, the regex is " " (blank space)
String[] words = st.split(" ");
//words contains:
//{"ab" , "as", "sdf", "asf"}
//and the length is 4 (which is the number of words)


Was This Post Helpful? 1
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Java Word Count

Posted 16 February 2010 - 05:51 PM

Another mistake I noticed:
  JTextArea = new textArea(5, 20);

You never give a variable name for the JTextArea, and unless textArea is something you created, do new JTextArea
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1