美好的一天, 我有一个.csv文件,其数据如下所示,我希望能够搜索该文件。该文件分为Regno,ModuleCode和考试Mark。如何在提供Regno和ModuleCode时搜索文件。在没有唯一列的意义上,此文件是唯一的。请帮忙。
Regno,ModuleCode,ExamMark
1334599, CE152-4-SP, 54.5
1332141, CE153-4-AU, 43.8
1398559, CE162-4-SP, 55.3
1334667, CE141-4-AU, 51.3
1335181, CE162-4-SP, 62.5
1332141, CE154-4-SP, 63.1
1335167, CE154-4-SP, 43.0
1365678, CE155-4-SP, 28.9
1334863, CE155-4-SP, 39.7
1334703, CE101-4-SP, 56.8
1335167, CE161-4-AU, 46.9
1365678, CE153-4-AU, 15.6
1334718, CE141-4-AU, 61.7
1394001, CE151-4-AU, 41.4
1334768, CE163-4-AU, 42.7
1334863, CE152-4-SP, 46.3
1334592, CE155-4-SP, 52.7
1383431, CE161-4-AU, 49.8
1398559, CE151-4-AU, 54.7
1334599, CE155-4-SP, 77.6
1334768, CE101-4-SP, 60.4
1334599, CE153-4-AU, 71.7
1383260, CE153-4-AU, 70.3
1334718, CE153-4-AU, 58.6
1334863, CE153-4-AU, 29.5
1334718, CE152-4-SP, 73.7
1394001, CE101-4-SP, 24.0
1383260, CE152-4-SP, 55.3
1374567, CE142-4-AU, 72.1
1383260, CE151-4-AU, 67.2
1394001, CE161-4-AU, 72.7
1398559, CE161-4-AU, 68.6
1332141, CE151-4-AU, 65.5
1383260, CE101-4-SP, 64.4
1334703, CE162-4-SP, 96.4
1398559, CE142-4-AU, 48.5
1334768, CE155-4-SP, 37.3
1332568, CE155-4-SP, 66.9
1334718, CE154-4-SP, 56.8
1364569, CE141-4-AU, 62.4
1334568, CE162-4-SP, 38.3
1334599, CE161-4-AU, 86.5
1334667, CE155-4-SP, 31.7
1334592, CE141-4-AU, 56.2
1334592, CE152-4-SP, 95.8
1334568, CE161-4-AU, 25.7
1383431, CE151-4-AU, 66.2
1394001, CE155-4-SP, 44.7
1332141, CE161-4-AU, 57.8
1332568, CE153-4-AU, 62.0
1334568, CE164-4-SP, 37.1
1334718, CE161-4-AU, 86.0
1354564, CE155-4-SP, 51.3
1364569, CE154-4-SP, 29.8
1334768, CE151-4-AU, 22.3
1383431, CE141-4-AU, 59.0
1398559, CE163-4-AU, 56.5
1365678, CE142-4-AU, 43.2
1374567, CE155-4-SP, 48.5
1394001, CE153-4-AU, 58.2
1374567, CE162-4-SP, 47.2
1354564, CE162-4-SP, 45.6
1334863, CE141-4-AU, 43.5
1334667, CE151-4-AU, 44.3
1394001, CE154-4-SP, 55.0
1334768, CE164-4-SP, 28.6
1334568, CE163-4-AU, 32.2
1394001, CE141-4-AU, 56.6
1334592, CE154-4-SP, 49.9
1335181, CE151-4-AU, 78.1
1365678, CE161-4-AU, 60.0
1374567, CE152-4-SP, 59.4
1334599, CE151-4-AU, 68.8
1334768, CE161-4-AU, 49.8
1398559, CE164-4-SP, 63.4
1334863, CE154-4-SP, 20.7
1364569, CE101-4-SP, 52.1
1334703, CE142-4-AU, 70.5
1383260, CE154-4-SP, 61.6
1334718, CE101-4-SP, 57.6
1334568, CE142-4-AU, 60.6
1335167, CE141-4-AU, 52.5
1334599, CE101-4-SP, 85.3
1374567, CE151-4-AU, 60.8
1334568, CE101-4-SP, 34.5
1334592, CE161-4-AU, 73.1
1332141, CE152-4-SP, 62.5
1365678, CE151-4-AU, 44.8
1335167, CE152-4-SP, 64.5
1398559, CE101-4-SP, 60.5
1383431, CE152-4-SP, 63.4
1383260, CE141-4-AU, 78.5
1374567, CE101-4-SP, 58.7
1332568, CE101-4-SP, 59.7
1365678, CE101-4-SP, 40.5
1334703, CE155-4-SP, 70.2
1335167, CE101-4-SP, 56.1
1334599, CE154-4-SP, 87.0
1334718, CE151-4-AU, 62.9
1332568, CE152-4-SP, 55.6
1354564, CE161-4-AU, 45.9
1335167, CE155-4-SP, 30.2
1334667, CE153-4-AU, 40.8
1354564, CE163-4-AU, 37.6
1398559, CE155-4-SP, 41.3
1334568, CE151-4-AU, 42.2
1334592, CE101-4-SP, 71.6
1374567, CE153-4-AU, 77.8
1335181, CE155-4-SP, 66.5
1354564, CE151-4-AU, 37.3
1334667, CE101-4-SP, 31.0
1383431, CE153-4-AU, 48.2
1354564, CE142-4-AU, 34.1
1334599, CE141-4-AU, 67.1
1383431, CE101-4-SP, 69.7
1335167, CE153-4-AU, 40.3
1364569, CE161-4-AU, 60.3
1383431, CE154-4-SP, 93.3
1365678, CE152-4-SP, 43.5
1334703, CE151-4-AU, 69.5
1383260, CE155-4-SP, 40.0
1332141, CE141-4-AU, 68.1
1334718, CE155-4-SP, 52.2
1335181, CE163-4-AU, 65.2
1364569, CE155-4-SP, 36.0
1332568, CE154-4-SP, 66.0
1334768, CE162-4-SP, 51.7
1364569, CE151-4-AU, 46.9
1334863, CE151-4-AU, 37.9
1334568, CE155-4-SP, 53.8
1335167, CE151-4-AU, 46.1
1332568, CE151-4-AU, 71.3
1354564, CE101-4-SP, 50.9
1334863, CE161-4-AU, 25.2
1334703, CE164-4-SP, 45.7
1334703, CE163-4-AU, 55.0
1335181, CE142-4-AU, 70.4
1335181, CE101-4-SP, 70.7
1334667, CE154-4-SP, 61.3
1334703, CE161-4-AU, 45.3
1332568, CE141-4-AU, 62.2
1374567, CE161-4-AU, 70.4
1365678, CE162-4-SP, 69.3
1334768, CE142-4-AU, 52.6
1394001, CE152-4-SP, 18.9
1335181, CE164-4-SP, 74.4
1354564, CE164-4-SP, 48.0
1332568, CE161-4-AU, 77.2
1383431, CE155-4-SP, 58.2
1334592, CE151-4-AU, 69.6
1335181, CE161-4-AU, 62.9
1334667, CE161-4-AU, 67.0
1332141, CE101-4-SP, 95.5
1364569, CE153-4-AU, 54.5
1383260, CE161-4-AU, 71.9
代码:
private void displayCourseModules() throws IOException{
File f= openFile("enrolment.csv");
BufferedReader fr = null;
fr = Buffered(fr, f);
// fr.i
byte len =8;
while(len==8)
{
String h = null;
try
{
h = fr.readLine();
}
catch (IOException ex)
{
System.out.println(ex);
}
byte length_2= 1;
try
{
String hr[] = h.split(",");
hr[0] = hr[0].concat(",");
hr[1] = hr[1].concat(",");
hr[2] = hr[2].concat(",");
hr[3] = hr[3].concat(",");
hr[4] = hr[4].concat(",");
hr[5] = hr[5].concat(",");
hr[6] = hr[6].concat(",");
hr[7] = hr[7].concat(",");
hr[8] = hr[8].concat(",");
enrollmentcolumn0.append(hr[0]);
enrollmentcolumn1.append(hr[1]);
enrollmentcolumn2.append(hr[2]);
enrollmentcolumn3.append(hr[3]);
enrollmentcolumn4.append(hr[4]);
enrollmentcolumn5.append(hr[5]);
enrollmentcolumn6.append(hr[6]);
enrollmentcolumn7.append(hr[7]);
enrollmentcolumn8.append(hr[8]);
}
catch(NullPointerException ew)
{
len=0;
}
}
String enrollmentColumns[] = new String[9];
enrollmentColumns[0]= enrollmentcolumn0.toString();
enrollmentColumns[1]= enrollmentcolumn1.toString();
enrollmentColumns[2]= enrollmentcolumn2.toString();
enrollmentColumns[3]= enrollmentcolumn3.toString();
enrollmentColumns[4]= enrollmentcolumn4.toString();
enrollmentColumns[5]= enrollmentcolumn5.toString();
enrollmentColumns[6]= enrollmentcolumn6.toString();
enrollmentColumns[7]= enrollmentcolumn7.toString();
enrollmentColumns[8]= enrollmentcolumn8.toString();
String enrollmentColumnArray0[]= enrollmentColumns[0].split(",");
String enrollmentColumnArray1[]= enrollmentColumns[1].split(",");
String enrollmentColumnArray2[]= enrollmentColumns[2].split(",");
String enrollmentColumnArray3[]= enrollmentColumns[3].split(",");
String enrollmentColumnArray4[]=enrollmentColumns[4].split(",");
String enrollmentColumnArray5[]= enrollmentColumns[5].split(",");
String enrollmentColumnArray6[]= enrollmentColumns[6].split(",");
String enrollmentColumnArray7[]= enrollmentColumns[7].split(",");
String enrollmentColumnArray8[]= enrollmentColumns[8].split(",");
// System.out.printf(enrollmentColumnArray8[0]+"hi");
double yearmark1[]= new double[9];
byte se = 0;
for(int a=0;a<=enrollmentColumnArray8.length;a++)
{
try
{
if( enrollmentColumnArray0[a].equals(registrationNumber))
{
counter=1;
System.out.println("________________________________________");
System.out.println("List of Modules");
System.out.println("Module Code\t\tModule Name\t\t\t\t\t\tCourseWorkMark\tExamMarks");
System.out.println(enrollmentColumnArray1[a]+"\t\t"+getModuleName(enrollmentColumnArray1[a])+"\t\t\t\t"+ getCourseWorkMark(registrationNumber,enrollmentColumnArray1[a])+"\t\t"+getExamMark(registrationNumber,enrollmentColumnArray1[a])+"\t\t"+moduleMark(getExamMark(registrationNumber,enrollmentColumnArray1[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray1[a])));
//yearmark1[0] =Double.parseDouble(moduleMark(getExamMark(registrationNumber,enrollmentColumnArray1[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray1[a])));
// if(yearmark1[0]>=40)
// {
se++;
// }
System.out.println(enrollmentColumnArray2[a]+"\t\t"+getModuleName(enrollmentColumnArray2[a])+"\t\t\t\t"+ getCourseWorkMark(registrationNumber,enrollmentColumnArray2[a])+"\t\t"+getExamMark(registrationNumber,enrollmentColumnArray2[a])+"\t\t"+moduleMark(getExamMark(registrationNumber,enrollmentColumnArray2[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray2[a])));
// yearmark1[1] =Double.parseDouble(moduleMark(getExamMark(registrationNumber,enrollmentColumnArray2[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray2[a])));
// if(yearmark1[1]>=40)
// {
se++;
// }
System.out.println(enrollmentColumnArray3[a]+"\t\t"+getModuleName(enrollmentColumnArray3[a])+"\t\t\t\t"+ getCourseWorkMark(registrationNumber,enrollmentColumnArray3[a])+"\t\t"+getExamMark(registrationNumber,enrollmentColumnArray3[a])+"\t\t"+moduleMark(getExamMark(registrationNumber,enrollmentColumnArray3[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray3[a])));
// yearmark1[2] =Double.parseDouble(moduleMark(getExamMark(registrationNumber,enrollmentColumnArray3[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray3[a])));
// if(yearmark1[2]>=40)
// {
se++;
// }
System.out.println(enrollmentColumnArray4[a]+"\t\t"+getModuleName(enrollmentColumnArray4[a])+"\t\t\t\t"+ getCourseWorkMark(registrationNumber,enrollmentColumnArray4[a])+"\t\t"+getExamMark(registrationNumber,enrollmentColumnArray4[a])+"\t\t"+moduleMark(getExamMark(registrationNumber,enrollmentColumnArray4[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray4[a])));
// yearmark1[3] =Double.parseDouble(moduleMark(getExamMark(registrationNumber,enrollmentColumnArray4[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray4[a])));
// if(yearmark1[3]>=40)
// {
se++;
// }
System.out.println(enrollmentColumnArray5[a]+"\t\t"+getModuleName(enrollmentColumnArray5[a])+"\t\t\t\t"+ getCourseWorkMark(registrationNumber,enrollmentColumnArray5[a])+"\t\t"+getExamMark(registrationNumber,enrollmentColumnArray5[a])+"\t\t"+moduleMark(getExamMark(registrationNumber,enrollmentColumnArray5[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray5[a])));
// yearmark1[4] =Double.parseDouble(moduleMark(getExamMark(registrationNumber,enrollmentColumnArray5[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray5[a])));
// if(yearmark1[4]>=40)
// {
se++;
// }
System.out.println(enrollmentColumnArray6[a]+"\t\t"+getModuleName(enrollmentColumnArray6[a])+"\t\t\t\t\t\t"+ getCourseWorkMark(registrationNumber,enrollmentColumnArray6[a])+"\t\t"+getExamMark(registrationNumber,enrollmentColumnArray6[a])+"\t\t"+moduleMark(getExamMark(registrationNumber,enrollmentColumnArray6[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray6[a])));
// yearmark1[5]=Double.parseDouble(moduleMark(getExamMark(registrationNumber,enrollmentColumnArray6[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray6[a])));
// if(yearmark1[5]>=40)
// {
se++;
// }
System.out.println(enrollmentColumnArray7[a]+"\t\t"+getModuleName(enrollmentColumnArray7[a])+"\t\t\t\t\t"+ getCourseWorkMark(registrationNumber,enrollmentColumnArray7[a])+"\t\t"+getExamMark(registrationNumber,enrollmentColumnArray7[a])+"\t\t"+moduleMark(getExamMark(registrationNumber,enrollmentColumnArray7[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray7[a])));
// yearmark1[6] =Double.parseDouble(moduleMark(getExamMark(registrationNumber,enrollmentColumnArray7[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray7[a])));
// if(yearmark1[6]>=40)
// {
se++;
// }
System.out.println(enrollmentColumnArray8[a]+"\t\t"+getModuleName(enrollmentColumnArray8[a])+"\t\t\t\t"+ getCourseWorkMark(registrationNumber,enrollmentColumnArray8[a])+"\t\t"+getExamMark(registrationNumber,enrollmentColumnArray8[a])+"\t\t"+moduleMark(getExamMark(registrationNumber,enrollmentColumnArray8[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray8[a])));
// yearmark1[7] =Double.parseDouble(moduleMark(getExamMark(registrationNumber,enrollmentColumnArray8[a]), getCourseWorkMark(registrationNumber,enrollmentColumnArray8[a])));
// if(yearmark1[7]>=40)
// {
se++;
// }
}
}
catch(Exception e)
{
// System.out.println(e);
}
}
yearmark1[8]= java.lang.Math.round((yearmark1[0]+yearmark1[1]+yearmark1[2]+yearmark1[3]+yearmark1[4]+yearmark1[5]+yearmark1[6]+yearmark1[7])/8);
System.out.println("YearMark: "+yearmark1[8]);
System.out.println("No of Modules Which the student has achieved a pass mark of 40% or more. : "+se);
}
private String getExamMark(String studentRegistrationNo1,String moduleCode1) throws IOException
{
File f= openFile("examMarks.csv");
BufferedReader fr = null;
fr = Buffered(fr,f);
int he;
// byte len =1;
String line = null;
int len=63;
while (len==63)
{
// line = fr.readLine();
// CharBuffer e = null;
try {
// info = in.readLine();
h = fr.readLine();
// sb.append(h);
}
catch (IOException ex) {
System.out.println(ex);
}
byte length_2= 1;
// r[i]=h;
try{
r= h.split(",");
// System.out.println(r.length);
// r[0] = r[0].concat(",");
// r[1] = r[1].concat(",");
//r[2] = r[2].concat(",");
examMark= r[0];
// System.out.println(r[0]);
sb.append(r[0]);
sb.append(",");
rb.append(r[1]);
rb.append(",");
fb.append(r[2]);
fb.append(",");
// System.out.println(r[0]);
// System.out.println(a[0]);
// r[0]= r[0].concat(",");
}
catch(NullPointerException e){
len=0;
}
}
// System.out.println(r.length);
examRegno = sb.toString();
examModulecode = rb.toString();
examMark = fb.toString();
// System.out.println(examRegno);
// System.out.println(examRegno );
String examRegnoArray[]= examRegno.split(",");
//System.out.println(examRegnoArray.length);
String examModulecodeArray[]= examModulecode.split(",");
// System.out.println( examModulecodeArray[5]);
String examMarkArray[]= examMark.split(",");
// System.out.println(examRegnoArray.length);
// System.out.println(examMarkArray[154]);
try
{
for(int i=0;i<=examMarkArray.length;++i)
if((examRegnoArray[i+9].equals(studentRegistrationNo1) && examModulecodeArray[i+9].equals(moduleCode1))){
return ((examMarkArray[i+1]));
}
// return examRegnoArray.length+"";
}
catch(Exception e)
{
// System.out.println(e);
// JOptionPane.showMessageDialog(null,e);
}
// System.out.println("hi");
// System.out.println(examModulecode);
// System.out.println(examMark);
return null;
}
答案 0 :(得分:0)
您提出的问题表明了许多数据库构建问题中出现的重要细节。
没有唯一的专栏。
检查您提供的清单确认,实际上,第1列和第2列都不能单独用作关键字。
但是,仅仅因为没有唯一列,并不意味着您没有任何可能的密钥。这两个非唯一列可以用作一起键,作为compound key,这似乎可以与您提供的列表一起使用。
换句话说,当您同时拥有RegNo
和ModuleCode
时,这些会映射到唯一值。
例如,密钥1334599 CE152-4-SP
将唯一映射到54.5
。
有一个Collection,允许您处理您应该能够用来实现它的键及其值。
答案 1 :(得分:0)
这应该可以满足您的需求:
public static void main(String[] args) {
String regno = "", moduleCode = "", examMark = "";
String row;
try {
Scanner csvFile = new Scanner(new File("test.csv"));
csvFile.nextLine(); // skip first line
while (csvFile.hasNextLine()) {
row = csvFile.nextLine();
String[] fields = row.split(", ");
regno = fields[0];
moduleCode = fields[1];
examMark = fields[2];
if (regno.equals("1334599") && moduleCode.equals("CE152-4-SP")) {
System.out.println(examMark);
}
}
} catch (FileNotFoundException ex) {
System.out.println(ex);
}
}
如果您希望用户能够提供'Regno'和'ModuleCode'值,那么您可以从用户那里获得这两个值的输入,并在if语句中提供它们。