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.
Code :
//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]);
}
}
}
Code :
//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.
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.
Re: Java Error cannot be applied to (java.lang.String), phone book entry program.
Quote:
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
Code :
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:
Code :
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:
Quote:
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.
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?
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.
Quote:
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.
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.