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

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)...
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
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
I keep getting this error "LetterDemo.cs(21,14): error CS1519: Unexpected symbol `string' in class, struct, or interface...
I keep getting this error "LetterDemo.cs(21,14): error CS1519: Unexpected symbol `string' in class, struct, or interface member declaration" Can someone please help me. Here is my code: using static System.Console; class LetterDemo {    static void Main()    {      Letter letter1 = new Letter();      CertifiedLetter letter2 = new CertifiedLetter();      letter1.Name = "Electric Company";      letter1.Date = "02/14/18";      letter2.Name = "Howe and Morris, LLC";      letter2.Date = "04/01/2019";      letter2.TrackingNumber = "i2YD45";      WriteLine(letter1.ToString());      WriteLine(letter2.ToString() +       " Tracking number: " + letter2.TrackingNumber);    } } class Letter {...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT