23,402
社区成员




import java.util.Scanner;
public class StudentArray{
static int Scount = 0;
public static void main(String []args){
String [][] studentInfo;
studentInfo = new String[10][5];
displayMenu(studentInfo);
System.out.println();
}/* main */
public static void displayMenu(String [][] student)
{
boolean quit = false;
String [] menu = {"add","dis","find","rm","update","quit","read","write"};
do{
System.out.println("+--------------------------------------------+");
System.out.println("|please choose the function you need: |");
System.out.println("+--------------------------------------------+");
System.out.println("| [ add ]:add |");
System.out.println("| [ read ]:read studentInfo from file |");
System.out.println("| [ dis ]:display |");
System.out.println("| [ find ]:find |");
System.out.println("| [ rm ]:remove |");
System.out.println("| [ update ]:update |");
System.out.println("| [ write ]:write studentInfo to file |");
System.out.println("| [ quit ]:quit |");
System.out.println("+--------------------------------------------+");
Scanner scanner = new Scanner(System.in );
String select = scanner.next();
if(select.equals(menu[0])){
add(student);
display(student);
}/* if add */
if(select.equals(menu[6])){
try{
readFromFile(student);
}catch(Exception e){
e.printStackTrace();
}/* catch */
display(student);
}/* if read */
else if(select.equals(menu[1])){
display(student);
}/* if display */
else if(select.equals(menu[2])){
find(student);
/* display(student); */
}/* if find */
else if(select.equals(menu[3])){
remove(student);
display(student);
}/* if remove */
else if(select.equals(menu[4])){
update(student);
display(student);
}/* if update */
else if(select.equals(menu[7])){
try{
writeToFile(student);
}catch(Exception e){
e.printStackTrace();
}/* catch */
display(student);
}/* if write */
else if(select.equals(menu[5])){
quit = true;
}/* if quit */
else
System.out.println("Input Error! Please Try again!");
}while(!quit); /* do{ } while */
}/* displayMenu() */
public static boolean add(String [][] student){
System.out.println("Please Input StudentInfomation like this:.\n\tNumber\tName\tGender\tPhoneNumber\tAddress");
Scanner scanner = new Scanner(System.in);
scanner.reset();
String [] info = new String [5];
for(int i = 0; i< 5;i++){
info[i] = scanner.next();
}/* for */
student[Scount] = info;
++Scount;
System.out.println("Add finished!");
return true;
}/* add() */
public static boolean display(String [][] student){
if(Scount < 1) {
System.out.println(" No Student In Here!");
return false;
}/* no student in */
System.out.println("All the Student Information As Follows:");
System.out.println("\tNumber\tName \tGender\tPhoneNumber \tAddress");
System.out.print("\t");
for(int j = 0; j < Scount;j++){
for(int i = 0; i< 5; i++){
System.out.print(student[j][i]+"\t");
if(i==3)System.out.print(" \t");
}/* for */
System.out.println();
System.out.print("\t");
}/* outer for */
System.out.println();
return true;
}/* display() */
public static boolean find(String [][] student){
int index;
System.out.println("+-----------------------------------------------------+");
System.out.println("|Please choose the which look up approach You need : |");
System.out.println("+-----------------------------------------------------+");
System.out.println("|[ 0 ]:ByNumber |");
System.out.println("|[ 1 ]:ByName |");
System.out.println("|[ 2 ]:ByGender |");
System.out.println("|[ 3 ]:ByPhoneNumber | ");
System.out.println("|[ 4 ]:ByAddress |");
System.out.println("+-----------------------------------------------------+");
Scanner scanner = new Scanner(System.in);
scanner.reset();
index = scanner.nextInt();
switch(index){
case 0: findByTarget(student,index);
break;
case 1: findByTarget(student,index);
break;
case 2: findByTarget(student,index);
break;
case 3: findByTarget(student,index);
break;
case 4: findByTarget(student,index);
break;
default:
System.out.println("Input Error:Please Try again!");
}/* switch */
return true;
}/* find() */
public static boolean findByTarget(String [][] student,int index){
boolean find = false;
String [] menu ={"StudentNumber","StudentNAME","StudentSex","StudentPhoneNumber","StudentHostel"};
System.out.println("Please Input The "+menu[index] + " You want Look Out:");
Scanner scanner = new Scanner(System.in);
String target=scanner.next();
for(int i = 0; i < Scount; i++)
{
if(student[i][index].equals(target))
{
displayIndex(student,i);
find = true;
/* not break then could find another same person ,ie find by sex*/
}/* if */
}/* for */
if(find ==false) System.out.println("The Student You want to find Isn't Exist.");
return true;
}/* findByTarget */
/** print the student that have finded ,only one for each time! */
public static void displayIndex(String[][] student,int index){
System.out.println("+-----------------------------------------------------+");
System.out.println("|The Student You find Are As Follows: |");
System.out.println("+-----------------------------------------------------+");
System.out.println("|\tNumber\tName \tGender\tPhoneNumber \tAddress |");
System.out.print("\t");
for(int i = 0; i< 5; i++){
System.out.print(student[index][i]+"\t");
if(i==3)System.out.print(" \t");
}/* for */
System.out.println();
System.out.println("+-----------------------------------------------------+");
}/* displayIndex */
public static void remove(String [][] student){
System.out.println(" Please Input The StudentNumber of the student you want to remove:");
Scanner scanner = new Scanner(System.in);
String target = scanner.next();
int i;
int start; /* store the position the first object need to remove */
for( i=0; i < Scount;i++)
if(student[i][0].equals(target)){
break;
}/* for and if */
/* Object at the back .remove forward */
for( start = i ;start < Scount-1;start++){
student[start] =student[start+1];
}/* another for */
student[--Scount] = null;
/* assigned null to last Object */
/** since student[--Scount] = null,ie cannot directly add student to
student[--Scount] now!
then ,if add directly ,throws NullPointerException
So we should try to come up with anoter approach!
*/
}/* remove() */
public static void update(String [][] student){
System.out.println("choose the Number Of The Student Which You Want to update:");
Scanner scanner = new Scanner(System.in);
String target = scanner.next();
int index =0;
for( int i = 0; i < Scount; i++)
if(target.equals(student[i][0]))
{
index = i;
break; /* only find the first occured student */
}/* if for */
System.out.println("*--------------------------------------------*");
System.out.println("The Student You Want to find is:");
displayIndex(student,index);
System.out.println("please Input new Information for this student!");
for(int i = 0; i< 5;i++){
student[index][i] = scanner.next();
}/* for */
}/* update */
public static boolean readFromFile(String [][] student) throws Exception {
/* Create a File instance */
java.io.File file = new java.io.File("student.txt");
/* Create a Scanner for the file */
java.util.Scanner input = new java.util.Scanner(file);
/* Read data from file */
while(input.hasNext()) {
for(int i = 0; i< 5;i++){
student[Scount][i] = input.next();
}/* for */
++Scount;
}/* while */
/* Close the file */
input.close();
return true;
}/* readFromFile() */
public static boolean writeToFile(String [][] student) throws Exception {
java.io.File file = new java.io.File("student.txt");
if(file.exists()) {
/* System.out.println(" File already exists!");
System.exit(0); */
if(!file.delete()){
System.out.println(" File already exists ! and cannot be delete!");
System.out.println("Now Create a anther file named \"new_student.txt\" to store you data!");
file = new java.io.File("new_student.txt");
}/* inner if */
else file = new java.io.File("student.txt");
}/* if */
/** Create a file */
java.io.PrintWriter output = new java.io.PrintWriter(file);
/** Write formatted output to the file */
for(int i = 0; i < Scount; i++){
for(int j = 0; j < 5; j++){
output.printf("%s\t",student[i][j]);
}/* inner for */
output.printf("\n");
}/* for */
/** Close the file */
output.close();
return true;
}/* writeToFile */
}