-4

I need help tweaking my code. I need to write a program that outputs the count of individual ascii characters in a txt file that the user uploads, but I'm having a lot of problems trying to get the array that I count into the GUI portion of the program that "draws" the data on the screen. I have the output looking how I want, but I can't figure out how to get the character count up there

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.io.FileReader;      // both needed
import java.io.BufferedReader;
import java.io.IOException;


public class textreader extends Frame implements ActionListener
{
String dataFilePath = null;
String dataFileName = null;
int[] counter = new int[256];

String command = "";

public static void main(String[] args)
{
    Frame frame = new textreader();
    frame.setResizable(true);
    frame.setSize(1000,850);
    frame.setVisible(true);
}

 public textreader()
{
     setTitle("Text File Processing");

    // Menu Creation
     MenuBar mn = new MenuBar();
    setMenuBar(mn);

    // Create "File" and add it
    Menu fileMenu = new Menu("File");
     mn.add(fileMenu);

    // Create Menu Items, Add action Listener, Add to "File" Menu Group

    // Open file
    MenuItem miOpen = new MenuItem("Open");
    miOpen.addActionListener(this);
    fileMenu.add(miOpen);

    // Process file
    MenuItem miProcess = new MenuItem("Process");
    miProcess.addActionListener(this);
    fileMenu.add(miProcess);

    // Exit program
    MenuItem miExit = new MenuItem("Exit");
    miExit.addActionListener(this);
    fileMenu.add(miExit);

    // To Terminate
    WindowListener d = new WindowAdapter()
    {
        public void windowClosing(WindowEvent ev)
        {
            System.exit(0);
        }

        public void windowActivated(WindowEvent ev)
        {
            repaint();
        }

        public void windowStateChanged(WindowEvent ev)
        {
            repaint();
        }
    };

    ComponentListener k = new ComponentAdapter()
    {
        public void componentResized(ComponentEvent e) 
        {
            repaint();           
        }
    };

    // listener registry
    this.addWindowListener(d);
    this.addComponentListener(k);
}

public void actionPerformed (ActionEvent ev)
{
// which command was issued?
command = ev.getActionCommand();

// act          
if("Open".equals(command))
{
    dataFilePath = null;
    dataFileName = null;

      JFileChooser chooser = new JFileChooser();
      chooser.setDialogType(JFileChooser.OPEN_DIALOG );
      chooser.setDialogTitle("Open Data File");

      int returnVal = chooser.showOpenDialog(null);
      if( returnVal == JFileChooser.APPROVE_OPTION) 
        {
          dataFilePath = chooser.getSelectedFile().getPath();
          dataFileName = chooser.getSelectedFile().getName();
        }
    repaint();
}
else
    if("Process".equals(command))
    {
        try 
        {   
            // Initialize
            int[] aCount = new int[256];

            // "Instantiate" streams
            BufferedReader inputStream  = new BufferedReader(new FileReader(dataFilePath));

            // read the file line by line and count the characters read

            String line = null;
            char c = 0;
            int lineLength = 0;
            int charValue = 0;

            while ((line = inputStream.readLine()) != null)
            {
            // *********  process line
                for (int i = 0; i < line.length(); i++)
                {
                    char ch = line.charAt(i);

                    if (ch >= 0 && ch <= 255)
                    {
                        counter[(int)ch]++;
                    }
                    else
                    {    // silently ignore non-ASCII characters
                    }
                    // count newline at the end
                    counter['\n']++;
                    }
                }
        }

        catch(IOException ioe)
        {
            System.out.print("You want to run that by me again?"); 
        }

        repaint();
    }
else
    if("Exit".equals(command))
    {
        System.exit(0);
    }

}
//********************************************************
//called by repaint() to redraw the screen
//********************************************************

public void paint(Graphics g)
{               
if("Open".equals(command))
{
    // Acknowledge that file was opened
    if (dataFileName != null)
    {
        g.drawString("File --  "+dataFileName+"  -- was successfully opened", 400, 400);
    }
    else
    {
        g.drawString("NO File is Open", 400, 400);
    }

    return; 
}
else
if("Process".equals(command))
    {
        for(int i = 0; i < 256; i++)
        {
             int x = 100;
             int y = 100;

             g.drawString("Int", x, y);
             g.drawString("Char", x+50, y);
             g.drawString("Count", x+100, y);
             g.drawLine(100, y+15, x+120, y+15);

             y = y + 30;

             int line = 0;

            for(int j = 0; j < 256; j++)
            {
                 line++;
                 g.drawString(Integer.toString(j), x, y);
                g.drawString(Character.toString((char)j), x + 50, y);   // Converts the # to a char, then to a String

                // This part of the code adds a new column when    the flag reaches 43

                if(line == 45)
                 {
                    x = x + 150;
                     y = 100;
                    g.drawString("Int", x, y);
                    g.drawString("Char", x+50, y);
                    g.drawString("Count", x+100, y);
                    g.drawLine(100, y+15, x+120, y+15);
                    y = y + 15;
                    line = 0;
                    }
                y = y+15;
                }

        }
        return; 
    }

    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

1

Your charValue variable appears to break your logic. You should remove it. This is sufficient:

for (int i=0; i<alphabetArray.length; i++)
    alphabetArray[i] = 0; // set initial values

while ((line = inputStream.readLine()) != null) {
    for(int i=0; i<line.length(); i++)
        alphabetArray[(int)line.charAt(i)]++; // use the ASCII value of the character as an index
}

Your alphabet counter also seems to go out of scope. Either (1) make alphabetArray an instance variable of the class, or (2) display its contents before it goes out of scope. I think #1 is preferable.

I'd also be concerned about this line:

System.out.println(c + "  : "+ char.alphabetArray[i]);

char is a datatype, and alphabetArray does not exist inside of it (and technically doesn't exist anywhere at this point since it's gone out of scope). c is also undefined. Take advantage of ASCII values! However, be careful printing non-printable characters and such. Your output will look really funky.

System.out.println((char)i + "  : "+ alphabetArray[i]);

Of course, you'd still need to make alphabetArray accessible somehow.

xikkub
  • 1,641
  • 1
  • 16
  • 28