我有一个关于如何将String或int或double值转换为Object Array
的问题假设我有两节课。一个是学生班,一个是数据库班
在Student课程中,我有以下内容:
public class Student {
String name;
int grade;
double average;
}
现在在Database类中,我正在使用代码创建一些新学生:
Student studentOne = new Student();
System.out.print("Enter student 1's name: ");
studentOne.name = in.next();
Student [] students = new Student [5];
现在,如果我创建:
students[0] = studentOne.name; // It gives me the following error: "Type mismatch: cannot convert from String to Student"
现在我明白我无法将字符串转换为对象,但在谷歌或此处搜索之后,我找不到如何完成此操作的方法。你能“投”到一个物体里吗?或.toObject方法?我很困惑。
谢谢和最好的问候
答案 0 :(得分:1)
您的数组属于Student
类型且studentOne.name
属于String
类型,您无法为String
类型数组指定Student
类型值。只能在Student
数组中添加student
类型对象。
students[0] = studentOne.name;
无效。
将其更改为
students[0] = studentOne;
此外,您首先需要实例化每个Student
对象,然后才能对其执行任何操作。
在students[0] = new Student();
students[0] = studentOne;
答案 1 :(得分:1)
您可以采取以下措施让不同数量的学生添加
public class Main {
public static class Student {
String name;
int grade;
double average;
public Student()
{
name = "Nothing";
grade = -1;
average = 0.0;
}
public void PrintStudent()
{
System.out.println("[" + name + ", " + grade + ", " + average + "]");
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Student [] students = null;
System.out.print("Enter How Many Students to Create: ");
String numberOfStudents = in.nextLine();
if(numberOfStudents.matches("\\d+"))
{
students = new Student [Integer.parseInt(numberOfStudents)];
}
else
{
System.err.println("Error when reading user input, exiting...");
return;
}
CreateStudents(students, in);
PrintStudents(students);
}//main method
public static void CreateStudents(Student[] allStudents, Scanner inputOrigin)
{
System.out.println("Enter Students as CSV(Comma Seperated Values).\nExample Given: Pete The Dragon, 11, 3.67");
for(int studentIndex = 0; studentIndex < allStudents.length; studentIndex++)
{
System.out.println("Enter information for student #" + studentIndex + ": ");
String[] input = inputOrigin.nextLine().split(",");
try{
//note how each index is a value of the student object in the input string array
Student newStudent = new Student();
newStudent.name = input[0];
newStudent.grade = Integer.parseInt(input[1].replaceAll(" ", ""));
newStudent.average = Double.parseDouble(input[2].replaceAll(" ", ""));
allStudents[studentIndex] = newStudent;
}
catch(Exception ex)
{
System.err.println("Failed to create the " + studentIndex + " due to the following:" + ex.toString());
allStudents[studentIndex] = new Student();
//or we can terminate
//return;
}
}
}
public static void PrintStudents(Student[] allStudents)
{
System.out.println("The students are as follows:");
for(int index = 0; index < allStudents.length; index++)
{
System.out.print(index + ": ");
if(allStudents[index] != null)
allStudents[index].PrintStudent();
}
}
}
以下是我们添加5名学生的程序的单次运行的以下输出。需要注意的是,当我在尝试创建学生时遇到异常时,我将添加一个默认的Student对象。如果你想,你可以停止添加学生,只需退出。然而,如果用户搞砸了,能够继续创建学生真是太好了。
输出
Enter How Many Students to Create: 5
Enter Students as CSV(Comma Seperated Values).
Example Given: Pete The Dragon, 11, 3.67
Enter information for student #0:
ryan, 4, 3.33
Enter information for student #1:
harambe the great, 12, 4.32
Enter information for student #2:
some other person, 6, 2.45
Enter information for student #3:
donald trump, 1, 0.35
Enter information for student #4:
another person, incorrect vlaue example, 4.67
The students are as follows:Failed to create the 4 due to the following:java.lang.NumberFormatException: For input string: "incorrectvlaueexample"
0: [ryan, 4, 3.33]
1: [harambe the great, 12, 4.32]
2: [some other person, 6, 2.45]
3: [donald trump, 1, 0.35]
4: [Nothing, -1, 0.0]
答案 2 :(得分:0)
您有一组Student
个对象。因此,您只能将Student
实例分配给其中一个阵列插槽。 Student.name
是String
。对我来说,这似乎是合乎逻辑的,你的意图是将整个Student
对象分配给你的数组,而不仅仅是名字。只需更改:
students[0] = studentOne.name;
到
students[0] = studentOne;
这将纠正您的类型不匹配错误。