0

So I've got a text file:

Name: Hulabaloo
Loading bay: 7
Container handling time: 50ms
Storage capacity: 70

and I want to extract this information to create an object in my class for similar variables as those in the text file. I've figured it out up till the point of reading the file, here's my code:

public class ThisClass
{
    private static final URL path = ThisClass.class.getResource("TextFile.info");
    private static final String TEXTFILE = path.getFile();

    private String name;
    private int loadingBays;
    private int containerHandlingTime;
    private int storageCapacity;

    public ThisClass(String name, int loadingBays, int containerHandlingTime, 
int storageCapacity)
    {
        this.name = name;
        this.loadingBays = loadingBays;
        this.containerHandlingTime = containerHandlingTime;
        this.storageCapacity = storageCapacity;
    }


    public static void main(String[] args) throws FileNotFoundException, IOException
    {
        BufferedReader br = null;
        FileReader fr = null;

        String sCurrentLine;

        br = new BufferedReader(new FileReader(TEXTFILE));

        while((sCurrentLine = br.readLine()) != null)
        {
            System.out.println(sCurrentLine);
        }           
    }
}

Well obviously all this does is print the textfile out. What I want to be doing is creating an object from the Harbour class and passing the values from the text file into the constructor. I thought trying to look for ": " in the text file and extracting from that point to the end of the line would be a good idea but I don't know how to do that. I would greatly appreciate any help on the matter, thanks in advance!

Romtromon
  • 5
  • 4
  • Possible duplicate of [How to split a string in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – Joe C May 06 '17 at 09:45
  • I guess you need to split every line by ":" and get the second index of splited array. – Borislav Kostov May 06 '17 at 09:47
  • Note: your program will fail once you pack the application into a jar. Since your file is a resource, it means it will be packed inside the jar, and will not be an actual file. This means you won't be able to create `TEXTFILE` and a `FileReader`. Always use `getResourceAsStream` to get the resource as an input stream rather than a file. – RealSkeptic May 06 '17 at 10:01
  • @RealSkeptic How would I go about assigning it to the TEXTFILE string after using getResourceAsStream? – Romtromon May 06 '17 at 10:21
  • You don't. You create a reader directly on the stream. You can't use a file when you don't have a file. – RealSkeptic May 06 '17 at 17:16

1 Answers1

0
public class ThisClass {
private static final URL path =      ThisClass.class.getResource("TextFile.info");
private static final String TEXTFILE = path.getFile();

private String name;
private int loadingBays;
private int containerHandlingTime;
private int storageCapacity;

public ThisClass(String name, int loadingBays, int containerHandlingTime,
                 int storageCapacity) {
    this.name = name;
    this.loadingBays = loadingBays;
    this.containerHandlingTime = containerHandlingTime;
    this.storageCapacity = storageCapacity;


}

public static void main(String[] args) throws FileNotFoundException, IOException {
    BufferedReader br = null;
    FileReader fr = null;

    String sCurrentLine;

    br = new BufferedReader(new FileReader(TEXTFILE));

    String name = "";
    int loading = 0;
    int time = 0;
    int capacity = 0;

    int count = 1;

    int elemCount = 0;

    ThisClass[] classArray = new ThisClass[2];

    while ((sCurrentLine = br.readLine()) != null) {

        System.out.println("sCurrentLine = " + sCurrentLine);
        if (sCurrentLine.toLowerCase().startsWith("name")) {
            name = sCurrentLine.split(":")[1].trim();
        } else if (sCurrentLine.toLowerCase().startsWith("loading")) {
            loading = Integer.parseInt(sCurrentLine.split(":")[1].trim());
        } else if (sCurrentLine.toLowerCase().startsWith("container")) {
            time = Integer.parseInt(sCurrentLine.split(":")[1].trim().replace("ms", ""));
        } else if (sCurrentLine.toLowerCase().startsWith("storage")) {
            capacity = Integer.parseInt(sCurrentLine.split(":")[1].trim());
        }
        if (count % 4 == 0) {
            classArray[elemCount] = new ThisClass(name, loading, time, capacity);
            elemCount += 1;

            System.out.println("name = " + name);
            System.out.println("loading = " + loading);
            System.out.println("time = " + time);
            System.out.println("capacity = " + capacity);
        }


        if (!sCurrentLine.trim().equalsIgnoreCase(""))
            count += 1;
    }

    for (ThisClass cl : classArray) {
        System.out.println("cl = " + cl);
    }
}

@Override
public String toString() {
    return "name: " + this.name
            + "loadingBays: " + this.loadingBays
            + "handlingTime: " + this.containerHandlingTime
            + "capacity: " + this.storageCapacity;
}

}

You can change the size of array as per your size. Please reply if you dont't get it.

pramesh
  • 1,914
  • 1
  • 19
  • 30
  • I have defined an array that would save object as long as it detects data in file. You can modify as per your wish. Some if techniques applied to use it as per necessity. – pramesh May 06 '17 at 11:14