1

これは、ユーザーにファイル名の入力を求める私の元のコードです。ただし、ユーザーが作業するには、コンソール内に書き込む必要があります。

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter file name: ");
        String filename = bf.readLine();
        File file = new File(filename);
        if (!filename.endsWith(".txt")) {
            System.out.println("Usage: This is not a text file!");
            System.exit(0);
        } else if (!file.exists()) {
            System.out.println("File not found!");
            System.exit(0);
        }

ここで、JOptionPaneを作成して、ペイン内に入力するようにユーザーに促したいと思いました。これは私のコードです。

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        JFrame frame = new JFrame();
        Object result = JOptionPane.showInputDialog(frame, "Enter a blog website");
        String word2 = (String) result;
        word2 = bf.readLine();
        File file = new File(word2);
        if (!word2.endsWith(".txt")) {
            System.out.println("Usage: This is not a text file!");
            System.exit(0);
        } else if (!file.exists()) {
            System.out.println("File not found!");
            System.exit(0);
        }

ユーザーの入力を受け取り、それらを処理するコーディングがさらにいくつかあります。ただし、joptionpaneを作成した後は、何も起こりません。joptionpaneが出てきましたが、入力を入力した後は何も起こりません。私の間違いはどこにあるのでしょうか?

4

1 に答える 1

2

値を読み取った後、値を上書きしています。

    Object result = JOptionPane.showInputDialog(null, "Enter a blog website");
    String word2 = (String) result;

    File file = new File(word2);
    if (!word2.endsWith(".txt")) {
        System.out.println("Usage: This is not a text file!");
        System.exit(0);
    } else if (!file.exists()) {
        System.out.println("File not found!");
        System.exit(0);
    }

JOptionPane を使用している場合、BufferedReader は必要ありません。

于 2011-04-17T17:32:08.373 に答える