比较/链接3个字符串数组

时间:2014-05-21 01:45:11

标签: java eclipse treemap

TreeMap<String,String> map = new TreeMap<String,String>();用于两个字符串比较/链接值。但我有一个问题。我需要链接/比较三个字符串我该怎么办?

我的示例输入是:

input #1
id number: 5
name: jomar
course: computer science

input#2
id number: 3
name: karen
course: IT

当我按ID号对其进行排序并打印时,它应该是这样的:

ID num          name          course
3               karen          IT
5               jomar          computer science

这是我的代码,我希望有人可以改进我的代码:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.TreeMap;

public class studentreport3 {
    static Scanner j = new Scanner(System.in);
    static Scanner a = new Scanner(System.in);
     public static void main(String[] args)    {
         List<String> mapmap = new ArrayList<String>();
         List<String> studentnumberA = new ArrayList<String>();
         List<String> studentnameA = new ArrayList<String>();
         List<String> courseA = new ArrayList<String>();
         List<Double> prelimA = new ArrayList<Double>();
         List<Double> midtermA = new ArrayList<Double>();
         List<Double> finalsA = new ArrayList<Double>();


         double prelim,midterm,finals;
         String menu,menu2;
         String transaction=null,transaction2=null,transaction3=null; 
         String studentname=null,course=null,studentnumber = null;

         do{
         System.out.print("\t\t\tDi Natuto University \n\t\t\t    Pililla,Rizal \n\n\t\t\t      Main Menu \n\t\tSM\tStudent Maintenance \n\t\tSR\tStudent Report \n\t\tSGR\tStudent Grade Report \n\t\tX\tExit \n\n\t\tProcess? ");
         menu=j.nextLine(); 
         if(menu.equals("x")||menu.equals("X")){
             System.exit(0);
             }   
         if(menu.equals("SM")||menu.equals("sm")){
             do{
                 TreeMap<String,String[]> map = new TreeMap<String,String[]>();
                 System.out.print("Student Number: ");
                 studentnumber=j.nextLine();
                 studentnumberA.add(studentnumber);

                 System.out.print("Student Name: ");
                 studentname=a.nextLine();
                 studentnameA.add(studentname);

                 System.out.print("Course : ");
                 course=j.nextLine();
                 courseA.add(course);

                 map.put(studentnumber, new String[]{studentname, course});

                 System.out.print("Prelim Grade: ");
                 prelim=j.nextDouble();
                 prelimA.add(prelim);

                 System.out.print("Midterm Grade: ");
                 midterm=j.nextDouble();
                 midtermA.add(midterm);

                 System.out.print("Finals Grade: ");
                 finals=j.nextDouble();
                 finalsA.add(finals);
                 j.nextLine();

                 System.out.println("Continue? (Y/N): ");
                 transaction=a.nextLine();

             } while(transaction.equalsIgnoreCase("Y"));
         }

         if(menu.equalsIgnoreCase("sr")){
             do{
             System.out.println("\t\t\tDi Natuto University \n\t\t\t    Pililla,Rizal \n\n\t\t\tStudent Summary Report \n\n" +
                    "\t\t\tSNo\tStudent no. \n\t\t\tSna\tStudent Name \n\t\t\tCo\tCourse \n\t\t\tX\tExit \n\n\t\tProcess? ");    
             menu2=j.nextLine();
             System.out.println("\n");
             if(menu2.equalsIgnoreCase("X")){
                 System.exit(0);
             }
             if(menu2.equalsIgnoreCase("sno")){

                 String[] array = studentnumberA.toArray(new String[studentnumberA.size()]);
                 String[] array2 = studentnameA.toArray(new String[studentnameA.size()]);
                 String[] array3 = courseA.toArray(new String[courseA.size()]);
                 String [][] storeAllArray = {array,array2,array3};
                 TreeMap<String,String> map = new TreeMap<String,String>();
                 TreeMap<String,String> map2 = new TreeMap<String,String>();

                 System.out.println("\t\t\t\tDi Natuto University \n\t\t\t\t    Pililla,Rizal \n\n\t\t\t\tStudent Summary Report \n\n\t\t\tStudent no."+"   Student Name"+"\t\tCourse ");
                 for(int i=0;i<array.length;i++){
                     map2.put(array[i], array3[i]);
                     map.put(array[i], array2[i]);
                 }
                 for (String key : map.keySet())
                     System.out.println("\t\t\t"+key + "\t\t " + map.get(key));

                 for (String key2 : map2.keySet())

                     System.out.println("\t\t\t\t\t\t\t\t"+map2.get(key2));

                 System.out.println("\n");
             }
             if(menu2.equalsIgnoreCase("sna")){
                 String[] array = studentnumberA.toArray(new String[studentnumberA.size()]);
                 String[] array2 = studentnameA.toArray(new String[studentnameA.size()]);
                 String[] array3 = courseA.toArray(new String[courseA.size()]);
                 String [][][] storeAllArray = {{array},{array2},{array3}};
                 TreeMap<String,String> map = new TreeMap<String,String>();
                 TreeMap<String,String> map2 = new TreeMap<String,String>();

                 System.out.println("\t\t\t\tDi Natuto University \n\t\t\t\t    Pililla,Rizal \n\n\t\t\t\tStudent Summary Report \n\n\t\t\tStudent no."+"   Student Name"+"\tCourse ");
                 for(int i=0;i<array.length;i++){

                     String  [][] Array11= {{array[i]},{array2[i]},{array3[i]}};
                     String  [][] Array12= {{array[i]},{array2[i]}};
                     map2.put(array2[i], array3[i]);
                     map.put(array2[i], array[i]);

                 }
                     for (String key : map.keySet())
                         System.out.println("\t\t\t"+map.get(key)+ "\t\t " + key+"\t\t");
                     for (String key2 : map2.keySet())

                         System.out.println("\t\t\t\t\t\t\t"+map2.get(key2));

             }
             if(menu2.equalsIgnoreCase("co")){
                 String[] array = studentnumberA.toArray(new String[studentnumberA.size()]);
                 String[] array2 = studentnameA.toArray(new String[studentnameA.size()]);
                 String[] array3 = courseA.toArray(new String[courseA.size()]);
                 TreeMap<String,String> map = new TreeMap<String,String>();
                 TreeMap<String,String> map2 = new TreeMap<String,String>();



                 System.out.println("\t\t\t\tDi Natuto University \n\t\t\t\t    Pililla,Rizal \n\n\t\t\t\tStudent Summary Report \n\n\t\t\tStudent no."+"   Student Name"+"\tCourse ");
                 for(int i=0;i<array.length;i++){
                     int count=0;

                     String [][][] storeAllArray = {{array},{array2},{array3}};
                     String  [][] Array11= {{array[i]},{array2[i]},{array3[i]}};
                     String  [][] Array12= {{array[i]},{array2[i]}};
                     map2.put(array3[i], array2[i]);
                     map.put(array3[i], array[i]);
                 }
                 for (String key : map.keySet())
                     System.out.println("\t\t\t"+map.get(key)+ "\t\t "  +"\t\t"+key);
                 for (String key2 : map2.keySet())

                     System.out.println("\t\t\t\t\t"+map2.get(key2));

                 System.out.println("\n");
             }

             System.out.print("Continue? (Y/N)");
             transaction2=j.nextLine();
             }while(transaction2.equalsIgnoreCase("Y"));
         }
         if(menu.equalsIgnoreCase("sgr")){

             String[] array = studentnumberA.toArray(new String[studentnumberA.size()]);
             String[] array2 = studentnameA.toArray(new String[studentnameA.size()]);
             String[] array3 = courseA.toArray(new String[courseA.size()]);
             Double[] array4 = prelimA.toArray(new Double[prelimA.size()]);
             Double[] array5 = midtermA.toArray(new Double[midtermA.size()]);
             Double[] array6 = finalsA.toArray(new Double[finalsA.size()]);
             String [][] storeAllArray = {array,array2,array3};


             System.out.println("\t\t\t\tDi Natuto University \n\t\t\t\t    Pililla,Rizal \n\n\t\t\t\tStudent Grade Report \n\n\t\t\tStudent no."+"   Student Name"+"\tCourse"+"\t\t\tAverage");

             for(int i=0;i<array.length;i++){
                 Double sum = (array4[i]+array5[i]+array6[i])/3;
                 System.out.println("\t\t\t"+array[i]+"\t\t "+array2[i]+"\t\t"+array3[i]+"\t\t\t"+sum);
             }
             System.out.print("\n\n\t\t\tM\t\tMain Menu\n\t\t\tX\t\tExit\nChoice: ");
             transaction3=j.nextLine();
             if(transaction3.equalsIgnoreCase("X")){
                 System.out.print("\t\t\t\tDi Natuto University \n\t\t\t\t    Pililla,Rizal \n\n\t\t\t\tThank you for using Di Natuto Grading System...");
                 System.exit(0);
             }
         }
         }while(transaction.equalsIgnoreCase("N")||transaction2.equalsIgnoreCase("N")||transaction3.equalsIgnoreCase("N"));

}




}

2 个答案:

答案 0 :(得分:0)

这取决于您希望如何访问数据。如果您想使用id作为参考,您可以执行以下操作:

TreeMap<String,String[]> map = new TreeMap<String,String[]>();

String[]可以保存名称和课程字符串,String将设置为ID。然后要访问它,你会做类似的事情:

String name = map.get(id)[0];
String course = map.get(id)[1];

另外,要添加数据,您可以:

String id = "5";
String name = "jomar";
String course = "computer science";

map.put(id, new String[]{name, course});

答案 1 :(得分:0)

如果您定义如下所示的TreeMap,则为是,TreeMap中只有两个字符串,即键和映射值

TreeMap<String,String> map = new TreeMap<String,String>();

但是如果你将TreeMap定义为

TreeMap<String,Student> map = new TreeMap<String,Student>();

public class Student{ // you might need to implement Comparable interface
private int id;
private String name;
private String course;

//getter and setter and other methods

}

您甚至可以比较三个以上的字符串。我希望你会找到自己的方式。

如果您只需要排序信息,例如学生,列表也是不错的选择

 List<Student> list = new ArrayList<Student>(); 
 Collections.sort(list); //you will have to implement comparable interface on Student class and do it with id on overrided compareTo() method