In: Computer Science
Keep getting error where the code cannot read the text file and create an arraylist of objects from it.
HouseListTester:
import java.util.*;
//Hard codes the criteria
public class HouseListTester {
static HouseList availableHouses;
public static void main(String []args) {
availableHouses = new
HouseList("C:\\Users\\jvs34\\Downloads\\houses.txt");
Criteria c1 = new Criteria(1000, 500000, 100, 5000, 0, 10);
Criteria c2 = new Criteria(1000, 100000, 500, 1200, 0, 3);
Criteria c3 = new Criteria(100000, 200000, 1000, 2000, 2, 3);
Criteria c4 = new Criteria(200000, 300000, 1500, 4000, 3, 6);
Criteria c5 = new Criteria(100000, 500000, 2500, 5000, 3, 6);
System.out.println("Test 1: ");
availableHouses.printHouses(c1);
System.out.println("Test 2: ");
availableHouses.printHouses(c2);
System.out.println("Test 3: ");
availableHouses.printHouses(c3);
System.out.println("Test 4: ");
availableHouses.printHouses(c4);
System.out.println("Test 5: ");
availableHouses.printHouses(c5);
}
}
HouseList:
import java.io.*;
import java.util.*;
//Contains arraylist of House objects that comes from house.txt to
allow for easy searching
public class HouseList {
ArrayList<House> houseList = new ArrayList<>();
//Gets house.txt file for future use
public HouseList(String HouseLists) {
houseList = new ArrayList();
Scanner myFileIn = null;
try
{
myFileIn = new Scanner(new File(HouseLists));
}
catch (FileNotFoundException e)
{
System.out.println("File: "+HouseLists+" is not found");
}
//Gets house info from text file to place into arraylist
String address1;
int price1;
int area1;
int numBedrooms1;
House h1;
while(myFileIn.hasNextLine())
{
address1 = myFileIn.next();
price1 = myFileIn.nextInt();
area1 = myFileIn.nextInt();
numBedrooms1 = myFileIn.nextInt();
h1 = new House(address1, price1, area1, numBedrooms1);
houseList.add(h1);
}
}
//prints houses that meet criteria
public void printHouses(Criteria c) {
for(int i =0;i<houseList.size();i++)
{
System.out.println(houseList.get(i).toString());
}
}
//returns string of details of houses
public String getHouses(Criteria c) {
String ans = "";
for(int i = 0;i < houseList.size();i++)
{
if(houseList.get(i).satisfies(c))
{
ans = ans + houseList.get(i).toString() + "\n";
}
}
return ans;
}
}
House:
import java.util.*;
//Represents details of houses for sale
public class House {
public String address;
public int price;
public int area;
public int numberOfBedrooms;
//constructor
public House(String addr, int p, int a, int bedroom) {
address = addr;
price = p;
area = a;
numberOfBedrooms = bedroom;
}
//gets address
public String getAddress() {
return address;
}
//gets price
public int getPrice() {
return price;
}
//gets area
public int getArea() {
return area;
}
//gets rooms
public int getRoom() {
return numberOfBedrooms;
}
//Compare price, area, and rooms
public boolean satisfies(Criteria c) {
if(price < c.getMinimumPrice() || price >
c.getMaximumPrice())
{
return false;
}
if(area < c.getMinimumArea() || area >
c.getMaximumArea())
{
return false;
}
if(numberOfBedrooms < c.getMinimumNumberOfBedrooms() ||
numberOfBedrooms > c.getMaximumNumberOfBedrooms())
{
return false;
}
return true;
}
//print it out into string
public String toString() {
return (address+ " "+ price + " " + area + " " +
numberOfBedrooms);
}
}
Criteria:
import java.util.*;
//contains criteria to select houses
public class Criteria {
public int minimumPrice;
public int maximumPrice;
public int minimumArea;
public int maximumArea;
public int minimumNumberOfBedrooms;
public int maximumNumberOfBedrooms;
//constructor
public Criteria(int minPrice, int maxPrice, int minArea, int
maxArea, int minRoom, int maxRoom) {
minimumPrice = minPrice;
maximumPrice = maxPrice;
minimumArea = minArea;
maximumArea = maxArea;
minimumNumberOfBedrooms = minRoom;
maximumNumberOfBedrooms = maxRoom;
}
//returns minimum price
public double getMinimumPrice() {
return minimumPrice;
}
//returns maximum price
public double getMaximumPrice() {
return maximumPrice;
}
//returns minimum area
public double getMinimumArea() {
return minimumArea;
}
//returns maximum area
public double getMaximumArea() {
return maximumArea;
}
//returns minimum bedrooms
public double getMinimumNumberOfBedrooms() {
return minimumNumberOfBedrooms;
}
//returns maximum bedrooms
public double getMaximumNumberOfBedrooms() {
return maximumNumberOfBedrooms;
}
}
Text file:
123-canal-street 129000 1800 3
124-main-street 89000 1600 3
125-college-street 199000 2000 4
126-lincoln-street 56000 1200 2
127-state-street 82000 1500 3
223-canal-street 385000 4500 5
224-main-street 40000 800 2
225-college-street 37999 800 2
226-lincoln-street 125000 1200 2
227-state-street 130000 1250 3
323-canal-street 60000 900 2
324-main-street 80000 1000 2
325-college-street 45000 800 1
326-lincoln-street 63000 900 1
327-state-street 145000 1400 3
423-canal-street 199999 2000 4
424-main-street 250000 3500 5
425-college-street 350000 4600 6
426-lincoln-street 133000 1300 2
427-state-street 68000 850 1
523-canal-street 299999 3000 4
524-main-street 260000 2500 6
525-college-street 359000 4900 4
526-lincoln-street 233000 1900 2
527-state-street 58000 750 1
It would be much appreciated to get some help on this.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
//Hard codes the criteria
public class HouseListTester {
static HouseList availableHouses;
public static void main(String[] args) {
availableHouses = new
HouseList("houses.txt");
Criteria c1 = new Criteria(1000,
500000, 100, 5000, 0, 10);
Criteria c2 = new Criteria(1000,
100000, 500, 1200, 0, 3);
Criteria c3 = new Criteria(100000,
200000, 1000, 2000, 2, 3);
Criteria c4 = new Criteria(200000,
300000, 1500, 4000, 3, 6);
Criteria c5 = new Criteria(100000,
500000, 2500, 5000, 3, 6);
System.out.println("Test 1:
");
availableHouses.printHouses(c1);
System.out.println("Test 2:
");
availableHouses.printHouses(c2);
System.out.println("Test 3:
");
availableHouses.printHouses(c3);
System.out.println("Test 4:
");
availableHouses.printHouses(c4);
System.out.println("Test 5:
");
availableHouses.printHouses(c5);
}
}
//Contains arraylist of House objects that comes from house.txt
to allow for easy searching
class HouseList {
ArrayList<House> houseList = new ArrayList<>();
//Gets house.txt file for future use
public HouseList(String HouseLists) {
houseList = new ArrayList();
Scanner myFileIn = null;
try {
myFileIn =
new Scanner(new File(HouseLists));
} catch (FileNotFoundException e)
{
System.out.println("File: " + HouseLists + " is not found");
}
//Gets house info from text file to place into arraylist
String address1;
int price1;
int area1;
int numBedrooms1;
House h1;
while (myFileIn.hasNextLine())
{
address1 =
myFileIn.next();
price1 =
myFileIn.nextInt();
area1 =
myFileIn.nextInt();
numBedrooms1 =
myFileIn.nextInt();
h1 = new
House(address1, price1, area1, numBedrooms1);
houseList.add(h1);
}
}
//prints houses that meet criteria
public void printHouses(Criteria c) {
for (int i = 0; i <
houseList.size(); i++) {
System.out.println(houseList.get(i).toString());
}
}
//returns string of details of houses
public String getHouses(Criteria c) {
String ans = "";
for (int i = 0; i <
houseList.size(); i++) {
if
(houseList.get(i).satisfies(c)) {
ans = ans + houseList.get(i).toString() +
"\n";
}
}
return ans;
}
}
//Represents details of houses for sale
class House {
public String address;
public int price;
public int area;
public int numberOfBedrooms;
//constructor
public House(String addr, int p, int a, int bedroom)
{
address = addr;
price = p;
area = a;
numberOfBedrooms = bedroom;
}
//gets address
public String getAddress() {
return address;
}
//gets price
public int getPrice() {
return price;
}
//gets area
public int getArea() {
return area;
}
//gets rooms
public int getRoom() {
return numberOfBedrooms;
}
//Compare price, area, and rooms
public boolean satisfies(Criteria c) {
if (price < c.getMinimumPrice()
|| price > c.getMaximumPrice()) {
return
false;
}
if (area < c.getMinimumArea() ||
area > c.getMaximumArea()) {
return
false;
}
if (numberOfBedrooms <
c.getMinimumNumberOfBedrooms() || numberOfBedrooms >
c.getMaximumNumberOfBedrooms()) {
return
false;
}
return true;
}
//print it out into string
public String toString() {
return (address + " " + price + " "
+ area + " " + numberOfBedrooms);
}
}
class Criteria {
public int minimumPrice;
public int maximumPrice;
public int minimumArea;
public int maximumArea;
public int minimumNumberOfBedrooms;
public int maximumNumberOfBedrooms;
//constructor
public Criteria(int minPrice, int maxPrice, int
minArea, int maxArea, int minRoom, int maxRoom) {
minimumPrice = minPrice;
maximumPrice = maxPrice;
minimumArea = minArea;
maximumArea = maxArea;
minimumNumberOfBedrooms =
minRoom;
maximumNumberOfBedrooms =
maxRoom;
}
//returns minimum price
public double getMinimumPrice() {
return minimumPrice;
}
//returns maximum price
public double getMaximumPrice() {
return maximumPrice;
}
//returns minimum area
public double getMinimumArea() {
return minimumArea;
}
//returns maximum area
public double getMaximumArea() {
return maximumArea;
}
//returns minimum bedrooms
public double getMinimumNumberOfBedrooms() {
return
minimumNumberOfBedrooms;
}
//returns maximum bedrooms
public double getMaximumNumberOfBedrooms() {
return
maximumNumberOfBedrooms;
}
}
Please create the houses.txt file in the project folder
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