Я написал эту программу и получил весь код, который вам нужен. Сначала у меня были некоторые ошибки во время выполнения, но я легко их исправил. Теперь он не показывает видимых ошибок, и когда я запускаю его, он ничего не делает.Программа ничего не делает при запуске?
По существу, то, что он должен делать, это распечатать список классов со студентами и номерами идентификаторов, которые были объединены из двух файлов. Он сопоставляет идентификаторы учеников с идентификаторами классов и формирует их в список. Кажется, все это происходит, это просто не печатает.
Мой код приведен ниже. Пожалуйста, дай мне знать, что ты думаешь. Я включил описание наверху и маркеры, где я думаю, что проблема начинается и заканчивается. Также после кода есть пример содержимого двух файлов.
/* This program reads in two files, one which contains a list of student names and id numbers, and another which lists class id numbers.
* Then, the data is taken into arrays which are then used in different sorts. One sorts the id numbers numerically, one sorts the students
* alphabetically, and one sorts the class names alphabetically.The id numbers of students are matched to class and both are compiled into
* a list which then prints out first the class name, then the list of students within it. All done alphabetically.
*
*/
import java.io.*;
import java.util.Scanner;
public class MergingFiles {
public static void main(String[] args) throws IOException {
String[][] studentArray = new String [1000][2];
String[][] classArray = new String[4000][2];
int count = 0;
Scanner sc = new Scanner(new File("students.txt"));
while(sc.hasNextLine()){
String studentInput = sc.nextLine();
String name = studentInput.substring(0, 30).trim();
String id = studentInput.substring(30).trim();
studentArray[count][0] = name;
studentArray[count][1] = id;
count++;
}
for(int i = 0; i < count-1; i++){ //sorts id number numerically
for(int j = 0; j < count-1-i; j++){
if(studentArray[j][1].compareTo(studentArray[j+1][1]) > 0){
String holder = studentArray[j][1];
studentArray[j][1] = studentArray[j+1][1];
studentArray[j+1][1] = holder;
}
}
}
int counter3 = 0;
Scanner sc2 = new Scanner(new File("classes.txt"));
while(sc2.hasNextLine()){
int counter2 = 0;
String classInput = sc2.nextLine();
while(classInput.charAt(counter2)!= ' '){
counter2++;
}
String className = classInput.substring(0, counter2);
while(classInput.charAt(counter2) == ' '){
counter2++;
}
String idNum = classInput.substring(counter2);
int low = 0;
int high = count - 1;
int mid = (high - low)/2 + low;
while(!idNum.equals(studentArray[mid][1])){ //binary search
if(idNum.compareTo(studentArray[mid][1]) < 0){
high = mid - 1;
}else
low = mid + 1;
mid = (high - low)/2 + low;
}
String studentName2 = studentArray[mid][1];
classArray[counter3][0] = className;
classArray[counter2][1] = studentName2;
}
//I THINK THE ISSUE STARTS HERE
for(int a = 0; a < (counter3 - 1); a++){ //sort class names alphabetically
for(int b = 0; b < counter3-1-a; b++){
if(classArray[b][0].compareTo(classArray[b+1][0]) > 0){
String holder2 = classArray[b][0];
classArray[b][0] = classArray[b+1][0];
classArray[b+1][0] = holder2;
}
}
}
for(int c = 0; c < (counter3 - 1); c++){ //sort class names alphabetically
for(int d = 0; d < counter3-1-c; d++){
if((classArray[d][0].compareTo(classArray[d+1][0])) == 0){
if(classArray[d][1].compareTo(classArray[d+1][1]) > 0){
String holder3 = classArray[d][1];
classArray[d][1] = classArray[d+1][1];
classArray[d+1][1] = holder3;
}
}
}
}
String currentClass = "";
for(int s = 0; s < counter3; s++){
if(!classArray[s][0].equals(currentClass)){
currentClass = classArray[s][0];
System.out.print(currentClass);
}
System.out.print(classArray[s][1]);
}
//THIS IS WHERE THE ISSUE ENDS
}
}
studentArray содержимое пример (без // 'ы):
//Lambert, Desmond 451825335
//Johnston, Michaela 143547061
//Wells, Shirley 942366473
// Blevins, Dawson 407183976
// Roy, Benjamin 575069268
// Mitchell, Jaquan 285633099
// Freeman, Nayeli 312234364
// Benson, Myles 755491358
// Wilkinson, Stephany 384506082
// Bradshaw, Nikki 159900631
// Davila, Sarah 788743448
// Wyatt, Eddie 253830483
// Ortega, Josh 891761169
// Pruitt, Deven 533987743
// Harrison, Aliyah 710258372
// Perez, Emerson 611325979
// Stanton, Sonny 430095944
// Rice, Bruce 301915859
// Callahan, Brandon 327995163
// Torres, Jovan 629852538
// Mathis, Timothy 936191071
// Calhoun, Nathanael 107519769
// Mullen, Malik 711847273
// Sims, Marvin 519717164
// Pham, Siena 530779316
// Vincent, Demetrius 618276821
т.д.
classArray содержимое пример (без //' ы):
//ECON101 938597595
//BUS100 951008337
//ECON408 620903271
//PHY101 695451867
//COSC150 392023624
//MATH215 557048539
//COSC325 495522117
//BUS215 185642398
//ECON408 807662685
//MATH215 920062540
//MATH325 517786537
//PHY150 915173832
//BUS100 518392974
//BUS408 410797598
//BUS215 152414047
//PHY150 561839967
и т.д.
Спасибо.
Показать содержимое 'students.txt'. –
Это очень длинный список. – throwaway298
Репрезентативная выборка. То же самое касается кода, если вы можете: обрезать его до наименьшего количества кода, который не выполняет то, что вы ожидаете. Это много кода, чтобы просить людей провалиться. –