Note that there are some explanatory texts on larger screens.

plurals
  1. POArray Index out of bounds
    primarykey
    data
    text
    <p>This is what I'm trying to do: I'm reading a file in from the command line. The file contains a list of data,below this paragraph is what it looks like. The problem I'm having is with the if statements.</p> <pre><code>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]; ArrayList allVehicles = new ArrayList(); for (int i = 0; i &lt; autos.length; i++) { autos[i] = input.nextLine(); } int j = 0; int i = 0; while (i++ &lt; autos.length) { if (vehicle.equalsIgnoreCase(autos[j++])) { Vehicle v = new Vehicle(); v.setOwnerName(autos[j]); allVehicles.add(v); }else if(car.equalsIgnoreCase(autos[j++])){ Car c = new Car(); c.setOwnerName(autos[j]); allVehicles.add(c); } } for(Object a: allVehicles){ System.out.println(a); } } } </code></pre> <p>In pseudo code this would be:</p> <pre><code>while i is less than the length of the string array if you see the word vehicle create a new vehicle object and add it to the arrayList. if you see the word car create a new car object and add it to the arrayList. ..... </code></pre> <p>The problems is that I get an arrayOutOfBounds exception with the code I'm using.</p> <p>I understand that j++ is what is wrong, but how else am I supposed to iterate through the string array so that I can read each line and create the appropriate objects? I'm at a loss as to what to do. I need some help.</p> <pre><code>foreign car aMarioy Mario's house (777) 777-7777 gmario@mario.com false black Italy 4415.91 truck aDougy Doug's house (123) 456-7890 hdoug@doug.com 30 61234.56 8/10/2003 vehicle aRobby Rob's house (987) 654-3210 irob@rob.com bicycle bTommy Tom's house (246) 810-1214 jtom@tom.com 7 truck bGeorge George's house (666) 666-6666 kgeorge@george.com 25 51234.56 12/4/2004 vehicle bTim Tim's house (111) 111-1111 tim@tim.com bicycle bJim Jim's house (555) 555-5555 Ajim@jim.com 5 american car bJohn John's house (888) 888-8888 Bjohn@john.com true green false true car cKen Ken's house (999) 999-9999 Cken@ken.com false orange foreign car cMario Mario's house (777) 777-7777 Dmario@mario.com false black Italy 4415.91 american car gSam Sam's house (333) 333-3333 Hsam@sam.com false blue true false </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload