13 Replies - 460 Views - Last Post: 25 June 2013 - 03:33 AM Rate Topic: -----

#1 judd123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 27-May 13

Learning to use java classes

Posted 22 June 2013 - 08:55 AM

Hey I'm learning to use classes in java, I am unable to figure out the compiler error for the class below, We have our own library called io.* which allows us students to use such calls as "ConsoleInput.readWord" etc

I am watching youtube tutorials for a beginner in trying to begin understand java classes.


import io.*;
public class Girlfriend
        {
        private String girlName;

                public Girlfriend (String name)
                        {
                        girlName=name;
                        }

                        public void setName(String name)
                                {
                                girlName=name;
                                }


                                public String getName()
                                {
                                return girlName;
                                }


                                        public void saying()
                                        {
                                        System.out.println("Your First Girlfriend was %s", getName());
                                        }

        }








and the compiler error is:

Girlfriend.java:28: cannot find symbol
symbol  : method println(java.lang.String,java.lang.String)
location: class java.io.PrintStream
                                        System.out.println("Your First Girlfriend was %s", getName());
                                                  ^
1 error






Thanks for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Learning to use java classes

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Learning to use java classes

Posted 22 June 2013 - 09:04 AM

Because you are using %s you need to use System.out.printf instead.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2832
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: Learning to use java classes

Posted 22 June 2013 - 09:04 AM

You mean printf, not println
Was This Post Helpful? 0
  • +
  • -

#4 judd123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 27-May 13

Re: Learning to use java classes

Posted 22 June 2013 - 09:06 AM

View PostRyano121, on 22 June 2013 - 09:04 AM, said:

Because you are using %s you need to use System.out.printf instead.

Okay so what would I do as an alternative because I can't use printf, I have to use println which is also apart of the io.* folder
Was This Post Helpful? 0
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Learning to use java classes

Posted 22 June 2013 - 09:08 AM

I have no idea what you are saying there, but if you really wanted to use println, you can just concatenate the two strings together

System.out.println("Your First Girlfriend was " + getName());

Was This Post Helpful? 0
  • +
  • -

#6 judd123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 27-May 13

Re: Learning to use java classes

Posted 22 June 2013 - 09:13 AM

View PostRyano121, on 22 June 2013 - 09:08 AM, said:

I have no idea what you are saying there, but if you really wanted to use println, you can just concatenate the two strings together

System.out.println("Your First Girlfriend was " + getName());


Girlfriend.java:28: ')' expected
System.out.println("Your First Girlfriend was " getName());
^
Girlfriend.java:28: illegal start of expression
System.out.println("Your First Girlfriend was " getName());
^
Girlfriend.java:28: not a statement
System.out.println("Your First Girlfriend was " getName());
^
Girlfriend.java:28: ';' expected
System.out.println("Your First Girlfriend was " getName());
^
4 errors



More errors?
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2832
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: Learning to use java classes

Posted 22 June 2013 - 09:16 AM

You didn't use what ryano121 posted
Was This Post Helpful? 0
  • +
  • -

#8 judd123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 27-May 13

Re: Learning to use java classes

Posted 22 June 2013 - 09:52 AM

View Postg00se, on 22 June 2013 - 09:16 AM, said:

You didn't use what ryano121 posted




import io.*;
public class Constructor
        {
                public static void main(String [] args)
                        {
                        String tempName = ConsoleInput.readWord("Enter Name of First girlfriend here");

                        Girlfriend GirlfriendObject = new Girlfriend();
                        GirlfriendObject.setName(tempName);
                        GirlfriendObject.saying();

                        }

        }






Constructor.java:9: cannot find symbol
symbol  : constructor Girlfriend()
location: class Girlfriend
                        Girlfriend GirlfriendObject = new Girlfriend();
                                                      ^
1 error


Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10814
  • View blog
  • Posts: 40,314
  • Joined: 27-December 08

Re: Learning to use java classes

Posted 22 June 2013 - 11:52 AM

A few things. First, please adopt an indentation convention that makes it easy to read and follow your code. Not to be rude, but the way you format your code doesn't lend itself to readability.

Second, your Girlfriend class constructor accepts a String. Per what you wrote: public Girlfriend (String name).
Was This Post Helpful? 0
  • +
  • -

#10 judd123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 27-May 13

Re: Learning to use java classes

Posted 22 June 2013 - 10:26 PM

View Postmacosxnerd101, on 22 June 2013 - 11:52 AM, said:

A few things. First, please adopt an indentation convention that makes it easy to read and follow your code. Not to be rude, but the way you format your code doesn't lend itself to readability.

Second, your Girlfriend class constructor accepts a String. Per what you wrote: public Girlfriend (String name).

That doesn't really help? I don't understand how that would solve my problem? please help.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10814
  • View blog
  • Posts: 40,314
  • Joined: 27-December 08

Re: Learning to use java classes

Posted 22 June 2013 - 10:28 PM

That does help. You need to pass a String to the constructor. You aren't doing such right now. In other words, your constructor method is expecting a parameter and you aren't passing it one.

Girlfriend GirlfriendObject = new Girlfriend("somename");


Was This Post Helpful? 0
  • +
  • -

#12 judd123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 27-May 13

Re: Learning to use java classes

Posted 23 June 2013 - 01:46 AM

View Postmacosxnerd101, on 22 June 2013 - 10:28 PM, said:

That does help. You need to pass a String to the constructor. You aren't doing such right now. In other words, your constructor method is expecting a parameter and you aren't passing it one.

Girlfriend GirlfriendObject = new Girlfriend("somename");


Sorry My mistake, I did't word it properly,

What I mean was that it doesn't help me work out what the problem is, like you and everyone else that replied probably knew what you meant, but I had no clue sorry I am not to good at any of this java stuff yet.
But none the less, thank you for you help!
Was This Post Helpful? 0
  • +
  • -

#13 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Learning to use java classes

Posted 23 June 2013 - 05:57 AM

View Postjudd123, on 23 June 2013 - 01:13 AM, said:

View PostRyano121, on 22 June 2013 - 09:08 AM, said:

I have no idea what you are saying there, but if you really wanted to use println, you can just concatenate the two strings together

System.out.println("Your First Girlfriend was " + getName());


Girlfriend.java:28: ')' expected
System.out.println("Your First Girlfriend was " getName());
^
Girlfriend.java:28: illegal start of expression
System.out.println("Your First Girlfriend was " getName());
^
Girlfriend.java:28: not a statement
System.out.println("Your First Girlfriend was " getName());
^
Girlfriend.java:28: ';' expected
System.out.println("Your First Girlfriend was " getName());
^
4 errors



More errors?


The problem with this is you forgot to type + between the string value and the getName() method. So g00se is right to say you didn't do what you were told. A very important member of the Computer Programmer's skill set is an eye for detail. The computer will do EXACTLY what you tell it to do, so make sure you tell it to do EXACTLY what you want it to do.
Was This Post Helpful? 0
  • +
  • -

#14 Barrett122  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 98
  • Joined: 22-February 13

Re: Learning to use java classes

Posted 25 June 2013 - 03:33 AM

Be a little mindful of Bucky's tutorials aswell. He explains things well but he tends to jump around a bit, its not very linear. Some of the things he mentions in certain tutorials he doesn't explain until much later and that makes it hard to use what you learn and adapt it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1