Question

Having error problem with my code. HouseListTester: import java.util.*; //Hard codes the criteria public class HouseListTester...

Having error problem with my code.

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 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("C:\\Users\\jvs34\\Downloads\\houses.txt"));
}
catch (FileNotFoundException e)
{
System.out.println("File: +((C:\\Users\\jvs34\\Downloads\\houses.txt))+ 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 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);
}
}

I cannot get it to read from this text file and make an array list from it:

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

If you could help me figure out how to fix the code to read the file and make an arraylist it would be greatly appreciated.

Homework Answers

Answer #1

Short Summary:

  • Have solved the errors and attached the updated code for HouseList.java (only this class had errors)
  • Attached sample output after the errors are gone.
  • Note: Criteria class was not attached in the question, hence I created and used it.

**************Please do upvote to appreciate our time. Thank you!******************

Source Code:

HouseList.java

package com.core.java;
import java.io.*;
import java.util.*;
//Contains arraylist of House objects that comes from house.txt to allow for easy searching
public class HouseList {
   //Added type as <House> to solve the error in line no 50.
   ArrayList<House> houseList = new ArrayList<>();
   //Gets house.txt file for future use
   public HouseList(String HouseLists) {
       houseList = new ArrayList();
       Scanner myFileIn = null;
       try
       {
           //Added the argument as path argument to the File oject
           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
   //Modified the for loop to clear the compilation errors
   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;
   }
}

Code Screenshot:

Error/Fix details:

Output:

Test 1:
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
Test 2:
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
Test 3:
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
Test 4:
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
Test 5:
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

**************Please do upvote to appreciate our time. Thank you!******************

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static void readFromFile() throws IOException { BufferedReader bufReader = new BufferedReader(new FileReader("processes.txt")); ArrayList<String> listOfLines = new ArrayList<>(); String line = bufReader.readLine(); while (line != null) { listOfLines.add(line); line = bufReader.readLine(); } bufReader.close(); System.out.println("Content of ArrayLiList:"); // split by new line int num = 0; for (String line1 : listOfLines) { String line2[] = line1.split(","); // int burstTime = Integer.parseInt(line2[3].trim()); // String retrival = listOfLines.get(0); System.out.println("...
<<<<<<<< I need only the UML diagram for ALL classes.Java???????????? public class House {    private...
<<<<<<<< I need only the UML diagram for ALL classes.Java???????????? public class House {    private int houseNumber;    private int bedrooms;    private int sqFeet;    private int year;    private int cost;    public House(int houseNumber,int bedrooms,int sqFeet, int year, int cost)    {        this.houseNumber = houseNumber;        this.bedrooms = bedrooms;        this.sqFeet = sqFeet;        this.year = year;        this.cost = cost;    }    public int getHouseNumber()    {        return houseNumber;    }   ...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
do a code trace on how a key gets deleted package interview; import java.util.*; public class...
do a code trace on how a key gets deleted package interview; import java.util.*; public class binarySearchTree { Node root; void insert(int key) { root = insertRec(root,key); } void delete(int key) { root = deleteRec(root,key); } Node insertRec(Node root, int key) { if(root == null) { root = new Node(key); return root; } if(key < root.key) { root.leftChild = insertRec(root.leftChild,key); } else if(key > root.key){ root.rightChild = insertRec(root.rightChild,key); } return root; } Node deleteRec(Node root, int key) { if(root ==...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...