0

Web ページに追加する前に編集する必要がある約 700 項目のリストがあります。各項目を手動で編集しようとしましたが、あまりにも広範囲になりました。編集する必要がある単語は各項目で同じ始まりと終わりを持っているため、代わりに Java を使用してファイルを読み取って編集することを考えました。

Q の単語をループして保存し、ロジックが機能するようになったら、テキスト ファイルの読み取り方法を見つけて、同じことをもう一度行うことから始めようと考えました。(他の方法があれば提案をお待ちしています) これまでにまとめたコードは次のとおりです。Java でコーディングしたのはずっと前だったので、現在は基本的にスキルがありません。

import javax.swing.JOptionPane;

public class CustomizedList
{

public static void main (String[] args)
{
    String Ord = JOptionPane.showInputDialog("Enter a word");
    String resultatOrd ="";

    for(int i = 0; i < Ord.length(); i++)
    {
        if(Ord.charAt(i) == 'y' && Ord.charAt(i) == 'e' && Ord.charAt(i) ==    

's')
        {
            resultatOrd += Ord.charAt(i);
            System.out.println(resultatOrd);
        }   

        else
        System.out.println("Wrong word.");
    }
}
}

何が間違っているのかわかりませんが、入力した単語が論理的に機能しません。このテキスト ファイルから削除したい単語が 2 つあります。小文字と大文字の YES と NO です。

4

3 に答える 3

5

あなたのコードは正しくありえません:

if(Ord.charAt(i) == 'y' && Ord.charAt(i) == 'e' && Ord.charAt(i) ==  's')

常にになります

解決:

Ord.toLower().contains("yes")

または(悪いですが、あなたの場合はまだ正しいです):

if(Ord.charAt(i) == 'y' && Ord.charAt(i+1) == 'e' && Ord.charAt(i+2) ==  's')

あなたが平等を探しているだけなら、あなたは使うことができますequals()

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#contains(java.lang.CharSequence

于 2012-07-30T17:22:42.460 に答える
1

あなたのifテスト:

if(Ord.charAt(i) == 'y' && Ord.charAt(i) == 'e' && Ord.charAt(i) == 's')

決して真実ではありません。同じ文字が3つの異なるものでなければならないことを指定しています。

String.equalsIgnoreCase必要な単語をテストするためのより良い方法については、メソッドを略奪してください。

例えば:

if (word.equalsIgnoreCase("yes") || word.equalsIgnoreCase("no"))
    // do something with word
于 2012-07-30T17:23:35.290 に答える
0

これがお役に立てば幸いです。各行が何をするのかを理解できるように、各部分にコメントを付けてみました。「はい」と「いいえ」が別々の行にある場合にのみ機能します。

I/O の Java チュートリアル リンクは次のとおりです。時間があるときに読むことをお勧めします。多くの良い情報Java I/O チュートリアル

import java.io.*;
import java.util.ArrayList;
public class test {

    public static void main(String[] args) throws Exception {
    //name of file to read
    File file = new File("filename.txt");

    //BufferedReader allows you to read a file one line at a time
    BufferedReader in = new BufferedReader(new FileReader(file));   

    //temporary Array for storing each line in the file
    ArrayList<String> fileLines = new ArrayList<String>();

    //iterate over each line in file, and add to fileLines ArrayList
    String temp=null;
    while((temp=in.readLine())!=null){
        fileLines.add(temp);        
    }
    //close the fileReader
    in.close();

    //open the file again for writing(deletes the original file)
    BufferedWriter out = new BufferedWriter(new FileWriter(file));

    //iterate over fileLines, storing each entry in a String called "line"
    //if line is equal to "yes" or "no", do nothing.
    //otherwise write that line the the file
    for(String line : fileLines){
        if(line.equalsIgnoreCase("yes")||line.equalsIgnoreCase("no")){
            continue;//skips to next entry in fileLines
        }
        //writes line, if the line wasn't skipped 
        out.write(line);
        out.write(System.getProperty("line.separator")); //newline
    }
    //save the new file 
    out.close();

    }

}
于 2012-07-30T17:56:25.730 に答える