1

我正在制作一个 Java 程序,该程序对用户键入的消息进行加密并将其返回给他们。它应该是读取文本区域中的文本并将其输出到一个文件中,然后将其逐个字符转换为编码消息,然后输出到另一个文件中,然后扫描并显示。现在我只是想让它输入文本,然后输出,然后逐个字符地获取并输出到另一个文件并读取和显示;基本上除了改变角色之外的一切。但是,只有消息或单词的第一个字符被输出到第二个文件并显示,我得到一个 java.util.NoSuchElementException 异常。将文本从文本区域获取到第一个文件可以正常工作。这是代码;

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

import javax.swing.*;

import java.io.*;

public class CodeMaker {
public File Cipher;
public int l;
public File message;
public char cipherChar;
public String finalCipher;
public Scanner charScan;
public Scanner scan;
public String cipherWord;
public int z;
public int words;
public File cipherFile;
public int n;
public String cipher;
public JPanel panel;
public JButton saveButton;
public JTextArea textArea;
boolean create = true;
private JFrame frame; 
public static void main (String[] args) {
    CodeMaker gui = new CodeMaker();
    gui.start();
}

private void start() {
    frame = new JFrame("Project TrenchCoat: Cipher Creator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setBackground(Color.DARK_GRAY);
    makeMenus();
    makeContent();
    frame.setVisible(true);
    frame.setExtendedState(Frame.MAXIMIZED_BOTH);

}

private void makeMenus() {
    JMenuBar menuBar;

    menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);
    JMenu menu;
    menu = new JMenu("File");
    JMenuItem menuItem = new JMenuItem("New Cipher");
    menuItem.addActionListener(new NewListener());
    menuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_N,
                Event.CTRL_MASK));

    menu.add(menuItem);

    JMenuItem menuitem = new JMenuItem("Read Cipher");
    menuitem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_R,
                Event.CTRL_MASK));

    menu.add(menuitem);
    menu.addSeparator();


    menuItem = new JMenuItem("Exit");
    menuItem.addActionListener(new ExitListener());
    menuItem.setAccelerator(
        KeyStroke.getKeyStroke(KeyEvent.VK_Q,
            Event.CTRL_MASK));
    menu.add(menuItem);
    menuBar.add(menu);
    JMenu helpMenu = new JMenu("Help");

    JMenuItem helpItem = new JMenuItem("Help");
    menuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_H,
                Event.CTRL_MASK));
    helpMenu.add(helpItem);
    menuBar.add(helpMenu);

    JMenu aboutMenu = new JMenu("About");

    JMenuItem aboutItem = new JMenuItem("About");
    menuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_A,
                Event.CTRL_MASK));
    aboutMenu.add(aboutItem);
    menuBar.add(aboutMenu);

}

private void makeContent() {
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new BorderLayout());

    JLabel label = new JLabel("Click a button to use its function");
    label.setForeground(Color.white);
    label.setFont(new Font("Lucida Console", Font.PLAIN, 14));
    contentPane.add(label, BorderLayout.NORTH);

    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));


    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

    panel.setBackground(Color.DARK_GRAY);

    JButton createButton = new JButton("Create Cipher");
    createButton.addActionListener(new NewListener());
    createButton.setBackground(Color.DARK_GRAY);
    createButton.setFont(new Font("Lucida Console", Font.PLAIN, 14));
    createButton.setForeground(Color.green);

    panel.add(createButton);
    JButton readButton = new JButton("Read Cipher");
    readButton.addActionListener(new ReadListener());
    readButton.setBackground(Color.DARK_GRAY);
    readButton.setFont(new Font("Lucida Console", Font.PLAIN, 14));
    readButton.setForeground(Color.green);
    panel.add(readButton);
    JButton aboutButton = new JButton("About Cipher Creator");
    aboutButton.setBackground(Color.DARK_GRAY);
    aboutButton.setFont(new Font("Lucida Console", Font.PLAIN, 14));
    aboutButton.setForeground(Color.green);
    panel.add(aboutButton);
    JButton helpButton = new JButton("Help");
    helpButton.setBackground(Color.DARK_GRAY);
    helpButton.setFont(new Font("Lucida Console", Font.PLAIN, 14));
    helpButton.setForeground(Color.green);
    panel.add(helpButton); 
    frame.add(panel, BorderLayout.WEST);
    JButton saveButton = new JButton("Save Cipher");
    saveButton.addActionListener(new SaveListener());
    saveButton.setBackground(Color.DARK_GRAY);
    saveButton.setFont(new Font("Lucida Console", Font.PLAIN, 14));
    saveButton.setForeground(Color.green);
    panel.add(saveButton); 
    textArea = new JTextArea(10,25);   
    contentPane.add(textArea, BorderLayout.CENTER);







}



