Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Reading in data from a text file and adding to ArrayList

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    My Mood
    Bitchy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Reading in data from a text file and adding to ArrayList

    I need to read in data from a text file with a scanner, which contains a list of vehicles. I'm then adding these Vehicles to an ArrayList. Each we have separate subclasses already written for a Car, Bike, raceCar, and Vehicle class. Each vehicle type is identified by a single char. (ex. raceCar 'R', Bike 'B' etc). So if I'm scanning from the file and I reach an 'R' that means that I have to scan the next 5 lines following it for (Make, Model, numWheels, engineSize, raceType). However, if I scan char 'C' or 'B' i'll have to read only the next 4.

    I keep getting this error:

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:658)
    at VehicleDriver.inputVehicles(VehicleDriver.java:18)
    at VehicleDriver.main(VehicleDriver.java:9)


    --------------------------
    From here i'm not sure what i'm supposed to do. And honestly i'm not even sure if this code I have will work and preform the necessary task. Any tips on what I could do?


    import java.util.*;
    import java.io.*;//File
    public class VehicleDriver 
    {
       public static void main (String [] args)throws IOException
       {
          ArrayList <Vehicle> vehicles = new ArrayList<Vehicle> ();
     
          inputVehicles(vehicles);
     
        }//end main()
     
        public static void inputVehicles(ArrayList<Vehicle> vehicles)throws IOException
         {    
             File infile = new File("vehicles.txt");  
             Scanner scan = new Scanner(infile);
             String x = "";
             char c = x.charAt(0);
     
             String make = "";
             String model = "";
             int numWheels = 0;
             int numGears = 0;
             String engineSize = "";
             String raceType = "";
     
     
     
             if(!infile.exists())
             {
                System.out.println("Your file does not exist");
                System.exit(0);
             }//end if
     
             while(scan.hasNext())
             {
     
                 if(c == 'R')
                 {
                    make = scan.nextLine();
                    model = scan.nextLine();
                    numWheels = scan.nextInt();
                    engineSize = scan.nextLine();
                    raceType = scan.nextLine();
     
                    RaceCar rc = new RaceCar(make, model, numWheels, engineSize, raceType);
                    vehicles.add(rc);
                    System.out.println(rc);
     
                 }  
                 else if(c == 'B') 
                 {
                    make = scan.nextLine();
                    model = scan.nextLine();
                    numWheels = scan.nextInt();
                    numGears = scan.nextInt();
     
                    Bike bi = new Bike(make, model, numWheels, numGears);
                    vehicles.add(bi);
                    System.out.println(bi);
                 }
                 else if(c == 'C')
                 {
                    make = scan.nextLine();
                    model = scan.nextLine();
                    numWheels = scan.nextInt();
                    engineSize = scan.nextLine();
     
                    Car ca = new Car(make, model, numWheels, engineSize);
                    vehicles.add(ca);
                    System.out.println(ca);
                 }
     
     
             }//end forloop
               scan.close();
       }//end inputVehicles
     
    }//end class

    Below is the textfile

    R
    Dodge
    Charger
    4
    5.7L
    Nascar
    C
    Volkswagon
    Taureg
    4
    V6
    B
    Schwinn
    Super Deluxe
    2
    2
    R
    Aston Martin
    DB9
    4
    6.0L
    Le Mans
    B
    Cannondale
    Trigger 29 3
    2
    30
    C
    Ford
    Focus
    4
    2.0L

    Vehicle Class
    public class Vehicle 
    {
       private String make;
       private String model;
       protected int numWheels;
     
       public Vehicle()
       {
          make = model = "";
          numWheels = 0;
       }//end default 
     
       public Vehicle(String make, String model, int numWheels)
       {
          this.make = make;
          this.model = model;
          this.numWheels = numWheels;
       }//end 2nd constructor
     
       public String getMake()
       {
          return make;
       }//end getMake()
     
       public String getModel()
       {
          return model;
       }//end getModel()
     
       public int getNumWheels()
       {
          return numWheels;
       }//end getNumWheels()
     
       //setters go here
     
       public String toString()
       {
          return "Make: " + make + 
                 "\nModel: " + model +
                 "\nNumber of Wheels: " + numWheels;
       }//end toString()
    }//end class Vehicle

    Car Class
    public class Car extends Vehicle
    {
       private String engineSize;
     
       public Car()
       {
          super();
          engineSize = "";
       }//end super constructor
     
       public Car(String make, String model, int numWheels, String engineSize)
       {
          super(make, model, numWheels);
          this.engineSize = engineSize;
       }
     
       public String getEngineSize()
       {
          return engineSize;
       }
     
       public String toString()
       {
          String str = "";
          str += super.toString();
          str += "\nEngine size: " + engineSize;
          return str;
       }
    }

    RaceCar Class
    public class RaceCar extends Car
    {
       private String raceType;
     
       public RaceCar()
       {
          super();//default constructor of Car
          raceType = "";
       }//end super constructor
     
       public RaceCar(String make, String model, int numWheels, String engineSize, String raceType)
       {
          super(make, model, numWheels, engineSize);
          this.raceType = raceType;
       }//end 2nd constructor
     
       public String getRaceType()
       {
          return raceType;
       }
     
       public String toString()
       {
          String str = "";
          str += super.toString();
          str += "\nRace Type: " + raceType;
          return str;
       }
    }


    Bike Class
    public class Bike extends Vehicle
    {
       private int numGears;
     
       public Bike()
       {
          super();
          numGears = 0;
       }//end super constructor
     
       public Bike(String make, String model, int numWheels, int numGears)
       {
          super(make, model, numWheels);
          this.numGears = numGears;
       }
     
       public int getNumGears()
       {
          return numGears;
       }//end getNumGears()
     
       public String toString()
       {
          String str = "";
          str += super.toString();
          str += "\nNumber Of Gears: " + numGears;
          return str;
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Reading in data from a text file and adding to ArrayList

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:658)
    at VehicleDriver.inputVehicles(VehicleDriver.java:18)
    At line 18 the code uses a index value in the charAt() method that is past the end of the String. Since the index is 0, the String must be empty. The code needs to test that the String's length is long enough.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    My Mood
    Bitchy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading in data from a text file and adding to ArrayList

    I'm honestly pretty lost and I don't know how to do that.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Reading in data from a text file and adding to ArrayList

    Did you look at line 18? What was there? What was in the String variable that was used on that line?

    See the API doc for the String class for its length method. It will return the length of the String as an int.

    I'm honestly pretty lost
    How much of the posted code did you write? There is so much code I would assume that you know something about writing a java program. If you got the code from someone else, then my assumptions about your knowledge would be wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Reading in data from a text file and adding to ArrayList

    look at this statement in your code in VehiclDriver program:

    File infile = new File("vehicles.txt");
    Scanner scan = new Scanner(infile);
    String x = "";
    char c = x.charAt(0);

    you are trying to access the first character in your String x, but actually String x is empty. that is why it throws an Exception of Array index out of bounds. there is no element in your String x so how could you get a character from it?

  6. #6
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    My Mood
    Bitchy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading in data from a text file and adding to ArrayList

    I reassigned String x which was empty to char c = x.CharAt(0). String x was empty on line 17, when I first made it.

    I'm not even asking for the length though? I don't see how that's relevant.

    We had written(my classmates and our instructor) had written everything aside from the Bike class, and we had written a different vehicleDriver.

    We have just gotten into Arrays and ArrayLists about a week ago. And this is our first time adding to an ArrayList.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Reading in data from a text file and adding to ArrayList

    The error has nothing to do with ArrayLists. The code tried to get a char from an empty String.

    What is the new code for lines for the area where the error happened?

    I don't see how that's relevant.
    Because the error was for using an index that was past the end of the String. Using the length method to test the String's length before trying to index into it can prevent that error from happening. If there are 2 char in a String, trying to get the 3rd char will cause an error.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    My Mood
    Bitchy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading in data from a text file and adding to ArrayList

    I honestly don't know. I figured that was creating a char called c, and then assigning it to x.charAt(0). I had made x empty because I don't know what else to set it to. I understand what you're saying with why is throws and exception of array index out of bounds. But I don't know how to fix that. I need to start reading the from the file find that the first letter is either 'R' or 'B' or 'C'. Then read a certain amount of lines after that depending on which I read in.

    I'd like to think that i'm somewhat close to a solution, but I have honestly no idea. I've never done anything like this before, and I have nothing I can compare it too.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Reading in data from a text file and adding to ArrayList

    If the String is empty, there is no char at index 0.

    You need to read a value from the user into the String BEFORE trying to get a char from the String.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    My Mood
    Bitchy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading in data from a text file and adding to ArrayList

    I honestly have no clue. Thanks for trying to help, but the headache isn't worth it. I'll turn the assignment in as it is.

    I'll make this as solved.

    Thanks you.

Similar Threads

  1. [SOLVED] Help - reading from a text file into an ArrayList
    By deeevo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 19th, 2013, 07:47 AM
  2. Reading into an ArrayList from a text file
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2013, 04:32 PM
  3. Reading from a text file into an ArrayList
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 17th, 2013, 01:24 AM
  4. Reading a text file into an arraylist of fueldispensers.
    By vbhatti in forum Object Oriented Programming
    Replies: 3
    Last Post: November 20th, 2011, 12:17 PM
  5. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM