简体   繁体   English

Java-从名称和地址的ArrayList返回“地址”

[英]Java - Returning an “address” from an ArrayList of names and addresses

I've been working on this AP Problem all day with no luck. 我整天都在解决这个AP问题,但是没有运气。 If anyone can help, it would be appreciated. 如果有人可以提供帮助,将不胜感激。

I have an ArrayList of Strings composed of names and addresses. 我有一个由名称和地址组成的Strings ArrayList After the addresses, there is an empty String and the next name starts. 在地址之后,有一个空String ,并且下一个名字开始。 The method getAddress takes a String parameter (a name) and returns the address of the pereson (the lines after the name including the empty String, but stopping there). 方法getAddress带有String参数(名称),并返回pereson的地址(名称后的行包括空String,但在此处停止)。 I'm having trouble writing this method. 我在编写此方法时遇到麻烦。

import java.util.ArrayList;

public class Recipients {
    ArrayList<String> lines = new ArrayList<String>();
    public String extractCity(String cityZip) {
        int pos = cityZip.indexOf(",");
        return cityZip.substring(0, pos);
    }
    public void printNames() {
        System.out.println(lines.get(0));
        for (int i = 0; i < lines.size()-1; i++) 
            if (lines.get(i).substring(0, lines.get(i).length()).equals("")) 
                System.out.println(lines.get(i+1));
    }
    public String getAddress(String name) {
        String address = "";
        int ct = 0;
        int place = 0;
        for (int i = 0; i < lines.size(); i++) {
            if (lines.get(i).substring(0, lines.get(i).length()).equals(name)) {
                place = i;
                for (int j = i+1; j < lines.size(); j++) {
                    ct = j;
                    if (!lines.get(i).substring(0, lines.get(i).length()).equals("")) {
                        ct++;
                    }
                }
                for (int k = place; k < ct; k++) {
                    address = address + lines.get(i);
                }
            }
        }

        return address;
    }
        public void main() {
            lines.add("Mr. J Adams");
            lines.add("Rose St.");
            lines.add("Ithaca, NY 14850");
            lines.add("");
            lines.add("Jack S. Smith");
            lines.add("12 Posy Way");
            lines.add("Suite #201");
            lines.add("Glendale, CA 91203");
            lines.add("");
            lines.add("Ms. M.K. Delgado");
            lines.add("2 River Dr.");
            lines.add("");
            System.out.println(getAddress("Jack S. Smith"));
            System.out.println("test line break");
            }
}

Thank you for your time. 感谢您的时间。

EDIT: This method is supposed to be written in the Recipients class. 编辑:此方法应该写在收件人类中。 It has an assumed constructor and I've written other methods inside the class. 它有一个假定的构造函数,并且我在类中编写了其他方法。 I've edited the class. 我已经编辑了课程。 I'm having trouble with the logic. 我在逻辑上遇到麻烦。

Worksheet says: Write the getAddress method of the Recipients class. 工作表说:编写收件人类的getAddress方法。 This method should return a string that contains only the address of the corresponding name parameter. 此方法应返回仅包含相应名称参数地址的字符串。 For example, if name is "Jack S. Smith", a string containing the three subsequent lines of his address should be returned. 例如,如果名称为“ Jack S. Smith”,则应返回包含其地址的后三行的字符串。 This string should contain line breaks in appropriate places, including after the last line of the address. 该字符串应在适当的位置包含换行符,包括地址的最后一行之后。

public String getAddress(String name)

You should create class for saving name and address. 您应该创建用于保存名称和地址的类。 Please find below mentioned approach. 请找到以下提到的方法。

package hello;

import java.util.ArrayList;
import java.util.List;

class Employee {

    private String name;
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}

public class So3 {

    public static void main(String[] args) {
        List<String> lines = new ArrayList<String>();
        lines.add("Mr. J Adams");
        lines.add("Rose St.");
        lines.add("Ithaca, NY 14850");
        lines.add("");
        lines.add("Jack S. Smith");
        lines.add("12 Posy Way");
        lines.add("Suite #201");
        lines.add("Glendale, CA 91203");
        lines.add("");
        lines.add("Ms. M.K. Delgado");
        lines.add("2 River Dr.");
        lines.add("");

        List<Employee> employees = new ArrayList<Employee>();
        Employee employee = new Employee();
        for (String str : lines) {
            if (str.isEmpty()) {
                if (employee.getName() != null && employee.getAddress() != null) {
                    employees.add(employee);
                    employee = new Employee();
                }
            } else if (employee.getName() == null) {
                employee.setName(str);
            } else {
                if (employee.getAddress() == null) {
                    employee.setAddress(str);
                } else {
                    employee.setAddress(employee.getAddress() + " " + str);
                }
            }
        }

        if (employee.getName() != null && employee.getAddress() != null) {
            employees.add(employee);
        }

        System.out.println(getAddress(employees, "Ms. M.K. Delgado"));

    }

    private static String getAddress(List<Employee> employees, String name) {
        if (employees != null && name != null) {
            for (Employee employee : employees) {
                if (name.equals(employee.getName())) {
                    return employee.getAddress();
                }
            }
        }
        return null;
    }

}

If you have to use as you have wanted then you need to modify your getAddress method. 如果必须按需要使用,则需要修改getAddress方法。 Try the following getAddress method in your code: 在您的代码中尝试以下getAddress方法:

public String getAddress(String name) {
    String address = "";
    boolean nameFound=false;
    for(String str:lines)
    {
        if(!nameFound && str.equals(name))
            nameFound=true;
        else if(nameFound && str.isEmpty())
            break;
        else if(nameFound && !str.isEmpty())
            address+=str;
    }
    return address;
}

You should use HashMap rather than an ArrayList for this. 为此,您应该使用HashMap而不是ArrayList。 This is a perfect scenario to use HashMap instead. 这是使用HashMap的完美方案。

public class APProblem {
    Map<String, List<String>> lines2 = new HashMap<>();

    public List<String> getAddress(String name) {
        return lines2.get(name);
    }

        public void main() {
            String name = "Mr. J Adams";
            List<String> address = Arrays.asList("Rose St.", "Ithaca, NY 14850");
            lines2.put(name, address);
            name = "Jack S. Smith";
            address = Arrays.asList("12 Posy Way", "Suite #201", "Glendale, CA 91203");
            lines2.put(name, address);

            System.out.println(getAddress("Jack S. Smith"));
            System.out.println("Testing line break.");
            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM