In: Computer Science
I keep getting this error,
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for
length 0
at
simpleInheritance/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, zip
code,
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 = new
File(args[0]);
BufferedReader
buffReader = new BufferedReader(new FileReader(file));
System.out.print("Print output 1");
ArrayList
<Dwelling> 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(Dwelling
dwl: listOfLines) {
/*
Prints data in the console
*/
System.out.println(dwl);
}
FileWriter write
= new FileWriter(file);
for(Dwelling
str: listOfLines) {
/*
Writes data into the file
*/
write.write(str + System.lineSeparator());
}
write.close();
}
}
}
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, zip code,
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 class DriverTest{
/*
command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
if(args==null || args.length==0) {
System.out.println("ERROR: Please pass the command line arguments to run the program successfully");
return;
}
File file = new File(args[0]);
BufferedReader buffReader = new BufferedReader(new FileReader(file));
System.out.print("Print output 1");
ArrayList <Dwelling> 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(Dwelling dwl: listOfLines) {
/*
Prints data in the console
*/
System.out.println(dwl);
}
FileWriter write = new FileWriter(file);
for(Dwelling str: listOfLines) {
/*
Writes data into the file
*/
write.write(str + System.lineSeparator());
}
write.close();
}
}
Here the issue is you need to pass the name of the file from the command line arguments than only program works.. Otherwise it will give an error. Please pass the properr file name it will work
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me