Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Java assignmnent due tonigh!! error message "Index 1 out of bounds for length 1"

Java assignmnent due tonigh!! error message "Index 1 out of bounds for length 1"

Scheduled Pinned Locked Moved Java
helpjavadatabasecareerlearning
6 Posts 4 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Olivia8
    wrote on last edited by
    #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

    OriginalGriffO J 2 Replies Last reply
    0
    • O Olivia8

      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

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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!

      "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

      O 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        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!

        O Offline
        O Offline
        Olivia8
        wrote on last edited by
        #3

        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!

        OriginalGriffO J 2 Replies Last reply
        0
        • O Olivia8

          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!

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          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 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

          1 Reply Last reply
          0
          • O Olivia8

            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

            J Offline
            J Offline
            jimakoskx
            wrote on last edited by
            #5

            Are 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?

            1 Reply Last reply
            0
            • O Olivia8

              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!

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups