2

所以,到目前为止,我已经浏览了这个论坛上的几乎所有建议,但没有一个能真正帮助我找到我需要的答案。

这是我的主要代码:我省略了工具,因为它工作正常。

public class Management {
    private static int Price1 = 0;
    private static int Price2 = 0;

    public static void main(String[] args) {
        try
        {
            Scanner file = new Scanner(new File("friendsfile"));
            String s;

            while(file.hasNext())
            {
                s = file.nextLine();
                if(s.contains("House 1"))
                {
                    Price1 = getPrice(s);
                }

                if(s.contains("House 2"))
                {
                    Price2 = getPrice(s);
                }
            }
            Filewriter fw = new Filewriter();
            fw.fileWriter(Price1,Price2);
        }
        catch(FileNotFoundException fnfe)
        {
            System.out.println("File not found");
        }
        catch(Exception e)
        {
            System.out.println("HERE IS AN ERROR MATE!");
        }
    }

    private static int getPrice(String s)
    {
        StringTokenizer st = new StringTokenizer(s,";");

        st.nextToken();
        int price1 = Integer.parseInt(st.nextToken().replace(" ",""));
        return price1;
    }
}

现在 Filewriter 看起来像这样:

public class Filewriter {

    public static void fileWriter(int price1, int price2)
    {
        System.out.println("Ye");
        final String FILE_PATH = "housesfile";
        final String MARKER_LINE1 = "price1";
        final String TEXT_TO_ADD1 = "price1: "+price1;
        final String MARKER_LINE2 = "price2";
        final String TEXT_TO_ADD2 = "price2: "+price2;

        List<String> fileLines = new ArrayList<String>();
        Scanner scanner = null;
        try {
            scanner = new Scanner(new File(FILE_PATH));
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                fileLines.add(line);
                if (line.trim().equalsIgnoreCase(MARKER_LINE1)) {
                    fileLines.add(TEXT_TO_ADD1);
                }
                if (line.trim().equalsIgnoreCase(MARKER_LINE2)) {
                    fileLines.add(TEXT_TO_ADD2);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }

        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new File(FILE_PATH));
            for (String line : fileLines) {
                pw.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (pw != null) {
                pw.close();
            }
        }
    }

}

基本上我想要:

House 1: price; 520592
House 2: price; 342038

一个文件中的那些价格数字,转换成这个文件,这是另一个文件。

house #1
owner: -NAME-
price1: 500000
info: 3 bedrooms, 3 bathrooms.
last changed: - DATE -
rented: no

house #2
owner: -NAME-
price2: 150000
info: 3 bedroom, 3 bathroom.
last changed: -date-
rented: no

现在,它并没有改变文件中的任何内容,这是为什么呢?请帮忙。

4

1 回答 1

0

那是因为您equalsIgnoreCase()Filewriter课堂上使用并将其与price1,进行比较price2。但是,您的文件具有类似price1: 500000.

因此,更改equalsIgnoreCase()contains()它应该可以工作。

更好的是,将两者都更改为小写或大写,然后匹配: line.trim().toLowerCase().contains(MARKER_LINE1.toLowerCase())

因此,您在 Filewrite 类中的 while 循环将是:

while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    fileLines.add(line);
    if (line.trim().toLowerCase().contains(MARKER_LINE1.toLowerCase())) {
        fileLines.add(TEXT_TO_ADD1);
    }
    if (line.trim().toLowerCase().contains(MARKER_LINE2.toLowerCase())) {
        fileLines.add(TEXT_TO_ADD2);
    }
}

编辑: 尝试并且您的代码有效,除了它还包括之前添加的price1,price2您可能想要排除/覆盖的值(在修改后的 if 语句本身上面):

house #1
owner: -NAME-
price1: 500000
price1: 520592
info: 3 bedrooms, 3 bathrooms.
last changed: - DATE -
rented: no

house #2
owner: -NAME-
price2: 150000
price2: 342038
info: 3 bedroom, 3 bathroom.
last changed: -date-
rented: no
于 2014-02-07T17:29:50.043 回答