I keep getting this error, Exception in thread \"main\" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0   ...

50.1K

Verified Solution

Question

Programming

I keep getting this error,

Exception in thread \"main\"java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds forlength 0
   atsimpleInheritance/simpInher.Dwelling$DriverTest.main(Dwelling.java:146)

Can someone help me fix it?

import java.io.BufferedReader;
   import java.io.FileNotFoundException;
   import java.io.FileReader;
   import java.util.*;
   import java.io.*;
   import java.io.FileWriter;
   import java.io.IOException;

   class Dwelling {
   /*
   Declaring Variables
   */
   String streetAddress;
   String city;
   String state;
   String zipCode;
   int bedrooms;
   double bathrooms;
  
   /*
       Getters and Setters for
       street address, city, state, zipcode,
       bedrooms, and bathrooms.
   */  
  
   public String getStreetAddress() {
       return streetAddress;
   }
  
   public void setStreetAddress(String streetAddress){
       this.streetAddress =streetAddress;
   }
  
   public String getCity() {
       return city;
   }
  
   public void setCity(String city) {
       this.city = city;
   }
  
   public String getState() {
       return state;
   }
  
   public void setState(String state) {
       this.state = state;
   }
  
   public String getZip() {
       return zipCode;
   }
  
   public void setZip(String zipCode) {
       this.zipCode = zipCode;
   }
  
   public int getBedrooms() {
       return bedrooms;
   }
  
   public void setBedrooms(int bedrooms) {
       this.bedrooms = bedrooms;
   }
  
   public double getBathrooms() {
       return bathrooms;
   }
  
   public void setBathrooms(double bathrooms) {
       this.bathrooms = bathrooms;
   }
  
   /*
   no arg constructor
   no parameters
   */
  
   public Dwelling() {
       /*
       Declaring Variables
       */
      
       streetAddress = \"\";
       city = \"\";
       state = \"\";
       zipCode = \"\";
       bedrooms = 0;
       bathrooms = 0.0;
   }
  
   public String toString() {
       return streetAddress + \"|\" + city +\"|\" + state +\"|\" +
              zipCode + \"|\" + bedrooms + \"|\" +bathrooms;
   }

   class House extends Dwelling{
       /*
       Declaring Variables
       */
       double acreage;
       int garageSize;
       /*
       no arg constructor
       no parameters
       */
       public House() {
           super();
           acreage =0.0;
           garageSize =0;
       }
       public String toString() {
           return(super.toString()+ \"|\" + acreage + \"|\" + garageSize);
       }
   }
  
   class Apartment extends Dwelling{
       /*
       Declaring Variables
       */
       String aptNum;
       boolean laundry;
      
       /*
       no arg constructor
       no parameters
       */
       public Apartment() {
           super();
           aptNum =\"\";
           laundry =false;
       }
       public String toString() {
           return(super.toString() + \"|\" + aptNum + \"|\" + laundry);
       }
   }
  
   public static class DriverTest{
       /*
       command line arguments
       */
      
       public static void main(String[]args) throws FileNotFoundException, IOException {
           File file = newFile(args[0]);
           BufferedReaderbuffReader = new BufferedReader(new FileReader(file));
          
          System.out.print(\"Print output 1\");
           ArrayList listOfLines = new ArrayList <>();
          
           String line =buffReader.readLine();
          
           Dwelling d1[] =new Dwelling[5];
           int i = 0;
          
           while (line !=null && i<5) {
              d1[i] = new Dwelling();
              d1[i].setStreetAddress(line);
              line = buffReader.readLine();
              d1[i].setCity(line);
              line = buffReader.readLine();
              d1[i].setState(line);
              line = buffReader.readLine();
              d1[i].setZip(line);
              line = buffReader.readLine();
              int bed = Integer.parseInt(line);
              d1[i].setBedrooms(bed);
              line = buffReader.readLine();
             d1[i].setBathrooms(Double.parseDouble(line));
              listOfLines.add(d1[i]);
              i++;
             
              line = buffReader.readLine();
              line = buffReader.readLine();
              line = buffReader.readLine();
              line = buffReader.readLine();
           }
          buffReader.close();
          
           for(Dwellingdwl: listOfLines) {
              /*
              Prints data in the console
              */
              System.out.println(dwl);
           }
           FileWriter write= new FileWriter(file);
          
           for(Dwellingstr: listOfLines) {
              /*
              Writes data into the file
              */
              write.write(str + System.lineSeparator());
           }
          write.close();
       }
  
  
  
   }
  
}

Answer & Explanation Solved by verified expert
4.4 Ratings (827 Votes)
import javaioBufferedReader import javaioFileNotFoundException import javaioFileReader import javautil import javaio import javaioFileWriter import javaioIOException class Dwelling Declaring Variables String streetAddress String city String state String zipCode int bedrooms double bathrooms Getters and Setters for street address city state zip code bedrooms and bathrooms public String getStreetAddress return    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students