java - I don't know where my String index out of range: 0 error is coming from -
i'm trying take data out of txt file , create comparable objects out of data , add them array. after array created, want make 2d array stores 1 in slot if 2 options meet requirements. keep getting string index out of range: 0 error though , not know comes from.
import java.util.*; import java.io.*; public class coursescheduler { public int numberofcourses; public int[][] adjacent; public course[] courses; public coursescheduler(string filename) { file file = new file(filename); try{ scanner scan = new scanner(file); numberofcourses = scan.nextint(); courses = new course[numberofcourses]; adjacent = new int[numberofcourses][numberofcourses]; scan.usedelimiter(",|\\n"); for(int = 0; < numberofcourses;i ++){ if(scan.hasnext()){ string dept = scan.next(); string num = scan.next(); string building = scan.next(); string room = scan.next(); string instruct = scan.next(); courses[i] = new course(dept, num, building, room, instruct); } } } catch(filenotfoundexception ex){ system.out.println("file not found"); } for(int x = 0;x<numberofcourses;x++){ for(int y = 0;y<numberofcourses;y++){ adjacent[x][y] = (courses[x].compare(courses[y])); } }
}
this code main class
public class course{ string department; string coursenum; string buildingcode; string roomcode; string instructorname; public course(string dept, string number, string building, string room, string instructor){ department = dept; coursenum = number; buildingcode = building; roomcode = room; instructorname = instructor; } public string getdept(){ return department; } public string getcourse(){ return coursenum; } public string getbuilding(){ return buildingcode; } public string getroom(){ return roomcode; } public string getinstructor(){ return instructorname; } public string tostring(){ return department + ";" + coursenum + ";" + buildingcode + ";" + roomcode + ";" + instructorname; } public int compare(course comp){ int ans = 1; string compnum = comp.getcourse(); if(instructorname == comp.getinstructor()) ans = 0; if(buildingcode == comp.getbuilding()){ if(roomcode == comp.getroom()) ans = 0; } if(department == comp.getdept()){ if(coursenum.charat(0) == compnum.charat(0)) ans = 0; } return ans; } }
this code course class
educated guess: error coming line:
if(coursenum.charat(0) == compnum.charat(0)) ans = 0;
either coursenum or compnum empty.
Comments
Post a Comment