我正在做一个从 ArrayList 存储、更新、搜索和删除数据的程序。我的问题是我无法从 ArrayList 中检索数据。我是Java新手,所以请帮助我。
我想显示我在下面提到的事物列表。
public Subject(int id, String name, int marks){
subId = id;
subName = name;
subMarks = marks;
}
public void show(){
System.out.println("1.Insert" + "\n" + "2.Search" + "\n" + "3.Update" + "\n" + "4.Delete" + "\n" + "5.Back");
try{
select = Integer.parseInt(br.readLine());
}
catch(Exception e){
System.out.println(e);
}
}
谢谢大家。
我的实际程序是这样的......所以请给我建议我应该怎么做才能将数据存储在 ArrayList..
package com.sms.data;
import com.sms.util.*;
import java.util.regex.*;
import java.io.*;
import java.util.*;
public class Subject extends Operation
{
int subId, subMarks;
String subName;
ArrayList <Subject> list = new ArrayList <Subject> ();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
DuplicateSubjectFoundException duplicate = new DuplicateSubjectFoundException();
NoSubjectFoundException noName = new NoSubjectFoundException();
InvalidDataException invalid = new InvalidDataException();
InvalidName inName = new InvalidName();
int select, exit = 0;
public Matcher matcher;
Subject sub;
public Subject(){
}
public Subject(int id, String name, int marks){
subId = id;
subName = name;
subMarks = marks;
}
public void display(){
System.out.println("SubjectId" + "\t" + "Name" + "\t" + "Marks");
System.out.println(subId + "\t" + subName + "\t" + subMarks);
}
public void show(){
System.out.println("1.Insert" + "\n" + "2.Search" + "\n" + "3.Update" + "\n" + "4.Delete" + "\n" + "5.Back");
try{
select = Integer.parseInt(br.readLine());
}
catch(Exception e){
System.out.println(e);
}
}
public void displayList(ArrayList <Subject> store)
{
int id = 0, subMarks = 0;
String name = " ";
while(exit != 3)
{
try
{
this.show();
switch(select)
{
case 1:
try{
System.out.println("Enter Subject ID: ");
subId = Integer.parseInt(br.readLine());
for(int j=0; j < list.size();j++){
System.out.println(list.get(j));
}
System.out.print("Subject Marks: ");
String marks = br.readLine();
if(marks.equals(" ")){
throw invalid;
}
else{
subMarks = Integer.parseInt(marks);
for(int j=0; j < list.size();j++){
System.out.println(list.get(j));
}
if(subMarks > 100)
throw invalid;
}
System.out.println("Enter subject Name: ");
subName = br.readLine();
Matcher matcher = Pattern.compile("[^a-zA-Z0-9]").matcher(subName);
while(matcher.find())
{
for(int j=0; j < list.size();j++){
System.out.println(list.get(j));
}
throw inName;
}
}
catch(NumberFormatException e){
System.out.println("Invald ID.");
continue;
}
catch(InvalidDataException e){
System.out.println(invalid.print());
continue;
}
catch(Exception e){
System.out.println(inName.print());
continue;
}
add(subId, subName, subMarks, list);
break;
case 2: //for search
case 3: //for update
case 4: //for delete
case 5:
exit = 3;
break;
default:
System.out.println("Enter your Choice.");
break;
}
}
catch(Exception e){
System.out.println(e);
}
}
}
public void add(int subId, String subName, int subMarks, ArrayList<Subject> list){
Subject sub = new Subject();
int i =0;
while(i < list.size()){
list.add(i, sub);
}
}
public void displayArrayList(ArrayList<Subject> list){ //for update
for(int i = 0;i < list.size(); i++){
Subject sub = list.get(i);
sub.display();
}
}
}