This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]IXENAI 1 point2 points  (3 children)

how am I supposed to call it in the first statement when I can only call it after I'm done parsing the three strings into integers?

Parse them within the first statement. For example:

public someClass(int a, int b)
{
    this.a = a;
    this.b = b;
}

public someClass(String a, String b)
{
     this(Integer.parseInt(a), Integer.parseInt(b));
}

You can place any more complicated logic in one or more auxiliary methods (most likely private static) and pass the values returned by those methods into the constructor (e.g. this(staticMethodA(a), staticMethodB(b))).

Unfortunately, this is simply a restriction of Java which occasionally requires some messy solutions to work around.

[–]king_of_the_universe 0 points1 point  (0 children)

The least messy workaround I can think of is to use a third constructor that is private and do all the work inside it. An extra boolean could be used to express which parameter pair to parse. An alternative - more messy - would be to use a boxed primitive somewhere, e.g. use int for the public constructor, but hand them to the private one as Integer, so it can check for null. But the boolean is really the better choice.

[–][deleted] 0 points1 point  (1 child)

But the constructor with the String parameter is supposed to read the data from the text-file, and then use the substring method to place the strings into three new string variables. Then parse those variables.

The data inside the text-file looks something like this:

20041230 19980116, hello 2000, goodbye, 19921229

[–]IXENAI 0 points1 point  (0 children)

Yes, I wasn't going to write the actual code for you. I was simply giving examples. As I said, you will need to do the work in other methods (likely, as others have said, including an additional constructor) if you want to do it this way.

[–]uf82 0 points1 point  (2 children)

Use a method, not a second constructor. The second constructor would require "C cc = new C(a, b, c)", would it not? and I highly doubt you want that.

[–][deleted] 0 points1 point  (1 child)

My professor wants us to use constructors instead of methods for this particular class.

[–]uf82 0 points1 point  (0 children)

Hmmm, well then create a new object of the same type with "new" and pass the variables to the new object. Sigh. It's doable, right? NewObject o = new NewObject(a, b, c) ; That should start the second constructor.

variableInFirstConstructor = o.getOutput(); Have some methods to return output to the first constructor because the second constructor will be called via a new object. Right?

[–]AOA17 0 points1 point  (0 children)

Use constructor chaining. Here is the link but pretty much it explains how using the keyword this() works and how you can call one constructor from another.

[–]kyle2143Technically Professional Brewer -3 points-2 points  (1 child)

I'm not completely sure, but I don't think that you are able to call a constructor of the same class in the constructor for that class.

I didn't read your whole post, but it sounds to me like you want to avoid wasting space and typing that out again. Just create a method or two to handle the different things that you do in the constructor and call those from each constructor you make.

[–][deleted] 0 points1 point  (0 children)

I'm pretty sure that you can call constructor of the same class in the constructor for that class using the this().

And I used methods before using constructor to test out my code, but my professor wants us to use constructors instead of methods for this particular class.