I'm working on a manage friend list program which allow you to add friends, remove friend from list, and display friend list. I have not finish this program yet. But I keep getting compile error at line 42 which I'm trying to create a method allow user to enter name and age of their friends and pass the value to the arraylist. I can't figure out where I did wrong. Thank you so much

Here is the code.

public class Friends
{
	private String name;
	private int age;
	
	public Friends(String friendName, int friendAge)
	{
		name = friendName;
		age = friendAge;	
	}
	
	public void setName(String friendName)
	{
		name = friendName;
	}
	
	public void setAge(int friendAge)
	{
		age = friendAge;
	}
	
	public String getName()
	{
		return name;
	}
	
	public int getAge()
	{
		return age;
	}
	

}
//Friend List
//Chia-Chao Liu
// 4/12/2001
//The purpose of this class is
//to add friends and remove friends,
//and display all friends.
import java.util.ArrayList;
import java.util.Scanner;

public class FriendsList
{
	ArrayList <Friends> fName = new ArrayList <Friends> ();
	ArrayList <Friends> fAge = new ArrayList <Friends> ();
	Scanner input = new Scanner(System.in);
	
	public FriendsList()
	{
		int choose;
		
		System.out.println("1. Add a friend");
		System.out.println("2. Remove a friend");
		System.out.println("3. Display Friend List");
		System.out.println("4. Exist");
		
		System.out.println("Please Enter the choose option: ");
		choose = input.nextInt();
		
		switch (choose)
		{
			case '1' :
				break;
			case '2' :
				break;
			case '3' :
				break;
			case '4' :
				break;
			default:
				
		}
		
		public void addfriends()
		{
			String name;
			int age;
			
			System.out.println("Please Enter the Name: ");
			name = input.next();
			add.fName(name);
			System.out.println("Please Enter the age: ");
			age = input.nextInt();
		}

}

Do me a favor and post the compiler output. Easier to diagnose the problem if I know what I'm looking for.

This is the compiler output and it also highlight line42:
FriendsList.java:43: illegal start of expression
public void addFriends()
^
FriendsList.java:43: illegal start of expression
public void addFriends()
^
FriendsList.java:43: ';' expected
public void addFriends()
^
FriendsList.java:56: reached end of file while parsing
}

Of course. You're trying to declare a method inside a method. I saw that earlier, but it didn't register...

"illegal start of expression" basically means: you can't do that here, you're not done with the last thing yet. you can't start declaring "addFriends" while you're still defining the constructor for the class. Amd that's why you get the "reached end of file" error - it gets to the end of the file, and you still owe it a close curly brace.

Finish up with the constructor, and you'll be fine.

I see. But after I fixed it, I still get the compiler errors at line 45 and line 48. I kind of add and change things a little bit.

here is the fixed cod:

import java.util.ArrayList;
import java.util.Scanner;

public class FriendsList
{
	Scanner input = new Scanner(System.in);
	ArrayList <Friends> fName = new ArrayList <Friends> ();
	ArrayList <Friends> fAge = new ArrayList <Friends> ();
	int optionChoose;
	String friendName;
	int friendAge;
	
	public FriendsList()
	{
		do
		{	
			System.out.println("1. Add a friend");
			System.out.println("2. Remove a friend");
			System.out.println("3. Display Friend List");
			System.out.println("4. Exist");
		
			System.out.println("Please Enter the choose option: ");
			optionChoose = input.nextInt();
		
			switch (optionChoose)
			{
				case '1' :
					break;
				case '2' :
					break;
				case '3' :
					break;
				case '4' :
					break;
				default:	
			}
		}while(optionChoose < 4 );

	}

	public void addFriends()
	{
			System.out.println("Please Enter the Name: ");
			friendName = input.next();
			fName.add(friendName);
			System.out.println("Please Enter the age: ");
			friendAge = input.nextInt();
			fAge.add(friendAge);
	}

		
}

The compiler shows:
FriendsList.java:52: cannot find symbol
symbol : method add(java.lang.String)
location: class java.util.ArrayList<Friends>
fName.add(friendName);
^
FriendsList.java:55: cannot find symbol
symbol : method add(int)
location: class java.util.ArrayList<Friends>
fAge.add(friendAge);
^
2 errors

I double check with my program. I did create Arraylist named fAge and fName.
how come it shows it cannot find symbol.

"Cannot find symbol" means there's an identifier that you're using that the compiier can't find a reference for. The symbols in this case are methods - it can find fName and fAge okay, and it sees that they're both declared as ArrayList<Friends>. You're trying to add friendName and friendAge, which are a String and an int. Since neither of those can be seen as an instanceof Friends, the add fails.

I got all of this except for the name of the variables you're adding from the compiler output - you should learn to read what the compiler tells you. Here's a link to a list of compiler errors - it's not perfect, but it'll give you something to refer to.


Is your intention here to create a new Friend and add that Friend to a list? If so, you want to use the new operator. Something like

Friend newFriend = new Friend(fName, fAge);
friends.add(newFriend);

which can be collapsed to

friends.add(new Friend (fName, fAge);

but make sure you understand the first before you try to see how the shorter form works.

Um... What I'm trying to do here is create a program allow user to add friends, removed a friend from the list, and display the friend list.
First I create a class named friends which contain name and age.

public class Friends
{
	private String name;
	private int age;
	
	public Friends(String friendName, int friendAge)
	{
		name = friendName;
		age = friendAge;	
	}
	
	public void setName(String friendName)
	{
		name = friendName;
	}
	
	public void setAge(int friendAge)
	{
		age = friendAge;
	}
	
	public String getName()
	{
		return name;
	}
	
	public int getAge()
	{
		return age;
	}
}

Then I create another class named FriendsList which contain the methods that allow the users to choose a option from menu, add friends, remove friends, and display friends. So I create an Arraylist that store named and ages. Usually we create ArrayList like

ArrayList <String>

but I'm using an Friends class which is an object. So I do

ArrayList <Friends>

but I'm not sure whether one array list can store both age and name in a same list or do I need to create another list. That is why I do ArrayList <Friends> fName, and ArrayList <Friends> fAge.
Then when I'm trying to create a method that named addFriends to allow the user to add friends. So I do

public void addFriends()
	{
			System.out.println("Please Enter the Name: ");
			friendName = input.next();
			fName.add(friendName);
			System.out.println("Please Enter the age: ");
			friendAge = input.nextInt();
			fAge.add(friendAge);
	}

so i can capture whatever the user enter and store in to the list. Sorry I wasn't being very detail when I'm asking questions. So do I just use the code you show me in preview post to add friends and age to arrayList? Thank you so much for so patient with me.

A Friend has a name and an age, right? So if you have a Friend and you want to know its name or age, you just ask it:

Friend f = new Friend ("Bob", 47);
System.out.println(f.getName()); // prints "Bob"

Your arraylist of Friends has to contain Friends - not names or ages. Like so:

ArrayList<Friend> friendsList= new ArrayList<Friend>();
friendsList.add(f); // Bob's now on the list. Welcome, Bob.

You want to find Bob? Well, you could iterate the list: look at each item and give back the ones whose name.equals("Bob"). There are better ways, but that's the simplest.


Now why do you have a setAge method? Do your friends change their age from time to time? If you want to keep the list up to date, get their birthdate and calculate their age when you're asked for it:

public int getAge()
{
  return thisYear-birthYear; // this is wrong, it assumes everyone is born on Jan. 1
                             // feel free to fix it if you want. 
}

Yes, it works. Thank you so much. But I run into another problem. When I create the display method and remove method which allow the user to remove a friend out of list and allow the user to see their friends list, I don't think I do right code for display method.

Here is the fixed Code:

import java.util.ArrayList;
import java.util.Scanner;

public class FriendsList1
{
	Scanner input = new Scanner(System.in);
	ArrayList <Friends1> list = new ArrayList <Friends1> ();
	String friendName;
	int friendAge;
	int index;
			
	public FriendsList1()
	{

 	}
	
	public void addFriends()
	{
		
		System.out.println("Please Enter the Name: ");
		friendName = input.next();
		System.out.println("Please Enter the age: ");
		friendAge = input.nextInt();
		Friends1 newFriend = new Friends1(friendName, friendAge);
		list.add(newFriend);
		
	}
	
	public void removeFriends()
	{
		System.out.println("Please enter the index number: ");
		index = input.nextInt();
		list.remove(index);
	}
	
	public void display()
	{
		
		System.out.printf("%s%8s%8s\n", "Index", "Name", "Age");
		
		for (int i; i<list.size; i++)
		{
			System.out.printf("%5d%8d\n", i, list.get(i));
		}
	}

		
}

I notice the list.size is private class which I got the compiler errors when I compile it, and beside it does not give me what I try to make the program do. What I'm trying to do here is when the user choose display option, the program would display the friend list like this:

Index    Name    Age
    0    Bob      12
    1    john     34

which depend on what the user enter into the friend list. Another question I have while I'm trying to make the program work is at this line

System.out.printf("%5d%8d\n", i, list.get(i));

Do I just use "list.get(i)" and the program will print out name and age store in arraylist or it won't give me what I want?

Once again thank you so much for so patient with me. I tried to do every thing I can before I post question. My teacher doesn't teach us how to use arraylist, and the book just briefly say what can arraylist do, but it doesn't tell you how to do it. Thank you so much.

size is a private field of ArrayList, but size() is a method which returns that value, so you can use that. You're probably confused because arrays expose that information as a public field, length - that was a mistake, but now they're stuck with it.


List.get() will return whatever is stored in the ArrayList. In this case, it's a Friend object. When you try to print an object, java will try to find a printable value for it, which in almost every case means it looks for a toString method. It always finds one. Either it finds one that belongs to that object's class, or it goes up to that object's superclass, and so forth. Eventually it comes to Object, because everything is an Object in java, and Object has a toString, but not a useful one - it just prints the address in memory that the reference points to. That does you no good at all, but if you haven't written a toString method for Friend, that's what you'll see when you try to print it.
If you have written a toString, you'll get whatever value that returns.
So you could tell toString to return some formatted string that gives you the name and age associated with that object.

That's fine if you always want that to be the String associated with that object. Better, I think, to use the methods you already have.

If you want to print the name of a Friend, and you have a reference to it, you just call your getName() method.

Friend f = getFriendFromSomewhere(); // method returns a Friend, we don't 
                              //care how: f is now a reference to a Friend object

System.out.println(f.getName());  // prints the value of f's name field to the console

Since list.get(i) returns a Friend, it counts as a reference to a Friend, and you can probably take it from there.


I tried to do every thing I can before I post question. My teacher doesn't teach us how to use arraylist, and the book just briefly say what can arraylist do, but it doesn't tell you how to do it. Thank you so much.

No worries. Everybody learns this stuff the hard way, one way or another - if you don't get it the hard way, you don't get it at all. Now go look for someone to teach this stuff to, and make them work for it as well.

This is what I have right now. I finally get rid of all the compiler errors. But I running to a logic error when I'm trying to make the program display the list.

here is the code

public class Friends1
{
	private String name;
	private int age;
	
	public Friends1(String friendName, int friendAge)
	{
		name = friendName;
		age = friendAge;	
	}
	
	public void setName(String friendName)
	{
		name = friendName;
	}
	
	public void setAge(int friendAge)
	{
		age = friendAge;
	}
	
	public String getName()
	{
		return name;
	}
	
	public int getAge()
	{
		return age;
	}
	
	public String toString()
	{
		String s = String.format( name+"%8d", age );
		return s;
	}

}

This the class which contain all the methods I'm going to use in the class I declare main.

import java.util.ArrayList;
import java.util.Scanner;

public class FriendsList1
{
	Scanner input = new Scanner(System.in);
	ArrayList <Friends1> list = new ArrayList <Friends1> ();
	String friendName;
	int friendAge;
	int index;
			
	public FriendsList1()
	{

 	}
	
	public void addFriends()
	{
		
		System.out.println("Please Enter the Name: ");
		friendName = input.next();
		System.out.println("Please Enter the age: ");
		friendAge = input.nextInt();
		Friends1 newFriend = new Friends1(friendName, friendAge);
		list.add(newFriend);
		
	}
	
	public void removeFriends()
	{
		System.out.println("Please enter the index number: ");
		index = input.nextInt();
		list.remove(index);
	}
	
	public void display()
	{
		int g = list.size();
		
		System.out.printf("%s%8s%8s\n", "Index", "Name", "Age");
		
		for (int i=0; i < g; i++)
		{
			System.out.printf("%5d%8d\n", i, list.get(i) );
		}
	}

		
}

This is the class which I declare main method.

import java.util.Scanner;
public class FriendListTest1
{
	static Scanner ky = new Scanner(System.in);
	static int optionChoose;
	
	public static void main (String [] args)
	{
		int option = optionChoose;
		FriendsList1 myList = new FriendsList1();
		
		do
		{	
			System.out.println("1. Add a friend");
			System.out.println("2. Remove a friend");
			System.out.println("3. Display Friend List");
			System.out.println("4. Exist");
		
			System.out.println("Please Enter the choose option: ");
			optionChoose = ky.nextInt();
			
			switch (optionChoose)
			{
				case 1 :
					myList.addFriends();
					break;
				case 2 :
					myList.removeFriends();
					break;
				case 3 :
					myList.display();
					break;
				case 4 :
					break;
				default:
					System.out.println("Invaild Selection");
			}

		}while(optionChoose != 4);		
	
	}
}

The error occur at line 44 in the friendslist1 class. I can't figure it out why it is a logical error. When I type the name and age, those value get store in name and age, and pass the value to arraylist. Then I create a for loot which that i=0 so it loop from zero and set it less then the total size of the arraylist. Next I do "list.get(i)" in the println which allow me to access data store in arraylist. The "i" just let it loop start from zero to how many friends the user enter in to the arraylist. This is my thinking process. I can't figure why it is a logical error.

Thank you so much again. I thought I would get it right this time, but fail again.

And what is it that it's doing that it shouldn't be doing, or not doing that it should be doing?

Sorry, I forgot to post the errors. When I run the friendListTest1 and choose option 3, the compiler showing some message like this:
Exception in thread "main" java.util.IllegalFormatConversionException: d != Friends1
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3999)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2709)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2661)
at java.util.Formatter.format(Formatter.java:2433)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at FriendsList1.display(FriendsList1.java:51)
at FriendListTest1.main(FriendListTest1.java:31)

but only when I enter add name and age first, if I don't add friend and age to arraylist, it does not show any error message.

Ah, okay. "Illegal Format Conversion" means it's trying to convert one thing into another, and it can't do it. The error here is in your format string in the display method:

System.out.printf("%5d%8d\n", i, list.get(i) );

You're telling it to display two ints, but list.get(i) returns a Friend, which can't be interpreted as an int. If you change "%5d%8d\n" to "%5d%8s\n" , it'll try to interpret Friend as a String, which should work. If that doesn't do it, you'll have to explicitly call the toString method:

System.out.printf("%5d%8s\n", i, list.get(i).toString() );

Thank you so much. You are a life saver. It works. After I change %8d to %s, it run perfectly. I was thinking about why the compiler show that I'm trying to convert something to the other things. Now I know where it went wrong. Thank you so much.
By the way, I like the quotation you put at the end

Ever tried. Ever failed. No matter. Try again. Fail again. Fail better. -Samuel Beckett

It does fail better before I finally make it works. Thank you so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.