Reputation: 39
This program is meant to prompt a user for address information. Identify the address that would come first by zip code, and then print that address.
I'm having a couple of issues. When I try to assign an int
value to the apartment variable, I get an error. Same thing with the zip code portion. Once the minimum value is found, then I want to get the index of the minimum value so that I can print the same index value of each arraylist
.
Can someone point me in the right direction or give me a good reference for this? I think I'm just confusing a few things.
package newchapter7;
import java.util.*;
/**
*
* @author Crazy
*/
public class Address {
ArrayList<Integer> houses = new ArrayList<>();
ArrayList<String> streets = new ArrayList<>();
ArrayList<Integer> apts = new ArrayList<>();
ArrayList<String> cities = new ArrayList<>();
ArrayList<String> states = new ArrayList<>();
ArrayList<Integer> zips = new ArrayList<>();
int minValue;
/**
* Adds a house number to the address
* @param house house number
*/
public void addHouse(int house)
{
houses.add(house);
}
public ArrayList<Integer> getHouse()
{
return houses;
}
/**
* Adds a street name to the address
* @param street street name
*/
public void addStreet(String street)
{
streets.add(street);
}
public ArrayList<String> getStreet()
{
return streets;
}
/**
* constructor to add an apartment number that equals 0
*/
public void addApt()
{
}
/**
* Adds an apartment number to the address
* @param aptNbr apartment number
*/
public void addApt(int aptNbr)
{
apts.add(aptNbr);
}
public ArrayList<Integer> getAptNbrs()
{
return apts;
}
/**
* Adds a city to the address
* @param city city
*/
public void addCity(String city)
{
cities.add(city);
}
public ArrayList<String> getCity()
{
return cities;
}
/**
* Adds a state to the address
* @param state state
*/
public void addState(String state)
{
states.add(state);
}
public ArrayList<String> getState()
{
return states;
}
/**
* Adds a zip code to the address
* @param zip zip code
*/
public void addZip(int zip)
{
zips.add(zip);
}
public ArrayList<Integer> getZip()
{
return zips;
}
public int arrValue()
{
minValue = zips.indexOf(Collections.min(zips));
return minValue;
}
}
Main
package newchapter7;
import java.util.*;
public class NewChapter7 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Address addy = new Address();
for (int i = 0; i < 3; i++)
{
System.out.print("Please enter a house number: ");
int house1 = in.nextInt();
addy.houses.add(house1);
System.out.print("Please enter the street name: ");
String street1 = in.next();
addy.streets.add(street1);
System.out.print("Please enter an apartment number if applicable: ");
int apartment;
apartment = in.nextInt();
addy.apts.add(apartment);
if (apartment != 0)
{
addy.apts.add(apartment);
}
else
{
addy.apts.add(0);
}
System.out.print("Please enter the city name: ");
String city2 = in.nextLine();
addy.cities.add(city2);
System.out.print("Please enter the state name: ");
String states2 = in.nextLine();
addy.states.add(states2);
System.out.print("Please enter the zip code: ");
int zipC = in.nextInt();
addy.zips.add(zipC);
}
}
}
This is a homework assignment. I think I've confused the material quite a bit and even if this works, I would like to learn a more efficient way to accomplish the same task.
Error
Please enter a house number: 772
Please enter the street name: Apple Drive
Exception in thread "main" java.util.InputMismatchException
Please enter an apartment number if applicable:
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at newchapter7.NewChapter7.main(NewChapter7.java:32)
Java Result: 1
BUILD SUCCESSFUL (total time: 21 seconds)
Assignment:
Implement a class Address. An address has a house number, a street, an optional apartment number, a city, a state, and a postal code. Supply two constructors: one with an apartment number and one without. Supply a print method that prints the address with the street on one line and the city, state, and postal code on the next line. Supply a method public boolean comesBefore (Address other) that tests whether this address comes before another when the addresses are compared by postal code.
Upvotes: 2
Views: 860
Reputation: 5772
change
String street1 = in.next();
addy.streets.add(street1);
line to
in.nextLine();
String street1 = in.nextLine();
addy.streets.add(street1);
and other next()
to nextLine()
also.
Ok the suggestion above is not working this is the full working code, but you have a possible logical error in this. Since it is homework you should be able to solve that. However...
for (int i = 0; i < 3; i++)
{
System.out.print("Please enter a house number: ");
int house1 = in.nextInt();
addy.houses.add(house1);
System.out.print("Please enter the street name: ");
in.nextLine();
String street1 = in.nextLine();
addy.streets.add(street1);
System.out.print("Please enter an apartment number if applicable: ");
int apartment;
apartment = in.nextInt();
addy.apts.add(apartment);
if (apartment != 0)
{
addy.apts.add(apartment);
}
else
{
addy.apts.add(0);
}
System.out.print("Please enter the city name: ");
in.nextLine();
String city2 = in.nextLine();
addy.cities.add(city2);
System.out.print("Please enter the state name: ");
String states2 = in.nextLine();
addy.states.add(states2);
System.out.print("Please enter the zip code: ");
int zipC = in.nextInt();
addy.zips.add(zipC);
}
Upvotes: -1
Reputation: 285440
Your Address class is mixed up and actually seems backwards to me. I think that you'd be much better off if it represented the state of a single address. If so, then it should not hold ArrayLists, but rather individual fields for a single house with getter and setter methods for each field, a constructor that accepts the field parameters, and perhaps a default constructor that accepts no parameters if desired.
Then if you need to work with many Addresses, you can create a single ArrayList<Address>
for this.
Note that as an aside, I wouldn't use int for apartment number or zip code. While these look like numbers and comprise the digits of numbers, they don't behave as numbers. Use String instead.
Upvotes: 3