Minerbob
Minerbob

Reputation: 459

Java Shared objects

I'm beginning to learn Java. I am stuck getting my simple program to work. The goal is to take a text file add it to a string value. Then modify the string. Display the original text Then display modified text.

The problem is both objects show the modified string. I understand the concept that if I said object = object2 and I modified object2 object one would be effected since I copied the reference to the object.

or is the text file considered a object in this case? How would I get around it?

import java.util.Scanner;
import java.io.File;

public class StartUpFile
{
    private String fileText;
    /**
     * Constructor for objects of class StartUpFile
     */
    public StartUpFile(String fileName)
    {
        readFile(fileName);
    }
    /**
     * Loads the text file into the class\object
     */
    public void readFile(String fileName)
    {
        fileText = "";
        try
        {
            Scanner file = new Scanner(new File(fileName));
            while (file.hasNextLine())
            {
                String line = file.nextLine();
                fileText += line + "\n";
            }
            file.close();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }

    public String showFileText()
    {
        return fileText;
    }

    public void modifyFileText()
    {       
        fileText = fileName.replaceAll("Q", "z");
    } 
}

public class ModifyStartupFile
{
    public static void main(String[] args)
    {
        StartUpFile startup = new StartUpFile("1.startup");
        StartUpFile startupModified = new StartUpFile("1.startup");

        System.out.println(startup.showText());

        startupModified.modifyFileText();
        System.out.println(startupModified.showText());
    }
}

File contents: QQQQQQQQQQQQQQQ

expected output:

QQQQQQQQQQQQQQQ zzzzzzzzzzzzzzz

Upvotes: 0

Views: 2963

Answers (3)

Minerbob
Minerbob

Reputation: 459

I feel like a dolt. The file I was using to test was quite large. My terminal was only giving me the output for part of the last modifed string values. So I made a assumption it was only printing the modified values.

I changed the blue-jay terminal to unlimited buffering and now it displays completely and correctly.

Thank you for the pointers in naming and for helping me with the code. I'm a dolt.

Upvotes: 0

Matt M
Matt M

Reputation: 36

In your code, you are attempting to create a Scanner object from a new File, and the file name you provided is an invalid file name.

Also, you tried to code two classes in one java file without making the ModifyStartupFile an inner class. You can create an inner class by following my example:

package test;

public class Tester {

public test.Tester.Hello getHelloObject() {
    return new Hello();
}

private class Hello {

    void sayHello() {
        System.out.println("hello");
    }
}

public static void main(String[] args) {
    Tester dude = new Tester();
    Hello man = dude.getHelloObject();
    man.sayHello();
}

}

Output:

hello

I fixed your code to make to compile by removing the class ModifyStartupFile. This class cannot be created inside another public class with an access specifier of public. In my example, the access specifier was private. The code still throws an FileNotFoundException, because the file name is invalid. Here is the end result of my modifications:

import java.util.Scanner;
import java.io.File;

public class StartUpFile {

private String fileText;

/**
 * Constructor for objects of class StartUpFile
 */
public StartUpFile(String fileName) {
    readFile(fileName);
}

/**
 * Loads the text file into the class\object
 */
public void readFile(String fileName) {
    fileText = "";
    try {
        Scanner file = new Scanner(new File(fileName));
        while (file.hasNextLine()) {
            String line = file.nextLine();
            fileText += line + "\n";
        }
        file.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

public String showFile() {
    return fileText;
}

public void modifyFile(String fileName) {
    fileText = fileName.replaceAll("Q", "z");
}

public static void main(String[] args) {
        StartUpFile startup = new StartUpFile("1.startup");
        StartUpFile startupModified = new StartUpFile("1.startup");

        System.out.println(startup.showFile());

        startupModified.modifyFile(startupModified.showFile());
        System.out.println(startupModified.showFile());
    }

}

Output:

java.io.FileNotFoundException: 1.startup (The system cannot find the file specified)
java.io.FileNotFoundException: 1.startup (The system cannot find the file specified)

Upvotes: 0

shmosel
shmosel

Reputation: 50726

The fileName parameter in modifyFile is unnecessary and misleading. The file has already been loaded, and the content is stored on the object, so the method just needs to modify it:

public void modifyFile() {
    fileText = fileText.replaceAll("Q", "z");
}

In your main method, simply call

startupModified.modifyFile();

Upvotes: 1

Related Questions