Java assignmnent due tonigh!! error message "Index 1 out of bounds for length 1"
-
I have an assignment where the main class output mentions Index 1 out of bounds for length 1 and im trying to read a csv file which has 4 rows and 4 columns below is my code and csv file. Please help the assignment is due tonight at midnight Eastern time :/ I am in my first year of computer science and the assignment is for my introduction of Object-oriented programing, I still consider myself beginner. main class:
public static void main(String[]args) {
String relativePath = "data/technicians 2.numbers";
List<BaseTechnician> technicians = new ArrayList<>();try { List<String\[\]> data = CSVReader.readTechnicians(relativePath); if(data != null) { for(String\[\]technician : data) { String position = technician\[0\]; String name = technician\[1\]; String technicianId = technician\[2\]; double salaryOrHourlyRate = Double.parseDouble(technician\[3\]); BaseTechnician emp; switch(position) { case " Fulltime ": emp = new FullTimeTechnician(name, technicianId,salaryOrHourlyRate); break; case " PartTime ": emp = new PartTimeTechnician(name, technicianId, salaryOrHourlyRate); break; default: System.out.println(" Wrong technician position "); continue; } technicians.add(emp); for(BaseTechnician tech : technicians ) { tech.displayDetails(); System.out.println(" Calculated Salary : " + tech.calculateSalary()); System.out.println(); } } } else { System.out.println("Error Failed to read data from the technician CSV file "); } } catch (Exception e) { System.out.println(" Error occured " + e.getMessage() ); } }
}
and csv file:
FullTime David Jonathan FT1001 50000
PartTime Keith Peters PTE 2001.00 24.5
FullTime Mary Aramal FTE1003 60000
PartTime Hassan Otosh PTE 2002.00 30.5
FullTime Bernard Becker FTE1004 40000
PartTime Roselyn Anne PTE 2003.00 24.5
FullTime James Lead FTE1005 80000 -
I have an assignment where the main class output mentions Index 1 out of bounds for length 1 and im trying to read a csv file which has 4 rows and 4 columns below is my code and csv file. Please help the assignment is due tonight at midnight Eastern time :/ I am in my first year of computer science and the assignment is for my introduction of Object-oriented programing, I still consider myself beginner. main class:
public static void main(String[]args) {
String relativePath = "data/technicians 2.numbers";
List<BaseTechnician> technicians = new ArrayList<>();try { List<String\[\]> data = CSVReader.readTechnicians(relativePath); if(data != null) { for(String\[\]technician : data) { String position = technician\[0\]; String name = technician\[1\]; String technicianId = technician\[2\]; double salaryOrHourlyRate = Double.parseDouble(technician\[3\]); BaseTechnician emp; switch(position) { case " Fulltime ": emp = new FullTimeTechnician(name, technicianId,salaryOrHourlyRate); break; case " PartTime ": emp = new PartTimeTechnician(name, technicianId, salaryOrHourlyRate); break; default: System.out.println(" Wrong technician position "); continue; } technicians.add(emp); for(BaseTechnician tech : technicians ) { tech.displayDetails(); System.out.println(" Calculated Salary : " + tech.calculateSalary()); System.out.println(); } } } else { System.out.println("Error Failed to read data from the technician CSV file "); } } catch (Exception e) { System.out.println(" Error occured " + e.getMessage() ); } }
}
and csv file:
FullTime David Jonathan FT1001 50000
PartTime Keith Peters PTE 2001.00 24.5
FullTime Mary Aramal FTE1003 60000
PartTime Hassan Otosh PTE 2002.00 30.5
FullTime Bernard Becker FTE1004 40000
PartTime Roselyn Anne PTE 2003.00 24.5
FullTime James Lead FTE1005 80000Simple: Java indexes run from 0 to N-1, where N is the number of elements in the collection. So if you have an array with three elements, the only valid indexes are 0, 1, and 2 - any index that is 3 or higher or that is negative is invalid and you will get this error message. In your specific case, the error is telling you that you are using an index of 1 to access a collection with a single element - so the only valid index will be zero. Read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] to find out where the error occurs and than use the debugger to find out why the collection contains fewer elements than you thought, or why the index is bigger than it should be. (It's primarily about syntax errors, but the message format is generally similar for run time errors as well) [edit] But one thing I did notice while adding code blocks to your original post is that that isn't CSV data: CSV stands for "Comma Separated Values" and your data as shown contains no commas ... this may be relevant to your problem ... check the data you read with the debugger and you'll see what I mean. [edit]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Simple: Java indexes run from 0 to N-1, where N is the number of elements in the collection. So if you have an array with three elements, the only valid indexes are 0, 1, and 2 - any index that is 3 or higher or that is negative is invalid and you will get this error message. In your specific case, the error is telling you that you are using an index of 1 to access a collection with a single element - so the only valid index will be zero. Read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] to find out where the error occurs and than use the debugger to find out why the collection contains fewer elements than you thought, or why the index is bigger than it should be. (It's primarily about syntax errors, but the message format is generally similar for run time errors as well) [edit] But one thing I did notice while adding code blocks to your original post is that that isn't CSV data: CSV stands for "Comma Separated Values" and your data as shown contains no commas ... this may be relevant to your problem ... check the data you read with the debugger and you'll see what I mean. [edit]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Hey! Thanks for the reply I ended up using the debugger and made some changes with the data index and my code runs! Appreciate it!
You're welcome!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
I have an assignment where the main class output mentions Index 1 out of bounds for length 1 and im trying to read a csv file which has 4 rows and 4 columns below is my code and csv file. Please help the assignment is due tonight at midnight Eastern time :/ I am in my first year of computer science and the assignment is for my introduction of Object-oriented programing, I still consider myself beginner. main class:
public static void main(String[]args) {
String relativePath = "data/technicians 2.numbers";
List<BaseTechnician> technicians = new ArrayList<>();try { List<String\[\]> data = CSVReader.readTechnicians(relativePath); if(data != null) { for(String\[\]technician : data) { String position = technician\[0\]; String name = technician\[1\]; String technicianId = technician\[2\]; double salaryOrHourlyRate = Double.parseDouble(technician\[3\]); BaseTechnician emp; switch(position) { case " Fulltime ": emp = new FullTimeTechnician(name, technicianId,salaryOrHourlyRate); break; case " PartTime ": emp = new PartTimeTechnician(name, technicianId, salaryOrHourlyRate); break; default: System.out.println(" Wrong technician position "); continue; } technicians.add(emp); for(BaseTechnician tech : technicians ) { tech.displayDetails(); System.out.println(" Calculated Salary : " + tech.calculateSalary()); System.out.println(); } } } else { System.out.println("Error Failed to read data from the technician CSV file "); } } catch (Exception e) { System.out.println(" Error occured " + e.getMessage() ); } }
}
and csv file:
FullTime David Jonathan FT1001 50000
PartTime Keith Peters PTE 2001.00 24.5
FullTime Mary Aramal FTE1003 60000
PartTime Hassan Otosh PTE 2002.00 30.5
FullTime Bernard Becker FTE1004 40000
PartTime Roselyn Anne PTE 2003.00 24.5
FullTime James Lead FTE1005 80000Are you sure that line FullTime David Jonathan FT1001 50000 gives FOUR Strings or it gives FIVE Strings? Depends on CSVReader.readTechnicians() Can understand that name is David AND Jonathan or it is just read name:David and id :Jonathan(wrong).. And in what line says exception occuring?
-
Hey! Thanks for the reply I ended up using the debugger and made some changes with the data index and my code runs! Appreciate it!
Olivia8 wrote:
I ended up using the debugger
And from first post... "I am in my first year of computer science" Someone is doing something right. Either you or the teacher or both of you are doing a great job. Even if the teacher covered this in detail, you still get points for paying attention. Not sure I have ever seen anyone before do the same.