ユーザーが入力したメッセージを暗号化して返すJavaプログラムを作成中です。テキスト領域のテキストを読み取ってファイルに出力し、それを 1 文字ずつ取得してコード化されたメッセージに変換し、それを別のファイルに出力してからスキャンして表示することになっています。現在、テキストが入力され、出力され、文字ごとに取得され、別のファイルに出力され、読み取られて表示されるようにしようとしています。キャラ変更以外は基本的に全部。しかし、メッセージまたは単語の最初の文字のみが 2 番目のファイルに出力されて表示され、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);
}
}
}
プログラムを実行すると、テキスト領域に入力して暗号の保存をクリックすると、入力したテキストが出力されます。どんな助けでも本当に感謝しています...ありがとう!