Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with my program! Pls HELP!

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Problem with my program! Pls HELP!

    Alright guys.. I have two classes in my program, called Name and NameTester..

    Here are the codes for both of them:

    /**
     * Write a description of class Name here.
     * 
     * @author Adi Malicbegovic
     * @version 03/20/2013
     */
    public class Name
    {   
            private String firstName = "";
            private String middleName = "";
            private String lastName = "";
     
     
            public void name(String first, String middle, String last)
            {
                firstName = first;
                middleName = middle;
                lastName = last;
            }
     
            public String firstMiddleLast()
            {
                return firstName + " " + middleName + " " + lastName;
            }
     
            public String lastFirstMiddle()
            {
                return lastName + "," + firstName + " " + middleName;
            }
     
            public boolean equals(Name otherName)
            {
                boolean eqf = this.firstName.equalsIgnoreCase(otherName.firstName);
                boolean eqm = this.middleName.equalsIgnoreCase(otherName.middleName);
                boolean eql = this.lastName.equalsIgnoreCase(otherName.lastName);
     
                return eqf && eqm && eql;
            }
     
            public String initials()
            {
                String newStr = firstName.substring(0,1) + middleName.substring(0,1) + lastName.substring(0,1);
                return newStr.toUpperCase();            
            }
     
            public int length()
            {
                String str = this.firstMiddleLast();
     
                int len = str.length();
                return len; 
            }
     
    }


    import java.util.Scanner;
    public class TestNames
        {
            public static void main(String [] args)
            {    
                System.out.println("Name of first person...");
                Name name = new Name();
     
                Scanner input = new Scanner(System.in);
                System.out.println("Enter your first name: ");
                double firstName = input.nextDouble();
     
                System.out.println("Enter your middle name: ");
                double middleName = input.nextDouble();
     
                System.out.println("Enter your last name: ");
                double lastName = input.nextDouble();
     
     
                System.out.println("Name of second person...");
                Name otherName = new Name();
     
                Scanner input2 = new Scanner(System.in);
                System.out.println("Enter your first name: ");
                double first = input2.nextDouble();
     
                System.out.println("Enter your middle name: ");
                double middle = input2.nextDouble();
     
                System.out.println("Enter your last name: ");
                double last = input2.nextDouble();
     
     
                System.out.println("Information about the first person:");
                System.out.println("Full name: " + firstMiddleLast());
                System.out.println("Last name: " + lastFirstMiddle());
                System.out.println("Initials: " + initials());
                System.out.println("Name length: " + length());
     
                System.out.println("Information about the second person:");
                System.out.println("Full name: " + firstMiddleLast());
                System.out.println("Last name: " + lastFirstMiddle());
                System.out.println("Initials: " + initials());
                System.out.println("Name length: " + length());
     
                if (name == otherName)
                    System.out.println("The names are the same.");
     
                else
                    System.out.println("The names are not the same.");
            }
     
        }


    In the first class when I go to compile it is showing me that there is no syntax errors, but when I go to the second class and click compile it says that it cannot find method firstMiddleLast()... I guess that it should be the same error for the other methods: lastFirstMiddle(), initials(), length()... If someone can help I would really appreciate it.

    Thank you


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Problem with my program! Pls HELP!

    The firstMiddleLast() method is a method of the Name class, and to call it, you must call it on a Name object. You're calling it as if it were a static method of the NameTest class, and it's not. So find the appropriate Name object (there's one there that you can use), and call the method on it.

    As an aside, why are you asking Scanner to get nextDouble()? Are the names you're trying to get numbers? Or are the Strings like most names that I know of. Also, you'll need to put data into your Name object before you can call methods to extract information out of it.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Problem with my program! Pls HELP!

    Also, you'll need to put data into your Name object before you can call methods to extract information out of it.
    Is it in the Name class, or TestNames class? And what exactly do you mean by data?


    import java.util.Scanner;
    public class TestNames
        {
            public static void main(String [] args)
            {    
                System.out.println("Name of first person...");
                Name name = new Name();
     
                Scanner input = new Scanner(System.in);
                System.out.println("Enter your first name: ");
                String firstName = input.next();
     
                System.out.println("Enter your middle name: ");
                String middleName = input.next();
     
                System.out.println("Enter your last name: ");
                String lastName = input.next();
     
     
                System.out.println("Name of second person...");
                Name otherName = new Name();
     
                Scanner input2 = new Scanner(System.in);
                System.out.println("Enter your first name: ");
                String first = input2.next();
     
                System.out.println("Enter your middle name: ");
                String middle = input2.next();
     
                System.out.println("Enter your last name: ");
                String last = input2.next();
     
     
                System.out.println("Information about the first person:");
                System.out.println("Full name: " + firstName + " " + middleName + " " + lastName);
                System.out.println("Last name: " + lastName + "," + firstName + " " + middleName);
                //System.out.println("Initials: " + initials());
                //System.out.println("Name length: " + length());
     
                System.out.println("Information about the second person:");
                System.out.println("Full name: " + first + " " + middle + " " + last);
                System.out.println("Last name: " + last + "," + first + " " + middle);
                //System.out.println("Initials: " + initials());
                //System.out.println("Name length: " + length());
     
     
                if (name != otherName)
                    System.out.println("The names are not the same.");
     
                else
                    System.out.println("The names are the same.");
            }
     
        }

    Here is what I did.. And now I can get good output for names, but the next thing is initials and length.. It is showing me the same as before that it cannot find neither method initials() nor length()..


    EDIT: Also I tried to put Name name = new name(firstMiddleLast) and it is saying that it cannot find firstMiddleLast method

Similar Threads

  1. pls give create the program for the following
    By brsshalini in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 24th, 2013, 09:17 AM
  2. pls help me with word occurances program
    By kashif in forum Collections and Generics
    Replies: 6
    Last Post: August 16th, 2011, 03:16 PM
  3. pls help me with word occurances program
    By kashif in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 16th, 2011, 03:16 PM
  4. pls help me with word occurances program
    By kashif in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2011, 09:39 AM
  5. can anyone help me pls to build this program
    By Xhejn in forum Member Introductions
    Replies: 1
    Last Post: May 31st, 2011, 05:05 PM