0

In java is there a way to call a void method from a constructor.I try something like this but get an error message that the compiler can not find symbol method printThis(java.lang.String):

public class Date{

public Date(String inString){

  String s = inString;

   String b;


     b.printThis(s);

}


public void printThis(getString)

{

System.out.printf(System.out.printf( new SimpleDateFormat("MM/dd").format(new SimpleDateFormat("MM/dd").parse(getString) ) );

}
4
  • Whats with all the SimpleDateFormats. You are reading a string in MM/dd into a date and then formatting it MM/dd
    – pjp
    Commented Nov 27, 2009 at 16:06
  • I have a question how come every time I add a quesion up here it never colors all the code how did you that?
    – AlThePal78
    Commented Nov 27, 2009 at 16:08
  • To get your code formatted and colored, you have to either indent it by 4 spaces or hit the 010101 button while the text is selected. Commented Nov 27, 2009 at 16:15
  • I have edited most of your code to show the formatting but suggest you finish off tidying it up :-) Commented Nov 27, 2009 at 16:35

5 Answers 5

3

You want printThis(s) - the complier is looking for a printThis method on the String instance, which does not exist.

1
  • 1
    It would be best to make printThis method private or static. Else you risk exposing a partially constructed object if this class is subclassed and the method overridden. See javapractices.com/topic/TopicAction.do?Id=215
    – pjp
    Commented Nov 27, 2009 at 16:00
2

There are LOTS of errors in the code as presented. These are the ones I spotted.

public class Date{

Problem: you are missing package declaration means this will be in the default package. That's a bad idea.

Problem: you are using a class name that is the same as commonly used classes in the standard class library. That's a bad idea.

public Date(String inString){
    String s = inString;
    String b;
    b.printThis(s);

Error: The code attempts to invoke a method in the String API called printThis(...). No such method exists. You should probably get rid of b and just call printThis(s)

Error: The code attempts to use an uninitialized local (b) and this will give a compilation error (if you "fixed" the previous error by changing the type of b to something that did have a printThis method).

Problem: It is bad practice for a constructor to invoke a method on the object being constructed if there is any possibility that it might be overridden in a subclass. The problem is that the overriding method (from the subclass) might be called on the object before the superclass initialization has completed. It is safe to call static or private methods.

}

public void printThis(getString) {

Error: There is a syntax error in the declaration. Change getString to String getString.

Problem: The choice of parameter name is (IMO) nonsensical. What is a "get string"???

    System.out.printf(System.out.printf( 
        new SimpleDateFormat("MM/dd").format(
        new SimpleDateFormat("MM/dd").parse(getString) ) );

Error: Compilation error: the parentheses don't balance.

Error: Compilation error: the first argument to printf must be a String or a Locale. In your code, the first argument in the outer call is a PrintStream instance.

Error: System.out.printf(System.out.printf( is nonsensical. You almost certainly should use just System.out.println or System.out.print. If you do use a printf method you have to supply a format string in the syntax specified in the PrintStream javadocs. (This is NOT the same as the syntax used for date formats!!!)

}

Error: missing '}' to complete class.

Problem: Your code style needs a lot of work. If you can swear on a bible that nobody else is ever going to have to read your code (!), then I suppose its OK. Otherwise, this kind of stuff is unacceptable. If this was homework, I'd dock you 50% of your marks straight off for making no attempt to get the style correct.

1
  • "+1 but are you half man half compiler?" - invalid expression: expected operator but found but. :-)
    – Stephen C
    Commented Nov 28, 2009 at 2:29
1

You have used printThis() as a method of String. If you want to print the date you might want

printThis(s);

It's not generally a good idea to use the same class name (Date) as the JDK library class

2
  • thats what my teacher wants so yeah i thought the same thing.
    – AlThePal78
    Commented Nov 27, 2009 at 15:54
  • for all of you that corrected me thank you very much it worked just right
    – AlThePal78
    Commented Nov 27, 2009 at 15:57
0

The following lines are not going to work:

String b;
    b.printThis(s);

What the above code is doing is attempting to call the printThis method on the String object called b.

Since a String.printThis method does not exist, the compiler returns the error message saying that it could not find the method.

What is probably intended is the following:

printThis(s);

The above will call the printThis method of the current instance.

0

You're getting that error because you're trying to call printThis() on object b, which is a String. You want:

public Date(String inString) {
  printThis(inString);
}

Just FYI, it's generally inadvisable to name classes the same as JDK classes (like Date). Also the assignment you're doing of inString doesn't really achieve anything. Perhaps your code is a simplification of what you're doing but I thought I'd mention it anyway.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.