private class NewListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        textArea.append("Type your message to be ciphered below, then click 'Save Cipher'. Remember, everything in the text area will be in your message, so delete this line.\n");

        }
    }
private class SaveListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        createCipher();
    }
}

private class ReadListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {


            JFileChooser fc = new JFileChooser();
            fc.showOpenDialog(frame);
            cipherFile = fc.getSelectedFile();
            if (cipherFile == null) {
                return;
        }

            readCipher();
        }
        }

    private void readCipher() {

        try
        {

            Scanner scan = new Scanner(cipherFile);
            while (scan.hasNext())
            {

                String cipher = scan.next();
                textArea.append(cipher);
                textArea.append("\n");


            }

            scan.close();
        }
        catch(IOException e)
        {
            JOptionPane.showMessageDialog(frame, 
                    "I/O error in file\n\n     " +
                            cipherFile.getName() +
                            "\n\nThis program will close", 
                    "I/O Error", 
                    JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
    }
    private void createCipher() {

        try
        {
            cipher = textArea.getText();
            PrintStream oFile = new PrintStream("Message.txt");
            message = new File("Message.txt");
            oFile.print(cipher);
            oFile.close();
        }
        catch(IOException ioe)
        {
            System.out.println("\n*** I/O Error ***\n" + ioe);
        }

        //problem is probably just below here


            try {
                 PrintStream oFile = new PrintStream("Cipher.txt"); 
                 File Cipher = new File("Cipher.txt");
                scan = new Scanner(message);
                while (scan.hasNext()) {

                cipherWord = scan.next();
                l = cipherWord.length();
                charScan = new Scanner(cipherWord);
                while (n<l) {
                     cipherChar = charScan.next().charAt(n);
                    oFile.print(cipherChar);
                }


                }
            }
            catch(Exception ioe) {

                System.out.println("\n*** Print to Cipher.txt Error ***\n" + ioe);

            }





            viewCipher();
    }

private void viewCipher() {
    textArea.append("Below is the cipher you created.\n Now you can copy/paste in and email it to the desired recipient.");



    try
    {
        File Cipher = new File("Cipher.txt");
        Scanner scan = new Scanner(Cipher);
        textArea.append("\n");

        while (scan.hasNextLine())
        {

            String cipher = scan.nextLine();
            textArea.append(cipher);
            textArea.append("\n");


        }


    }
    catch(IOException e)
    {
        JOptionPane.showMessageDialog(frame, 
                "I/O error in file\n\n     " +
                        cipherFile.getName() +
                        "\n\nThis program will close", 
                "I/O Error", 
                JOptionPane.ERROR_MESSAGE);
        System.exit(1);
    }







    }



private class ExitListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        System.exit(0);
    }
}
}

如果您运行该程序,应该会发生的是,当您在文本区域中输入并单击保存密码时,您输入的文本会输出回给您。非常感谢任何帮助......谢谢!

4

2 回答 2

0

您遇到的问题是扫描程序使用分隔符来检索文本,并且您的代码在找不到完整令牌时会犹豫不决。

于 2013-08-14T01:47:06.863 回答
0

在 createCipher 中,更改-

while (n<l) {
    cipherChar = charScan.next().charAt(n);
    oFile.print(cipherChar);
}

至-

for(int i = 0; i < cipherWord.length(); i++){
    cipherChar = cipherWord.charAt(i);
    oFile.print(cipherChar);
}

首先阅读 Scanner 及其方法,并在使用它们之前尝试了解它们。在finally块中关闭读取器/写入器。如果在打开或读取或写入文件时发生异常/错误,您将不会在当前程序中关闭它们,因为将执行catch并且不会关闭调用。

于 2013-08-14T01:56:02.957 回答