Question

In: Computer Science

Keep getting error where the code cannot read the text file and create an arraylist of...

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.

Solutions

Expert Solution

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


Related Solutions

so i want to read in a file. just to keep it simple, the text file...
so i want to read in a file. just to keep it simple, the text file has student name and the grade, separated by a space. let's say there are 192 student's name and I want to read them in and made them into a Student /object class, which has a constructor, student(string name, int grade). individually creating them seems pain in the ass. reading in should work for any number of names. So how do I do this in...
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is...
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is given by the input parameter file_path There are 4 columns in the text dataset that are separated by colons ":". c1:c2:c3:c4 Plot 3 datasets. (x axis vs y axis) c1 vs c2 (Legend label "n=1") c1 vs c3 (Legend label "n=1") c1 vs c4 (Legend label "n=1") Make sure you have proper x and y labels and a title. The x label should be...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at Main.main(Main.java:34) Your output Welcome to the food festival! Would you like to place an order? Expected output This test case should produce no output in java import java.util.Scanner; public class Main {    public static void display(String menu[])    {        for(int i=0; i<menu.length; i++)        {            System.out.println (i + " - " + menu[i]);...
I keep getting an error that I cannot figure out with the below VS2019 windows forms...
I keep getting an error that I cannot figure out with the below VS2019 windows forms .net framework windows forms error CS0029 C# Cannot implicitly convert type 'bool' to 'string' It appears to be this that is causing the issue string id; while (id = sr.ReadLine() != null) using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; namespace Dropbox13 { public partial class SearchForm : Form { private List allStudent = new List(); public SearchForm() { InitializeComponent(); } private void SearchForm_Load(object...
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint....
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'test_ibfk_5' in the referenced table 'appointment', can you please tell me what is wrong with my code: -- Table III: Appointment = (site_name [fk7], date, time) -- fk7: site_name -> Site.site_name DROP TABLE IF EXISTS appointment; CREATE TABLE appointment (    appt_site VARCHAR(100) NOT NULL, appt_date DATE NOT NULL, appt_time TIME NOT NULL, PRIMARY KEY (appt_date, appt_time), FOREIGN KEY (appt_site)...
Please fix this python script, I keep getting a return outside function error and cannot get...
Please fix this python script, I keep getting a return outside function error and cannot get it to run: def caesar(plainText, shift):     cipherText = "" for char in plainText:     newChar = ord(char) + shift     if newChar > 128:       newChar = newChar % 128     finalChar = chr(newChar)     cipherText += finalChar return cipherText text = input("Enter a message: "); s = input("Enter the distance value: "); print (caesar(text, int(s)));
Need C++ code to be able to run, keep getting a constant value error #include #include...
Need C++ code to be able to run, keep getting a constant value error #include #include #include #include #include #include using namespace std; using namespace std::chrono; int c; void insertionSort(int* arr, int n) { for (int i = 1;i < n;i++) { int v = arr[i]; int j; for (j = i - 1;j > -1;j--) { c++; if (arr[j] > v) { arr[j + 1] = arr[j]; } else { break; } } arr[j + 1] = v; }...
HI. I have been trying to run my code but I keep getting the following error....
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method. Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at project2.areacalculation.main(areacalculation.java:26) My code is below package project2; import java.util.Scanner; public class areacalculation { private static...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
Whats the code to open a text file and every line in that text file that...
Whats the code to open a text file and every line in that text file that starts with # then it should delete that line In python using .strip
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT