我收到一个错误Java.lang字符串无法转换为学生?
我有一个方法文件名student,其中有一组方法。我需要这些方法来完成实验13。我还有一个学生对象的数组列表。在我的13号实验代码中,我遇到了一个错误,我无法将字符串转换为Student 我已经尝试了我能做的一切例如我试图通过把学生放在代码前面来转换它例如student.jessica.addfriend(“emily”)。但它仍然不起作用。请帮我解决我的问题 这是13号实验室的代码我收到一个错误Java.lang字符串无法转换为学生?,java,string,arraylist,Java,String,Arraylist,我有一个方法文件名student,其中有一组方法。我需要这些方法来完成实验13。我还有一个学生对象的数组列表。在我的13号实验代码中,我遇到了一个错误,我无法将字符串转换为Student 我已经尝试了我能做的一切例如我试图通过把学生放在代码前面来转换它例如student.jessica.addfriend(“emily”)。但它仍然不起作用。请帮我解决我的问题 这是13号实验室的代码 import java.util.ArrayList; public class Lab13 {
import java.util.ArrayList;
public class Lab13
{
public static void main(String[] args)
{
Student jessica = new Student("Jessica", 23,
Student.SocialYear.SENIOR);
Student henry = new Student("Henry", 16, Student.SocialYear.FRESHMAN);
String n = jessica.getName();
System.out.println("One student is " + n + ".");
System.out.println("This student is " + jessica.getAge() + " years old.");
System.out.print(henry.getName() + " used to be " + henry.getAge() + " years old.");
henry.getAge();
System.out.println(" He just had a birthday and is now " + henry.getAge() + " years old.");
System.out.println(jessica.getName() + " is a " + jessica.getSocialYear() + ".");
printVote(jessica);
printVote(henry);
// HELP! Modify me to use the updated Student class.
//What type input parameter should be passed into the method?
jessica.addFriend("Erin");
jessica.addFriend("Bert");
henry.addFriend("Isaac");
henry.addFriend("Kaley");
// HELP! Modify me to use the updated Student class.
// What type of objects does the ArrayList of friends have?
printFriend(jessica, "Bert");
printFriend(henry, "Sam");
// HELP! Create more instances of the Student class for testing.
// HELP! Add friends to the ArrayLists of the instances.
// HELP! Print out the favorite friend of one of the students.
// HELP! Change the favorite friend of one of the students.
// The new favorite should already be a friend of the student.
// Print out this new favorite friend.
// HELP! Print out all the friends of one of the students.
// HELP! Check to see if a student is friends with another student.
// This test should be true (the student should have the friend).
// HELP! Check to see if a student is friends with another student.
// This test should be false (the student should NOT have the friend).
// HELP! A friend transfers and a student loses track.
// The student should unfriend the friend.
// Print out the updated list of friends.
}
/**
* This method prints whether a student can vote based on age.
*
* @param s A Student object
*/
public static void printVote(Student s)
{
if (s.canVote()) {
System.out.println(s.getName() + " can vote.");
} else {
System.out.println(s.getName() + " is not old enough to vote.");
}
}
/**
* This method prints whether a student knows the friend.
*
* @param s A Student object
* @param f A String representing the friend
*/
public static void printFriend(Student s, String f)
{
if (s.hasFriend(f)) {
System.out.println(s.getName() + " is friends with " + f + ".");
} else {
System.out.println(s.getName() + " does not know " + f + ".");
}
}
}
这是我的学生方法
import java.util.ArrayList;
import java.util.Collections;
public class Student
{
public enum SocialYear { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR }
private String name;
private ArrayList<Student> friends;
private int age;
private SocialYear year;
/**
* Constructor for objects of class Student
*/
public Student(String n, int a, SocialYear sy)
{
this.name = n;
this.age = a;
this.year = sy;
this.friends = new ArrayList<Student>();
}
public String getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
public void setAge()
{
this.age++;
}
public SocialYear getSocialYear()
{
return this.year;
}
public boolean canVote()
{
return this.age >= 18;
}
public void addFriend(Student friend)
{
friends.add(friend);
}
public boolean hasFriend(String friend)
{
return friends.contains(friend);
}
public void unFriend(Student friend)
{
friends.remove(friend);
}
public void getFriends()
{
for(Student s : friends)
{
System.out.println(s + ",");
}
}
public Student getFavFriendName()
{
return friends.get(0);
}
public void myNewFavFriend(Student friend)
{
friends.remove(friend);
friends.add(0, friend);
}
}
import java.util.ArrayList;
导入java.util.Collections;
公立班学生
{
公共枚举社会年{大一、大二、大三、大四}
私有字符串名称;
私人ArrayList朋友;
私人互联网;
私人社会年;
/**
*类Student对象的构造函数
*/
公立学生(字符串n、整数a、社会年sy)
{
this.name=n;
这个年龄=a;
今年=sy;
this.friends=new ArrayList();
}
公共字符串getName()
{
返回此.name;
}
公共整数getAge()
{
返回这个年龄;
}
公共无效设置()
{
这个.age++;
}
公共社会年getSocialYear()
{
今年返回;
}
公共布尔投票()
{
返回此值。年龄>=18;
}
公众朋友(学生朋友)
{
朋友。添加(朋友);
}
公共布尔hasFriend(字符串友元)
{
返回好友。包含(好友);
}
公共无效取消朋友(学生朋友)
{
朋友。删除(朋友);
}
公开作废getFriends()
{
(学生:朋友)
{
System.out.println(s+“,”);
}
}
公立学生getFavFriendName()
{
返回好友。获取(0);
}
公开作废我的新朋友(学生朋友)
{
朋友。删除(朋友);
添加(0,朋友);
}
}
方法addFriend
将学生
作为参数
public void addFriend(Student friend)
您的代码提供了一个字符串
jessica.addFriend("Erin");
这会管用的
jessica.addFriend(henry);
因为您已经创建了一个名为henry的学生
Student henry = new Student("Henry", 16, Student.SocialYear.FRESHMAN);
那么我该怎么做呢?我该怎么把它转换成学生呢?你是什么意思