when try put in () brackets of friends f = new friends(friendsname, friendsage); comes error:
constructor friends in class friends cannot applied given types. required: no arguments. found: string, int. reason: actual or formal argument lists differ in length.
but when take out arguments friends list displays "null 0". values not set though have string friendsname = input.next();?
also, when try remove friend, doesn't anything. in source code bring warning,
suspicious call util.java.collection.remove: given object cannot contain given instances of string (expected friends).
i'm confused on means?
import java.util.arraylist; import java.util.scanner; public class friends { public static void main( string[] args ) { int menu; int choice; choice = 0; scanner input = new scanner(system.in); arraylist< friends > friendslist = new arraylist< >(); system.out.println(" 1. add friend "); system.out.println(" 2. remove friend "); system.out.println(" 3. display friends "); system.out.println(" 4. exit "); menu = input.nextint(); while(menu != 4) { switch(menu) { case 1: while(choice != 2) { system.out.println("enter friend's name: "); string friendsname = input.next(); system.out.println("enter friend's age: "); int friendsage = input.nextint(); friends f = new friends(friendsname, friendsage); friendslist.add(f); system.out.println("enter another? 1: yes, 2: no"); choice = input.nextint(); } break; case 2: system.out.println("enter friend's name remove: "); friendslist.remove(input.next()); break; case 3: for(int = 0; < friendslist.size(); i++) { system.out.println(friendslist.get(i).name + " " + friendslist.get(i).age); } break; } system.out.println(" 1. add friend "); system.out.println(" 2. remove friend "); system.out.println(" 3. display friends "); system.out.println(" 4. exit "); menu = input.nextint(); } system.out.println("thank , goodbye!"); } public string name; public int age; public void setname( string friendsname ) { name = friendsname; } public void setage( int friendsage ) { age = friendsage; } public string getname() { return name; } public int getage() { return age; } }
you try instantiate object of friends class this:
friends f = new friends(friendsname, friendsage); the class not have constructor takes parameters. should either add constructor, or create object using constructor exist , use set-methods. example, instead of above:
friends f = new friends(); f.setname(friendsname); f.setage(friendsage);
Comments
Post a Comment