0

So this is what I need to do. I have a specific file with a specific format to it: https://pastebin.com/rL3jxZAF. The 4 at the beginning is the number of houses. I need to read this file in a later assignment, but in this assignment I have to say how it needs to read it. It needs to read whether the house is for rent, for sale, rented or sold. It needs to have an address(which consists of a street, number, zipcode and city). It needs to see the sale price and what kind of heating system it has.

So I am stuck here: When I scan the first line including if the house is sold or rented etc. I don't know how to do this, because it needs to be a boolean and because sold is one word and for sale is 2 words, so I don't know how to show java that there is a difference between them. I need to scan it as a string and do somethings with it to give the constructor a boolean and I don't know how to do this all.

Second problem

Obviously java can't read objects, so I need to say that this object is a string and than later need to convert it to an object. This is not a big deal but the problem is that I don't know how to implement them into the constructor. I am very stressed right now, so if my explanations didn't do any justice, here is the assignment. I also don't know what do with the owneroccupied and rentalhouse class. What do I need to do in them. I am totally confused. Please help. Here is the assignment: https://i.stack.imgur.com/7IPlL.jpg And here is the code I have. Like you can see many things are in comments because they don't work:

public static House read(Scanner sc) throws IOException {
    //BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
    //String statuss =buffer.readLine();
    Scanner ss = new Scanner(System.in).useDelimiter("\\s");
    //String statuss = sc.next();

    //  boolean status = statuss.equals("FOR RENT") | statuss.equals("FOR SALE");

    Address address = Address.read(sc);

    int nRooms = sc.nextInt();
    sc.next();
    sc.next();

    int salePrice = sc.nextInt();

    String heatingsystemstring = sc.next();

    if (heatingsystemstring.equals("boiler")) {
        HeatingSystem heatingsystem = new Boiler();
    }
    if (heatingsystemstring.equals("Centralheating")) {
        HeatingSystem heatingsystem = new CentralHeating();
    }

    if (statuss.equals("FOR RENT") | statuss.equals("RENTED")) {
        if ()
            RentalHouse house = new RentalHouse(statuss, address, nRooms, salePrice, HeatingSystem);
    }

    if (statuss.equals("FOR SALE") | statuss.equals("SOLD")) {
        OwnerOccupiedHouse house = new OwnerOccupiedHouse(statuss, address, nRooms, salePrice, heatingsystem);
    }
}
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
  • You explain very detailed what you are trying to achieve in a whole. However for the problem this does not seem relevant. Can you reproduce the problem with just `< 10` lines or so? Can you, for example, shrink it down to *"how to read with a String with Scanner?"*. Also, please exactly explain **why** your current code fails. You just say that it does not work. ([mcve]) – Zabuzard Oct 18 '17 at 12:45
  • Its really hard to understand where you're exact problem is. You say you don't know how to differentiate between "SOLD" and "FOR SALE", but you obviously already do know how to check if a string is equal to a sepecific value as you already used that plenty of times in your code. So what exactly is stopping you from checking if the string is "SOLD" and handle that case and the same for "FOR SALE"? – OH GOD SPIDERS Oct 18 '17 at 12:55
  • Are you adamant on extracting one word at a time, or are you OK with extracting line-by-line? – Robo Mop Oct 18 '17 at 13:13
  • @CoffeehouseCoder, it doesnt matter – lederhoser Oct 18 '17 at 13:31
  • @OHGODSPIDERS the thing i need to do: create a owneroccupiedhouse and a rental house. I have to determine by the line if the house is a rental, or owneroccupied, and add it to that set of houses. My constructor its first argument is a boolean, its true if its not sold or rented yet. I dont know how to do both – lederhoser Oct 18 '17 at 13:34
  • For heatingsystem: i need to read it as a string, need to convert it back to an object, and put it as an object into the constructor, but it says this: string cannot be converted to a heatingsystem. What i did was: HeatingSystem heatingsystem = heatingsystemstring; – lederhoser Oct 18 '17 at 13:36
  • @lederhoser: Still, i don't see your problem. You know that `equals("FOR RENT")` returns a boolean if the string is "FOR RENT" and therefor the house is for rent. Just use that and pass it to the constructor. As for the HeatingSystem: You already created the heating system by the looks of it. This is just a problem of variable scope as you define those variables inside your if block. Just move the variable declaration (just declaration, not the assignment) out of that block and pass it into the constructor. – OH GOD SPIDERS Oct 18 '17 at 13:40
  • which part do you mean with assignment, and which part with declaration? By the way, i think i fixed the for rent part now. @OHGODSPIDERS – lederhoser Oct 18 '17 at 13:45
  • `HeatingSystem heatingsystem;` is a variable declaration `heatingsystem = new Boiler();` is assigning the variable. `HeatingSystem heatingsystem = new Boiler();` is declaration and assignment combined. Just put `HeatingSystem heatingsystem = null;` before your `if(heatingsystemstring...` statement and inside the if just do `heatingsystem = new Boiler();` (For the boiler case of course) to reassign them. Then you will have access to the variable `heatingsystem` outside your if statements and can pass them into the constructor. – OH GOD SPIDERS Oct 18 '17 at 13:54
  • Re variable declaration/initialization and assignment - see also: https://stackoverflow.com/questions/2614072/java-define-terms-initialization-declaration-and-assignment – OH GOD SPIDERS Oct 18 '17 at 13:54
  • @OHGODSPIDERS So now, if i do the same for centralheating, will it read and know that if for example house 1 has a centralheatingsystem ,that it has a centralheatingsystem? – lederhoser Oct 18 '17 at 15:00

0 Answers0