On Sep 4, 12:05 pm, Bo Vance <bowman.va...@invalid.invalid> wrote:
SamuelXiao wrote:
Actually, the lab requires me that I can't use the classes that
implement the Collection interface, except for Array class. The above
website seems to use this Collection.sort(). Moreover, I tried to
change some code, but the problem still exists. Are there any better
ways to correct my program?
Concerning your [snipped] request:
> I want to Read data from file(student.txt), then sort the list in
> ascending order. One more question, i want to reports the number of
> students records successfully read as its return value and no
> duplicate record should be read in. How can I do that?
since you cannot use classes from Collections you might examine
the Arrays.binarySearch(Object[] a, Object key) method:
<http://java.sun.com/javase/6/docs/api/java/util/Arrays.html
#binarySearch(java.lang.Object[], java.lang.Object)>
if(Arrays.binarySearch(students, studentNameComparator) < 0) {
// You have no duplicate
}
Sorry again, I decided not using the comparator, instead, i use the
comparable function and, i can successful read the file and store the
data. Nevertheless, the compiler complaint with:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at Main.main(Main.java:24)
I try again and again to debug but still no valid, the problem seems
to be the array objects; students[], because the experiment require me
to assume that there are at most 30 students records,
so, in the main method(): i initialize that
Student[] cRecord = new Student[30];
for(int i = 0; i<cRecord.length;i++){
cRecord[i] = new Student();
}
but then i realize that in the txt file, there may be only a few of
records, e.g. 5 or 6 or 7 records and so on..
if i initialize the student[] in above way, the length of cRecord will
always be 30!
So, in the displayRecords method, i check whether it is out of the
records should be checked. that is:
for(int i=0;(std[i].getName()!=null) && i<std.length;i++){
System.out.println(std[i].getName()); // First
display Acc Name
System.out.println(std[i].getBal()); // Second
display Acc Bal
System.out.println(std[i].getAccId()); // Third Acc
Id
System.out.println();
}
it is ok and shows that only 5 records( because my txt has 5 records
only initially).
but it comes with the
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at Main.main(Main.java:24)
error....how can i solve it??? Sorry for bothering again and again, i
have tried many ways but still no valid, and this experiment is urgent
now.
At line 24 Main.java you have attempted to read a non-existant element.
Without the entire code I cannot help.