DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Posts
    1

    Urgent help needed Please

    Hi all,

    Can someone please help me i am in a spot of bother, i need to get this little program going by monday morning and do not know how to do it, as everything i have tried hasn`t worked.

    This program is suposed to take a small text file and count the numbers of odd /even words as well as display the words. The program compiles but doesn`t run and gives me an error message that I don`t understand. I have attached the text file I am supposed to use to test the program.
    Could someone else please run it and see if they have any suggestions.....
    Thanks again folks



    import java.io.*;

    public class EvenOddCount {

    private static BufferedReader input;


    public void main (String [] args) {
    EvenOddCount count = new EvenOddCount();
    count.countWords(input);
    }

    private EvenOddCount() {
    input = openRead("C:\\TestFile.txt");
    }

    public BufferedReader openRead(String filename){
    try{
    BufferedReader input = new BufferedReader (new FileReader(filename));
    } catch (FileNotFoundException e) {System.exit(0);}
    return input;
    }

    public void countWords (BufferedReader input) {
    StreamTokenizer test = new StreamTokenizer(input);
    int evenCount = 0;
    int oddCount = 0;
    int charCount = 0;
    try {
    while (test.nextToken() != test.TT_EOF) {
    switch (test.ttype) {
    case StreamTokenizer.TT_WORD:
    charCount += test.sval.length();
    if (test.sval.length() % 2 == 0) {evenCount++;}
    else {oddCount++;}
    System.out.println(test.sval+ "[" + test.sval.length() + "]");
    break;
    }
    }
    } catch (IOException e) {System.exit(0);}
    int totalWords = evenCount + oddCount;
    System.out.println("There were " + totalWords + " words and " + charCount + " characters");
    System.out.println("There were " + evenCount + " even words, and " + oddCount + " odd words");
    }

    }
    Attached Files

  2. #2
    Join Date
    Apr 2004
    Posts
    1

    Cool

    Hi,

    Try This.....


    import java.io.*;

    public class EvenOddCount {

    private static BufferedReader input;

    private EvenOddCount() {
    input = openRead("C:\\TestFile.txt");
    }

    public BufferedReader openRead(String filename){
    try{
    /*
    * Remove BufferReader
    *
    * BufferedReader input = new BufferedReader (new FileReader(filename));
    *
    */

    input = new BufferedReader (new FileReader(filename));

    } catch (FileNotFoundException e) {
    System.out.println("Exception Occured!!........");
    System.exit(0);
    }

    return input;
    }

    public void countWords (BufferedReader input) {

    StreamTokenizer test = new StreamTokenizer(input);
    int evenCount = 0;
    int oddCount = 0;
    int charCount = 0;
    try {
    while (test.nextToken() != test.TT_EOF) {

    switch (test.ttype) {
    case StreamTokenizer.TT_WORD:
    charCount += test.sval.length();
    if (test.sval.length() % 2 == 0) {evenCount++;}
    else {oddCount++;}
    System.out.println(test.sval+ "[" + test.sval.length() + "]");
    break;
    }
    }
    } catch (IOException e) {System.exit(0);}

    int totalWords = evenCount + oddCount;
    System.out.println("There were " + totalWords + " words and " + charCount + " characters");
    System.out.println("There were " + evenCount + " even words, and " + oddCount + " odd words");
    }

    public static void main (String [] args) {
    EvenOddCount count = new EvenOddCount();
    count.countWords(input);
    }

    }

  3. #3
    Join Date
    Apr 2004
    Location
    Hyderabad
    Posts
    1
    Hai friend,
    Here's your work done, Try stoping redeclaring the same variable which will lead you to null pointer exception.

    import java.io.*;

    public class EvenOddCount {

    private static BufferedReader input;

    private EvenOddCount() {
    input = openRead("C:\\testfile.txt");
    }

    public static void main (String [] args) {
    EvenOddCount count = new EvenOddCount();
    count.countWords(input);
    }


    public BufferedReader openRead(String filename){
    try{
    input = new BufferedReader (new FileReader(filename));
    } catch (FileNotFoundException e) {System.exit(0);}
    return input;
    }

    public void countWords (BufferedReader input) {
    StreamTokenizer test = new StreamTokenizer(input);
    int evenCount = 0;
    int oddCount = 0;
    int charCount = 0;
    try {
    while (test.nextToken() != test.TT_EOF) {
    switch (test.ttype) {
    case StreamTokenizer.TT_WORD:
    charCount += test.sval.length();
    if (test.sval.length() % 2 == 0) {evenCount++;}
    else {oddCount++;}
    System.out.println(test.sval+ "[" + test.sval.length() + "]");
    break;
    }
    }
    } catch (IOException e) {System.exit(0);}
    int totalWords = evenCount + oddCount;
    System.out.println("There were " + totalWords + " words and " + charCount + " characters");
    System.out.println("There were " + evenCount + " even words, and " + oddCount + " odd words");
    }

    }
    Friend of All
    - Shiva

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links