Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2007

Tip: Looking for answers? Try searching our database.

Newbie Java Help!!!

Thread view: 
BlackJackal - 14 Jan 2007 18:11 GMT
Alright I don't know what is going wrong here.  When I complie the
class it works fine but when I try and compile a class that makes an
instance of the class I get the following error message

ShowStudent.java:9: 'void' type not allowed here
        System.out.println("Studend ID - " + newstudent.getid());
                           ^
ShowStudent.java:10: 'void' type not allowed here
        System.out.println("Credit Hours Earned - " +
newstudent.getch());
                           ^
ShowStudent.java:11: 'void' type not allowed here
        System.out.println("Points Earned - " + newstudent.getpe());
                           ^
ShowStudent.java:12: 'void' type not allowed here
        System.out.println("GPA - " + newstudent.getgpa());

Here is the class and instance.

   public class Student
  {
     private int id;
     private int ch;
     private int pe;
     private double gpa;
      public static void main(String[] args)
     {

     }
      public void setid(int a)
     {
        id = a;
     }
      public void setch(int a)
     {
        ch = a;
     }
      public void setpe(int a)
     {
        pe = a;
     }
      public void getid()
     {
        System.out.print(id);
     }
      public void getch()
     {
        System.out.print(ch);
     }
      public void getpe()
     {
        System.out.print(pe);
     }
      public void getgpa()
     {
        gpa = pe / ch;
        System.out.print(gpa);
     }
  }

   public class ShowStudent
  {
      public static void main(String[] args)
     {
        Student newstudent = new Student();
        newstudent.setid(1234);
        newstudent.setch(12);
        newstudent.setpe(48);
        System.out.println("Studend ID - " + newstudent.getid());
        System.out.println("Credit Hours Earned - " +
newstudent.getch());
        System.out.println("Points Earned - " + newstudent.getpe());
        System.out.println("GPA - " + newstudent.getgpa());
     }
  }
Knute Johnson - 14 Jan 2007 18:43 GMT
> Alright I don't know what is going wrong here.  When I complie the
> class it works fine but when I try and compile a class that makes an
[quoted text clipped - 71 lines]
>       }
>    }

Because you are trying to concatenate a String to a void.  Look at your
method getid(), it returns void.

What you wrote is effectively: System.out.println("string"+void).

As an aside, methods names should reflect what they do.  get??? should
return something not be a print method.  Your getID() should look like:

public String getID() {
    return studentID;
}

Signature

Knute Johnson
email s/nospam/knute/

Lew - 14 Jan 2007 20:13 GMT
BlackJackal wrote:
>>     public class Student
>>    {
[quoted text clipped - 4 lines]
>>        public static void main(String[] args)
>>       {

// main() does nothing, why is it here?

>>       }
>>        public void setid(int a)
>>       {
>>          id = a;
>>       }
...
>>        public void getid()
>>       {
>>          System.out.print(id);
>>       }
...
>>    }

> As an aside, methods names should reflect what they do.  get??? should
> return something not be a print method.  Your getID() should look like:
>
> public String getID() {
>     return studentID;
> }

As another aside, by convention the property name part of get... and set...
methods begins with an upper-case letter, but otherwise duplicates the case of
the private variable, which should begin with a lower-case letter. In general
all identifiers use camel case - the first letter is lower case for variables
or methods, upper case for class names, each subsequent word part begins with
an upper-case letter, the other letters are lower case. So your methods would
be getId() and setId(). Longer names would look like
"thisIsALongerVariableName" or "ThisIsALongerClassName".

- Lew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.