<identifier> expected

Discussion in 'The Lounge' started by Keke101, Jan 2, 2012.

  1. Keke101

    Keke101

    Joined:
    Jan 2, 2012
    Messages:
    1
    Hi all I am taking an intro to programming (Java) class and I am working on an assignment and I keep getting this error. Here is my code and the error I am getting.

    // Class Phone represents a telephone object
    public class Phone
    {
    private static String firstname;
    private static String lastname;
    private static String number;

    public Phone()
    {
    }

    public Phone(String firstname1, String lastname1, String number1)
    {
    firstname = first1;
    lastname = last1;
    number = number1;
    }

    public static String getfirstname()
    {
    return firstname;
    }
    public void setfirstname(String first1);
    {
    firstname = first1;
    }
    public static string getlastname()
    {
    return lastname;
    }
    public void setlastname(String last1);
    {
    lastname = last1;
    }
    public static String getnumber()
    {
    return number;
    }
    public void setnumber(String number1);
    {
    number = number1;
    }
    System.out.println("First Name : " + getfirstname()); [error here]
    System.out.println("Last Name : " + getlastname()); [error here]
    System.out.println("Number : " + getnumber()); [error here]
    }
    }

    File: C:\Documents and Settings\Keke\Desktop\School\Programming\Lab03\Lesson3_5\src\Phone.java [line: 43]
    Error: C:\Documents and Settings\Keke\Desktop\School\Programming\Lab03\Lesson3_5\src\Phone.java:43: <identifier> expected

    Thank you in advance to anyone who can help. I would greatly appreciate it.
    Keke101, Jan 2, 2012
    #1
    1. Advertising

  2. Keke101

    tuttle64

    Joined:
    Feb 25, 2012
    Messages:
    1
    it seems you are a java beginner. anyway, the main errors are:

    1) if you want to assign the arguments of your constructor to the objects fields, the argument names must match the one given in the method. ex.

    public Phone(String firstname1, String lastname1, String number1)
    {
    firstname = firstname1;

    2) If you use a constructor to initialize the field variables you don't need any setter methods and you methods doesn't need to be static.

    3) The method signature doesn't end with a semicolon. so

    public void setfirstname(String first1);
    {

    will not run, but

    public void setfirstname(String first1)
    {

    is ok.

    4) Java is case sensitive, so an identifier getLastName will not be recognized by the compiler if you use getlastname.

    5) If you make a call to a method you must specify the object or if the method is static you must specify the class. only getfirstname() will not work.

    Here is the code that will compile and run:

    Code:
    public class Phone {
        private String firstname;
        private String lastname;
        private String number;
    
        public Phone() {
        }
    
        public Phone(String firstname1, String lastname1, String number1) {
            firstname = firstname1;
            lastname = lastname1;
            number = number1;
        }
    
        public String getfirstname() {
            return firstname;
        }
    
        public String getlastname() {
            return lastname;
        }
    
        public String getnumber() {
            return number;
        }
    
        public static void main(String[] args) {
            Phone p1 = new Phone("Albert", "Einstein", "01-11111");
            System.out.println("First Name : " + p1.getfirstname());
            System.out.println("Last Name : " + p1.getlastname());
            System.out.println("Number : " + p1.getnumber());
        }
    }
    and the output will be:

    Last edited: Feb 25, 2012
    tuttle64, Feb 25, 2012
    #2
    1. Advertising

  3. Keke101

    sandi7777777

    Joined:
    Oct 11, 2012
    Messages:
    1
    Location:
    Indonesia
    i get the same error. Could you tell me how to fix it ? I'm an ameteur about this.

    class encrypt1 {
    public static void main (String[] args) {
    String msg = "sasdasdasdasd";
    String msg2 = "";
    char c ='a';
    boolean b = true;
    for (int i = 0; i < msg.length(); i++) ;
    c = msg.charAt(i);
    if (msg.charAt(i) == 'm') {
    if (b) {
    c = 'a' ;
    b = false;
    } else {
    b = true;
    }
    }
    if (msg.charAt(i) == 'z') {
    c = 'i';
    }
    if (msg.charAt(i) == 'x') {
    c = 'e';
    }
    if (msg.charAt(i) == 'a') {
    c = 's';
    }
    msg2 = msg2 + c ;
    }
    System.out.println(msg2);
    }
    sandi7777777, Oct 11, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vigi98
    Replies:
    0
    Views:
    4,298
    vigi98
    Jul 15, 2003
  2. msnider9

    help with Java <identifier> expected

    msnider9, Apr 13, 2008, in forum: General Computer Support
    Replies:
    0
    Views:
    2,628
    msnider9
    Apr 13, 2008
  3. Tcole90

    JAVA < identifier > expected error

    Tcole90, Mar 29, 2010, in forum: General Computer Support
    Replies:
    0
    Views:
    4,371
    Tcole90
    Mar 29, 2010
  4. Zuni

    Error: <identifier> expected

    Zuni, Nov 14, 2011, in forum: Software
    Replies:
    0
    Views:
    1,571
  5. mdgtjulie

    <identifier> expected error in Java

    mdgtjulie, Feb 6, 2014, in forum: Software
    Replies:
    0
    Views:
    348
    mdgtjulie
    Feb 6, 2014
Loading...

Share This Page