gpt4 book ai didi

java - 将字符串/整数转换为对象数组

转载 作者:行者123 更新时间:2023-12-02 03:07:27 26 4
gpt4 key购买 nike

我有一个关于如何将 String 或 int 或 double 值转换为对象数组的问题

假设我有两个类。一种是学生类,一种是数据库类

在学生类(class)中,我有以下内容:

public class Student {
String name;
int grade;
double average;

}

现在在数据库类中,我正在使用代码创建一些新学生:

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方法?我很困惑。

谢谢并致以诚挚的问候

最佳答案

您可以执行以下操作来添加可变数量的学生

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 名学生。需要注意的是,当我在尝试创建学生时遇到异常时,我将添加一个默认的学生对象。如果您愿意,您可以停止添加学生并直接退出。不过,如果用户搞砸了,能够继续创建学生是件好事。

输出

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]

关于java - 将字符串/整数转换为对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41558632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com