GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Mar-2008, 11:21
et21zero et21zero is offline
New Member
 
Join Date: Dec 2007
Posts: 13
et21zero is on a distinguished road

A program that calculates the number of characters in the text file


I have been creating a program that calculates the number of characters in the text file.

Question #1: How do I calculate all other characters in the text file?
Question #2: How do I calculate average word length and average sentence length?

JAVA Code:
import java.io.*; // classes for input/output
public class PunctuationCount //declare class PunctuationCount
{
public static BufferedReader inFile; // declare BufferedReader and use inFile for input
public static void main(String[] args) // main method throws IOException, StringIndexOutOfBoundsException 
throws IOException, StringIndexOutOfBoundsException
{
String line; // declare String line
inFile = new BufferedReader(new FileReader("history.dat")); // input text file history.dat
char symbol; // declare char symbol
int periodCt = 0; // declare integer periodCt and assign 0 to it
int commaCt = 0; //declare integer periodCt and assign 0 to it
int questionCt = 0; //declare integer commaCt and assign 0 to it
int colonCt = 0; // declare integer questionCt and assign 0 to it
int semicolonCt = 0; // declare integer semicolonCt and assign 0 to it
int exclamationPointCt = 0; // declare integer exclamationPointCt and assign 0 to it
int spaceCt = 0; // declare integer spaceCt and assign 0 to it
int upperCaseCt = 0; // declare integer upperCaseCt and assign 0 to it
int lowerCaseCt = 0; // declare integer lowerCaseCt and assign 0 to it
int digitCt = 0; //declare integer digitCt and assign 0 to it
 
 
while ((line = inFile.readLine())!= null) // read line and Loop until end of data
{
for (int count = 0; count < line.length(); count++)
{ // Loop until end of line
symbol = line.charAt(count);
 
switch(symbol){
case '.':
++periodCt;
break;
case ',':
++commaCt;
break;
case '?':
++questionCt;
break;
case ':':
++colonCt;
break;
case ';':
++semicolonCt;
break;
case '!':
++exclamationPointCt;
break;
case ' ':
++spaceCt;
break; 
case'A':case'B':case'C': case'D': case'E':case'F':case'G':case'H':case'I':case'J':case'K':case'L':case'M':case'N':case'O':case'P':case'Q':case'R':case'S':case'T':case'U':case'V':case'W':case'X':case'Y':case'Z':
++upperCaseCt;
break;
case'a':case'b':case'c':case'd':case'e':case'f':case'g':case'h':case'i':case'j':case'k':case'l':case'm':case'n':case'o':case'p':case'q':case'r':case's':case't':case'u':case'v':case'w':case'x':case'y':case'z':
++lowerCaseCt;
break;
case'0':case'1':case'2':case'3':case'4':case'5':case'6':case'7':case'8':case'9':
++digitCt;
break; 
}
}
}
System.out.println("The periods in the text are:" + " " + periodCt); // display output
System.out.println("The commas in the text are:" + " " + commaCt); // display output
System.out.println("The question marks in the text are:" + " " + questionCt); // display output
System.out.println("The colons in the text are:" + " " + colonCt); // display output
System.out.println("The semicolons in the text are:" + " " + semicolonCt); // display output
System.out.println("The exclamation points in the text are:" + " " + exclamationPointCt); // display output
System.out.println("The blank spaces in the text file are:" + " " + spaceCt); // display output 
System.out.println("The upper case letters in the text file are:" + " " + upperCaseCt); // display output
System.out.println("The lower case letters in the text file are:" + " " + lowerCaseCt); // display output
System.out.println("The digits in the text file are:" + " " + digitCt); // display output
}
}
Last edited by admin II : 03-Mar-2008 at 05:05. Reason: Changed [CODE] to [JAVA]
  #2  
Old 03-Mar-2008, 07:41
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: A program that calculates the number of characters in the text file


I think that one thing you could do to make it more simple is to load the entire file into a vector of Strings. This way, you can pass that vector to different functions that calculate different things. For example:

JAVA Code:
// Vector v is a Vector of Strings that represent the file
public int CountSpaces( Vector v )
{ int i,j,count=0;

  for(i=0;i<v.size();i++)
  { 
    for(j=0;j>v[i].length();j++)
    { if(v[i].charAt(j) == ' ') count++;
    }

  }

  return count;
}
  #3  
Old 03-Mar-2008, 12:30
et21zero et21zero is offline
New Member
 
Join Date: Dec 2007
Posts: 13
et21zero is on a distinguished road

Re: A program that calculates the number of characters in the text file


Thanks a lot for your help but I need to use a switch statement, that is why the code looks so boring.
  #4  
Old 03-Mar-2008, 12:52
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: A program that calculates the number of characters in the text file


Quote:
Originally Posted by et21zero
Question #1: How do I calculate all other characters in the text file?
Question #2: How do I calculate average word length and average sentence length?
It sounds to me like a StringTokenizer object would be useful to you. At its most basic level, it will separate a string into words for you. You could also set the delimiter to be a period character and it would separate the string into sentences. Here is an example:

JAVA Code:
public int CountWords( String EntireFile )
{ StringTokenizer st = new StringTokenizer( EntireFile ); // the default delimiter is any whitespace
  int count = 0;
  /*
  while( st.hasMoreTokens() ) 
  { st.nextToken();
    count++;
  }
  */
  count = st.countTokens();
  return count;
}
  #5  
Old 04-Mar-2008, 12:09
et21zero et21zero is offline
New Member
 
Join Date: Dec 2007
Posts: 13
et21zero is on a distinguished road

Re: A program that calculates the number of characters in the text file


Thanks a lot
  #6  
Old 28-Apr-2008, 12:04
JustinFox JustinFox is offline
Junior Member
 
Join Date: Mar 2008
Posts: 59
JustinFox will become famous soon enough

Re: A program that calculates the number of characters in the text file


for total character...

JAVA Code:

import java.util.Scanner;

public class A{

private Scanner scan = null;
private int TChars = 0;
private String temp = "";
private TWords = 0;

private void getAllCharacterAndAVGWordLength(String fileName)
{
     try{
     scan = new Scanner(fileName);

     while(temp = scan.next())
     {

         TChars += temp.length();
         TWords += 1;

      }
     }catch(Exception e){}
}

System.out.println("Number of characters was: " + TChars);
System.out.println("Number of words was: " + TWords);
System.out.println("Avg length of a word was: " + Tchars/TWords);

private void getAVGSentences(String fileName)
{

Scanner scan = null;

try{

scan = new Scanner(fileName);
int LineCount = 0;
int LineLength = 0;
String temp = "";

while(temp = scan.nextLine())
{

  LineCount += 1;
  LineLength += temp.length();

}

System.out.println("The total lines were: " + LineCount + "\nThe avg. length of a line was: " + LineLength/LineCount);

}catch(Exception e){}

}

}




Justin Fox
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Imagegalore -- Free image host, no registration needed! Qoozz Free Web Hosting 0 03-Jan-2006 05:47
Program Needed: Perfect Number Program in C language koool_kid C Programming Language 12 02-Dec-2005 14:02
Free 1st month / Free setup / No credit card needed...Plans start at 4.95 LarryIsaac Web Hosting Advertisements & Offers 0 11-Oct-2003 14:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:17.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.