Java:将数组移动到与文本有关的不同方法

时间:2014-02-26 03:47:30

标签: java arrays sorting text-processing

我需要帮助调整我的代码。我需要编写一个程序,在用户上传的txt文件中输出单个ascii字符的数量,但是我在尝试获取我计入“绘制的程序的GUI部分”中的数组时遇到了很多问题“屏幕上的数据。 我的输出看起来像我想要的,但我无法弄清楚如何让字符计数在那里

我想在用户在图形显示器上传的文件中输入字符/功能/号码的次数。例如,33或!有3个实例。或65 A文件中有4354个实例。但是我让计数器正确地计算单词中的字符有一个很大的问题,甚至更难将存储的字符数组到程序的GUI(g.draw)部分。

而不是数字,我只得到一个空白输出列。

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; 
        }
    }
}

1 个答案:

答案 0 :(得分:0)

只需将其添加到您的代码中:

g.drawString(Integer.toString(counter[j]), x + 120, y);

就在这里(在绘画方法中):

g.drawString(Integer.toString(j), x, y);
g.drawString(Character.toString((char)j), x + 50, y);
g.drawString(Integer.toString(counter[j]), x + 120, y);