Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,219 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,628 people online right now. Registration is fast and FREE... Join Now!




Doesnt show frequency

 

Doesnt show frequency, working but run-time error

Public Designs

11 Mar, 2009 - 06:05 PM
Post #1

D.I.C Head
**

Joined: 8 Nov, 2008
Posts: 111


My Contributions
I have a letter frequency code working but when it runs it gives the value 0 for all letters which obviously is the problem



CODE
import java.io.FileReader;
import java.io.IOException;

public class BruteForce
{

      public static void main(String[] args )
      {
            if (args.length < 0)
            System.out.println("No filename specified");
            else
            try
            {

                  FileReader reader = new FileReader("ad.txt");
          }
          catch (IOException ioe)
          {
                  System.err.println(" Error on  opening file ad.data\n" + ioe.toString() );
                  System.exit(1);
          }
          try
          {
                  char[] capital = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N',
                                    'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

            char[] small =   { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                                  'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
                  System.out.println("Leter     Frequency");
                  FileReader reader = new FileReader("ad.txt");
                  int nextChar;
                  char ch;
                  int count=0;
            for (int i = 0; i < 26; i++)
            {


      while ( (nextChar = reader.read() ) != -1 )
      {

      ch = (char) nextChar;
      if( ch== capital[i] || ch == small[i] )
      {
       count++;
                }

      }

              System.out.print("  " + capital[i]);
              System.out.println("          " + count);

             }

                  reader.close();

                  System.exit(0);
            }
            catch( IOException ioe)
            {
                  System.err.println("Input/output error");
                  System.exit(2);
            }
      }
}


Here is my output

init:
deps-jar:
compile-single:
run-single:
Leter Frequency
A 0
B 0
C 0
D 0
E 0
F 0
G 0
H 0
I 0
J 0
K 0
L 0
M 0
N 0
O 0
P 0
Q 0
R 0
S 0
T 0
U 0
V 0
W 0
X 0
Y 0
Z 0
BUILD SUCCESSFUL (total time: 0 seconds)


User is offlineProfile CardPM
+Quote Post


pbl

RE: Doesnt Show Frequency

11 Mar, 2009 - 06:33 PM
Post #2

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,966



Thanked: 1188 times
Dream Kudos: 450
My Contributions
Kind of ackward:
- you open the file and you scan it for all "A"
- then you scan it for all "B"

you have to "rewind" "close/open" the file befor that because you will be at EndOfFile
if the file has 20,000 lines good luck

better to read the file only once an have an array of count
and use a Scanner a lot easier

CODE

import java.io.File;
import java.util.*;

public class BruteForce
{

    public static void main(String[] args )
    {
        char[] capital = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N',
                'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

        char[] small =   { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

        Scanner scan;
        try {
            scan = new Scanner(new File("BruteForce.java"));
        } catch (Exception e) {
            System.out.println("File not found");
            return;
        }
        int[] count = new int[26];
        while(scan.hasNextLine()) {
            String line = scan.nextLine();
            System.out.println("Line read: " + line);
            char[] digit = line.toCharArray();
            for(int i = 0; i < digit.length; i++) {
                for(int j = 0; j < 26; j++) {
                    if(digit[i] == capital[j] || digit[i] == small[j]) {
                        count[j]++;
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < 26; i++)
        {
            System.out.print("  " + capital[i]);
            System.out.println("          " + count[i]);

        }

    }
}

User is online!Profile CardPM
+Quote Post

Public Designs

RE: Doesnt Show Frequency

11 Mar, 2009 - 06:36 PM
Post #3

D.I.C Head
**

Joined: 8 Nov, 2008
Posts: 111


My Contributions
You are awesome. Thanks a bunch
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 10:23PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month