Results 1 to 13 of 13
  1. #1
    iceyferrara is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    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.



    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.
    Last edited by iceyferrara; 09-23-2011 at 10:09 AM.

  2. #2
    iceyferrara is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

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

    Java Code:
    //Mike Ferrara
    //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]);
    		}
    	}
    }

    Java Code:
    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;
        }
    }

  3. #3
    iceyferrara is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

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

    sorry about that i forgot to put it into code mode.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

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

    dummy error: you make a call to a method with an argument but the method in the class has no
    argument:

    the call is: getName(keyboard.nextLine())
    and the method signature is: getName()

    so, to fix this error, the method call must match the signature. either you correct the method call or the signature.

  5. #5
    iceyferrara is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

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

    how would i go about that? i try to make them match, and it gives me the same error, i even tried renaming them but nothing. Am i missing any lines anywhere?

  6. #6
    iceyferrara is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

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

    Still trying everything but nothing, do i need to put something after the method call in the ()?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by iceyferrara View Post
    Still trying everything but nothing, do i need to put something after the method call in the ()?
    Nope, because the definition of those methods is 'public String getName()' and 'public String getNumber()' so those methods don't take parameters and that's exactly what you're trying to do: call those methods with a parameter and the compiler complains about it. The remedy is simple: leave out the parameters and the compiler will be happy.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    iceyferrara is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

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

    okay so by leaving out the parameters u mean take away the ()? i thought those were needed, i need the user to be able to input into that method so it can save it and return it. I understand that u said leave out the parameters but i thought they were already left out since i left the () blank. Im not quite sure what parameters your talking about. Sorry, im really not that bad with this stuff but this has been puzzling me and even my professor couldn't help(or didn't want to) idk.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by iceyferrara View Post
    okay so by leaving out the parameters u mean take away the ()? i thought those were needed, i need the user to be able to input into that method so it can save it and return it. I understand that u said leave out the parameters but i thought they were already left out since i left the () blank. Im not quite sure what parameters your talking about. Sorry, im really not that bad with this stuff but this has been puzzling me and even my professor couldn't help(or didn't want to) idk.
    If a method doesn't take parameters (as your methods do), you call them like this:

    Java Code:
    getName(); // look, no parameters here
    getNumber(); // also no parameters here
    But I doubt if you want to call those methods instead of their counterparts setName( ... ) and setNumber( ... ). This is just basic programming stuff and you have to know what you want your program to be doing.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    iceyferrara is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

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

    thanks for all the help im trying to figure it out ill give an update in a little.

  11. #11
    iceyferrara is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

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

    i have getName(keyboard.nextInt) because i want the user to input the name, but keyboard.nextInt is a parameter and i need it to be able to get the name from the user.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by iceyferrara View Post
    i have getName(keyboard.nextInt) because i want the user to input the name, but keyboard.nextInt is a parameter and i need it to be able to get the name from the user.
    You're getting an int from the keyboard and you want to set (not get) it as the name of your object? Something's rotten in the state of Denmark here ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    iceyferrara is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

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

    haha, wow i got it man, thanks for all your help. idk what i was thinking, my professor taught me different so i was very confused, thanks for all your help. getName needed to be setName.

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 07:51 AM
  2. operator - cannot be applied to java.lang.String,int
    By cc11rocks in forum AWT / Swing
    Replies: 0
    Last Post: 02-11-2011, 03:33 AM
  3. Replies: 4
    Last Post: 01-29-2010, 09:57 PM
  4. Replies: 4
    Last Post: 11-27-2008, 06:09 AM
  5. Error: cannot be applied to (java.lang.String)
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •