Integer和ArrayList问题

时间:2013-11-28 16:44:30

标签: java integer

此程序旨在提示用户提供地址信息。确定邮政编码首先出现的地址,然后打印该地址。

我有几个问题。当我尝试为公寓变量分配int值时,出现错误。与邮政编码部分相同。找到最小值后,我想得到最小值的索引,以便我可以打印每个arraylist的相同索引值。

有人能指出我正确的方向还是给我一个很好的参考?我想我只是混淆了一些事情。

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;
    }
}

主要

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);
    }


}

}

这是一项家庭作业。我想我已经把这些材料弄得很混乱了,即使这样有效,我也想学习一种更有效的方法来完成同样的任务。

错误

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) 

作业:

实施类地址。地址有门牌号码,街道,可选的公寓号码,城市,州和邮政编码。供应两个施工人员:一个有公寓号,一个没有。提供一种打印方法,用于在一行上打印街道地址,在下一行打印城市,州和邮政编码。提供一个方法public boolean comesBefore(Address other),用于通过邮政编码比较地址时测试此地址是否在另一个地址之前。

2 个答案:

答案 0 :(得分:3)

你的地址课程混淆了,实际上似乎倒退了。如果它代表单个地址的状态,我认为你会好得多。如果是这样,那么它不应该包含ArrayLists,而是包含单个房屋的单个字段,每个字段具有getter和setter方法,接受字段参数的构造函数,以及可能的默认构造函数,如果需要,不接受任何参数。

然后,如果您需要使用多个地址,则可以为此创建一个ArrayList<Address>

请注意,除此之外,我不会将int用于公寓号码或邮政编码。虽然这些看起来像数字并且包含数字的数字,但它们并不像数字那样。改为使用String。

答案 1 :(得分:-1)

更改

    String street1 = in.next();
    addy.streets.add(street1);

行到

    in.nextLine();
    String street1 = in.nextLine();
    addy.streets.add(street1);

和其他next()nextLine()

好的上面的建议不起作用这是完整的工作代码,但是你有一个可能的逻辑错误。既然是家庭作业,你应该能够解决这个问题。然而...

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);
}