Array Out Of Bounds

My array is out of bounds

Page 1 of 1

2 Replies - 874 Views - Last Post: 19 March 2009 - 07:51 AM Rate Topic: -----

#1 mesmer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-January 09

Array Out Of Bounds

Posted 19 March 2009 - 05:22 AM

 

import java.util.*;
import java.io.*;

public class VehicleTest {

   
	public static void main(String[] args) throws FileNotFoundException {

		String vehicle = "vehicle";
		String car = "car";
		String americanCar = "american car";
		String foreignCar = "foreign car";
		String truck = "truck";
		String bicycle = "bicycle";

		File file = new File(args[0]);
		Scanner input = new Scanner(file);

		String[] autos = new String[100];


		for (int i = 0; i < autos.length; i++) {
			autos[i] = input.nextLine();
		}

	   
		int j = 0;
		int i = 0;

		
			while(i++ < autos.length){
				
			  /*the line below is where the exception is thrown */
				if (autos[j++].equalsIgnoreCase(vehicle)) {
				Vehicle v = new Vehicle();
				v.setOwnerName(autos[j++]);
				v.setAddress(autos[j++]);
				v.setPhone(autos[j++]);
				v.setEmail(autos[j++]);

				System.out.println(v.getOwnerName());
				System.out.println(v.getAddress());
				 
		  
				

				

			}
			}

	  


	   










	}
}





Can someone tell me why my array is going out of bounds at the if statement? How can I fix this exception?
Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Array Out Of Bounds

#2 eddieboy665  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 113
  • Joined: 09-April 07

Re: Array Out Of Bounds

Posted 19 March 2009 - 05:25 AM

Try this:

if (autos[j++].equalsIgnoreCase(vehicle)) {
				Vehicle v = new Vehicle();
				v.setOwnerName(autos[j]);
				v.setAddress(autos[j]);
				v.setPhone(autos[j]);
				v.setEmail(autos[j]);



You were incrementing j more than once per iteration.
Was This Post Helpful? 0
  • +
  • -

#3 mesmer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-January 09

Re: Array Out Of Bounds

Posted 19 March 2009 - 07:51 AM

View Posteddieboy665, on 19 Mar, 2009 - 04:25 AM, said:

Try this:

if (autos[j++].equalsIgnoreCase(vehicle)) {
				Vehicle v = new Vehicle();
				v.setOwnerName(autos[j]);
				v.setAddress(autos[j]);
				v.setPhone(autos[j]);
				v.setEmail(autos[j]);



You were incrementing j more than once per iteration.



That did it thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1