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 6 of 6

Thread: Java Error cannot be applied to (java.lang.String), phone book entry program.

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Error cannot be applied to (java.lang.String), phone book entry program.

    So we have to have two classes so i set it up good and it was almost exactly like my friends who's worked but mine gives me two errors.
    Here are the two classes.
    //Lab 2: 8.13
     
    import java.util.*;
    public class PhoneBookList
    {
    	public static void main(String[] args)
    	{
    		Scanner keyboard = new Scanner(System.in);
     
    		String[] nameList = new String[5];
    		String[] numberList = new String[5];
     
    		PhoneBook entry = new PhoneBook();
     
    		System.out.println("Please enter a name then their corresponding " +
    							"number. (dashes not included): Press enter after each name and number ");
    		for(int i = 0; i < nameList.length; i++)
    		{
    			entry.getName(keyboard.nextLine());
    			nameList[i] = entry.getName();
    			entry.getNumber(keyboard.nextLine());
    			numberList[i] = entry.getNumber();
    		}
     
    		System.out.println("The Names and Numbers in this phone book are");
    		System.out.println("Names/t/t/tNumbers");
    		System.out.println("**************************");
    		for (int i = 0; i <nameList.length; i++)
    		{
    			System.out.println(nameList[i]+ "/t/t/t"+ numberList[i]);
    		}
    	}
    }

    //Lab 2: 8.13
     
     
    import java.util.*;
    public class PhoneBook
    {
        private String friend;
        private String numbers;
     
        //Create Array Lists
        ArrayList<String> nameList = new ArrayList<String>();
    	ArrayList<String> numberList = new ArrayList<String>();
     
    	//Set Name Field
        public void setName(String name)
        {
    		nameList.add(name);
            friend = name;
        }
     
    	//Get Name Field
        public String getName()
        {
            return friend;
        }
     
    	//Set Number for Name
        public void setNumber(String num)
        {
    		numberList.add(num);
            numbers = num;
        }
     
    	//Get Number for same Name
        public String getNumber()
        {
            return numbers;
        }
    }



    The Errors i recieve are:
    E:\Lab 2\PhoneBookList.java:20: getName() in PhoneBook cannot be applied to (java.lang.String)
    entry.getName(keyboard.nextLine());
    ^
    E:\Lab 2\PhoneBookList.java:22: getNumber() in PhoneBook cannot be applied to (java.lang.String)
    entry.getNumber(keyboard.nextLine());
    ^

    Any help? i put it in right but its saying it cant be applied to string which i dont know why.

    Any help would be greatly appreciated.


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Error cannot be applied to (java.lang.String), phone book entry program.

    please any help, i need this cuz its due by tomorrow and im only getting these two errors. as mentioned above.

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Java Error cannot be applied to (java.lang.String), phone book entry program.

    im only getting these two errors
    There's a lot more wrong with your code than that. The two errors are quite easy to fix though. When you use a method, you have to follow the method's 'signature' - how it is declared in its class. 'getters and setters' are a popular pattern in Java, with getters being declared like
    public <type> getSomething()
    When you use a 'get method', you typically don't pass any arguments (the brackets are empty like '()'), and it returns to you something that is stored inside an object. A setter (set method) is typically declared like this:
    public void setSomething(<type> newValueForSomething)
    When you use a 'set method' you provide some new data inside the brackets, and the setMethod assigns that new data to a reference inside the object so that the object 'remembers' what its new value is.

    You've got getters and setters, but your error messages are telling you that you're trying to supply new data to a get method when its signature:
    public String getName()
    says that it doesn't accept any new data. Your getName method looks right, but you're using it wrong. If you want to store new data into your PhoneBook objects (as you appear to want to do, whether that's in fact the right thing to do or not), you should be using the set method you've written.

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Error cannot be applied to (java.lang.String), phone book entry program.

    i get what your trying to say, but i want to be able to keep it in two seperate classes, and oh so your saying its not the getName(keyboard.nextInt) thats messing up but the fact that the setName cannot read it?

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Java Error cannot be applied to (java.lang.String), phone book entry program.

    When you're adding data to your phone book, you would use a method like 'setName(<data user just typed>)' to store the new data in the phone book entry. When you're retrieving a phone book entry or listing the book's contents, you would use a method like 'System.out.println(entry.getName())' to print the name of an *existing* entry.

    It's good to use separate classes, but it's important to model your application correctly. A PhoneBook class sounds good - you can have a static or singleton PhoneBook, or lots of separate PhoneBooks if that suits your application. A PhoneBook would ideally consist of one or more objects like 'PhoneBookEntry' or 'PhoneBookItem' (or for inner class goodness, PhoneBook.Entry or PhoneBook.Item), where each one of those would have a name, number etc.

    setName cannot read it
    A 'set method' wouldn't normally read anything, it would take some new data from somewhere and assign it to the internal reference of an object. If you want to 'read' the data held by an object, you would use a get method.

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Error cannot be applied to (java.lang.String), phone book entry program.

    ohh i got you, i changed the keyboard int entry one from getName to setName and it worked out perfectly. Thanks so much for your help.

Similar Threads

  1. Operator % cannot be applied to java.lang.String,int?
    By javarum in forum Java Theory & Questions
    Replies: 8
    Last Post: July 12th, 2011, 08:06 PM
  2. [SOLVED] Java run time error: 'java.lang.NullPointerException"
    By ChandanSharma in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 20th, 2011, 11:53 AM
  3. Replies: 1
    Last Post: January 15th, 2010, 01:32 AM
  4. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  5. [SOLVED] Facing java error "cannot be applied to (java.lang.String)"
    By tazjaime in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2009, 10:19 AM

Tags for this Thread