Based on the requirements on the page below, we have created a membership registration function in Java.
When I finished writing the code and moved it, an error occurred and the data was not output well.
java.lang.IndexOutOfBoundsException—Index 333 out of bounds for length1
I don't think there is any problem with the code, but I don't know how to fix it.
I apologize for the inconvenience, but please let me know.
2/24 Additional
After modifying the Register.java code,
This data was displayed.
Register.java
package lenyu;
import java.util.Date;
import java.util.*;
public class Register {
static ArrayList<Kaiin>kaiinAll = new ArrayList<Kaiin>();
public static void main(String[]args) {
Register = new Register();
Scanner scan = new Scanner (System.in);
System.out.println("How many people would you like to register?");
inti=scan.nextInt();
int id = 0;
String name = null;
for(int j=0;j<i;j++){
System.out.println("Please enter your membership ID"");
id=scan.nextInt();
System.out.println("Please enter a name"");
name = scan.next();
kaiinAll.add(r.kaiinAdd(id,name)));
Kaiin lastElement=kaiinAll.get(kaiinAll.size()-1);
System.out.println(lastElement);
}
}
private Kaiin kaiinAdd(intid, String name) {
Kaiink = new Kaiin();
k.setId(id);
k.setName(name);
Date = new Date();
k.setAddDate(d);
returnk;
}
}
Kaiin.java
package lenyu;
import java.util.Date;
public class Kaiin{
intid;
String name;
Date addDate;
public int getId(){
return id;
}
public void setId(intid){
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name){
this.name = name;
}
publicDategetAddDate(){
return addDate;
}
public void setAddDate(DateAddDate){
This.addDate=addDate;
}
}
This is because kaiinAll.get(id)
uses id as the key to retrieve elements from a variable-length array.This is not a HashMap or associative array, so it is an array in the order of insertion.(For example, the index of the item you want to retrieve when the screenshot exception is 0)
For example, if you want to retrieve an element with a specific ID from ArrayList<Kaiin>
, you can use the for-each loop.
ArrayList#add()
is the method to add the specified element to the end of the list
Therefore, to retrieve the previously added element, you can simply pass the ArrayList#get()
argument with the index indicating the last element in the list.
Since the index is 0 beginning, the index that indicates the last element of the list is -1 in the To sum up, You can retrieve the member objects that you just registered in . Implement Questionnaire The argument is a value that is independent of the index. (Added after receiving comments) I fixed it, but it didn't show well. Passing an object to the is used. The solution is to override (However, it does not seem that In addition, the issues to be linked to are Please summarize the output at the end of the program. Therefore, in the end, you will not think about the index of the previously registered element... and will output all the elements on the list in a loop at once.Kaiin lastElement=kaiinAll.get(kaiinAll.size()-1);
kaiinAll.get(id)
System.out.println()
argument prints a string generated by the toString()
method. The Kaiin
class does not implement the toString()
method.getClass().getName()+'@'+Integer.toHexString(hashCode())
This is probably not what you expect.toString()
in the Kaiin
class for proper implementation.public class Kaiin{
// ...
@ Override
public String to String() {
// Assemble the string to output here
return "TODO";
}
}
toString()
should be used for this challenge.)
© 2024 OneMinuteCode. All rights reserved.