Reputation: 35
I've searched through a few different posts but haven't found the exact issue I am having. I feel like what I am missing should be very obvious but for some reason can't quite pin it down have been playing with everything but cant quite get it. If anyone can give me a hand it would be greatly appreciated. The program is meant to pull house information from a text file and output it. the JMUnit7 file was given to us by the instructor. The JMUnit7House file is my work. Thanks in advance.
package jmunit7;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
/**
*
* @author Xephus
*/
public class JMUnit7 {
/**
* @param args the command line arguments
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception
{
Scanner stdIn = new Scanner(System.in);
Scanner stdInFile = new Scanner(new
File("JMUnit7House.txt"));
JMUnit7House house1, house2; //New houses
//Create house 1 using default constructor
house1 = new JMUnit7House();
house1.print(); //print house 1 with default values
String street, city, state, zipCode;
int number;
System.out.println("Importing Number.");
number = stdInFile.nextInt();
stdInFile.nextLine();
System.out.println("Importing Street.");
street = stdInFile.nextLine();
System.out.println("Importing City.");
city = stdInFile.nextLine();
System.out.println("Importing State.");
state = stdInFile.nextLine();
System.out.println("Importing ZipCode.");
zipCode = stdInFile.nextLine();
System.out.println();
//use method call chaining to set values
//and print results for house 1
house1.setNumber(number).setStreet(street)
.setCity(city).setState(state)
.setZipCode(zipCode).print();
System.out.println("Importing Number.");
number = stdInFile.nextInt();
stdInFile.nextLine();
System.out.println("Importing Street.");
street = stdInFile.nextLine();
System.out.println("Importing City.");
city = stdInFile.nextLine();
System.out.println("Importing State.");
state = stdInFile.nextLine();
System.out.println("Importing ZipCode.");
zipCode = stdInFile.nextLine();
System.out.println();
//create house 2 using 5 parameter constructor
house2 = new JMUnit7House(number,
street, city, state, zipCode);
//print house 2
house2.print();
}
}
My code is as follows im getting and erroneous sym type error which is: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: .setCity.setState.setZipCode.print at jmunit7.JMUnit7.main(JMUnit7.java:49) 49 is the house1.setNumber line.
public class JMUnit7House {
private int number;
private String street;
private String city;
private String state;
private String zipCode;
public JMUnit7House()
{
number = 0;
street = "No Street";
city = "No City";
state= "No State";
zipCode = "No Zip Code";
}
//intializes house with arguments
public JMUnit7House(int number, String street, String city, String state, String zipCode)
{
this.number = number;
this.street = street;
this.city = city;
this.state = state;
this.zipCode = zipCode;
}
public int setNumber(int number)
{
return number;
}
public String setStreet(String street)
{
return street;
}
public String setCity(String city)
{
return city;
}
public String setState(String state)
{
return state;
}
public String setZipCode(String zipCode)
{
return zipCode;
}
//function to print
public void print()
{
String info=String.format("House: %d\n", number);
info += String.format("Street: %s\n", street);
info += String.format("City: %s\n", city);
info += String.format("State: %s\n", state);
info += String.format("Zip: %s\n", zipCode);
System.out.println(info);
}
}
Upvotes: 0
Views: 1424
Reputation: 39507
Okay I see the problem. In the below:
house1.setNumber(number).setStreet(street)
.setCity(city).setState(state)
.setZipCode(zipCode).print();
Since, setNumber()
returns a int
and you're calling setStreet()
on that, it won't work.
If you want to make calls in the above fashion return this
object instead in the required methods.
For e.g.:
public int setNumber(int number)
{
//do something
return number;
}
will become
public JMUnit7House setNumber(int number)
{
//do something
return this;
}
Upvotes: 0