Search code examples
javaclasstext-filesselectionchanging

Changing a specific line in a file from another one


So, I have looked through just about every suggestion on this forum so far and none of them can really help me towards the answer that I need.

Here is the my main code: I've left out the implements as this works fine.

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;
    }
}

Now the Filewriter looks like this:

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();
            }
        }
    }

}

Basically I want:

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

Those price numbers from one file, converted into this file, which is another one.

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

Now, it doesn't change anything in the file, why is this? Please help.


Solution

  • That is because you use equalsIgnoreCase() in your Filewriter class and compare it with price1, price2. However, your file has values like price1: 500000.

    Hence, change equalsIgnoreCase() to contains() and it should work.

    Better, change both to lower or upper cases and then match: line.trim().toLowerCase().contains(MARKER_LINE1.toLowerCase())

    Hence, you while loop within Filewrite class would be:

    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);
        }
    }
    

    EDIT: Tried and your code works, except it also includes the previously added price1, price2 values which you may want to exclude/overwrite (in the modified if statements above itself):

    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