Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 168347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:26:13+00:00 2026-05-11T12:26:13+00:00

This is what I’m trying to do: I’m reading a file in from the

  • 0

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.

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 < autos.length; i++) {             autos[i] = input.nextLine();         }          int j = 0;         int i = 0;          while (i++ < 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);         }     } } 

In pseudo code this would be:

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

The problems is that I get an arrayOutOfBounds exception with the code I’m using.

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.

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   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:26:14+00:00Added an answer on May 11, 2026 at 12:26 pm

    A couple of problems:

    • You’re incrementing j in both ‘if’ tests. I haven’t checked to be certain (it’s quite convoluted code, to be honest) but if you make sure that you only increment j when you’ve found a match, that will help.
    • Your test using i basically means it will try to read as many vehicles as there are lines in the file, rather than stopping when you’ve reached the end of the file. Basically you don’t need i here.

    Here’s one changed version:

        while (j < autos.length) {         if (vehicle.equalsIgnoreCase(autos[j])) {             j++;             Vehicle v = new Vehicle();             v.setOwnerName(autos[j++]);             allVehicles.add(v);         } else if(car.equalsIgnoreCase(autos[j])){             j++;             Car c = new Car();             c.setOwnerName(autos[j++]);             allVehicles.add(c);         }     } 

    It would be slightly cleaner to extract the type once though – then you can do the comparisons separately:

        while (j < autos.length) {         String type = autos[j++];         if (vehicle.equalsIgnoreCase(type)) {             Vehicle v = new Vehicle();             v.setOwnerName(autos[j++]);             allVehicles.add(v);         } else if(car.equalsIgnoreCase(type)){             Car c = new Car();             c.setOwnerName(autos[j++]);             allVehicles.add(c);         }     } 

    It’s still not quite how I’d do it, but it’s closer…

    My next step would be to use the scanner more appropriately:

    while (scanner.hasNext()) {     String type = scanner.nextLine();     if (type.equalsIgnoreCase('vehicle')) {         allVehicles.add(new Vehicle(scanner));     } else if (type.equalsIgnoreCase('car')) {         allVehicles.add(new Car(scanner));     }     // ... } 

    Then make the constructor for Vehicle, Car etc do the parsing directly from the scanner.

    The next step would be to separate out the construction from the iteration. Introduce a new method:

    // Use a base type in real code private static Object parseNextVehicle(Scanner scanner) {     String type = scanner.nextLine();     if (type.equalsIgnoreCase('vehicle')) {         return new Vehicle(scanner);     } else if (type.equalsIgnoreCase('car')) {         return new Car(scanner);     }     // ... throw an exception indicating an unknown vehicle type }  // ... and then in the main method, use it like this: while (scanner.hasNextLine()) {     allVehicles.add(parseNextVehicle(scanner)); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You could use the SQLite assembly that's shipped with Mono… May 11, 2026 at 2:50 pm
  • added an answer I think you want to use the Microsoft.WindowsMobile.Status namepsace (specifically… May 11, 2026 at 2:50 pm
  • added an answer Yes this is correct. The semantics of a checkbox are… May 11, 2026 at 2:50 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
This is what I've got. It works. But, is there a simpler or better
This is what I've got. It works. But, is there a simpler or better

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.