简体   繁体   English

从文本文件读取为arraylist / hashmap

[英]Reading from a text file as an arraylist/hashmap

Okay so here's an explanation of what I have to write: 好的,这是我必须写的说明:

  • getBreadInfo - reads bread.txt into an array list (containing bread name, $, and price) and then assigns to an array breadInfo[], then return this array for SandwichApp to display bread menu. getBreadInfo将面包getBreadInfo读取到一个数组列表(包含面包名称,$和价格)中,然后分配给一个数组BreadInfo [],然后将此数组返回给SandwichApp以显示面包菜单。
  • getBread - is similar to getBreadInfo, except it only contains the bread name, and return another array bread[] for SandwichApp to figure out which bread the user selected because user type in a number associate with the bread (index+1), rather than bread name. getBread与getBreadInfo相似,不同之处在于它仅包含面包名称,并为SandwichApp返回另一个数组面包[],以找出用户选择的面包,因为用户键入的数字与面包(index + 1)相关,而不是面包名称。
  • getMapBreadPrice - is similar to the above two, except it returns a hash map containing pair values for bread name (key) and price (value) for SandwichApp to figure out what is the price for the bread user selected. getMapBreadPrice与上面两个类似,不同之处在于它返回一个哈希图,其中包含面包名称(关键字)和SandwichApp的价格(值)的对值,以找出所选面包用户的价格。

This is what I have written. 这是我写的。 Just wondering if this is correct or not? 只想知道这是否正确?

public class SandwichDB {  
private ArrayList<String> breadsList = null;

public String[] getBreadInfo()
{ 
    breadsList = new ArrayList<>();

        try (BufferedReader in =
                new BufferedReader(
                new FileReader("bread.txt")))
        {
            String line = in.readLine();
            while (line != null)
            {
                String[] elems = line.split("~");
                breadsList.add(elems[0]+ " $" + elems[1]);  
            }

        }
        catch(IOException e)
        {
            System.out.println(e);
            return null;
        }
    String[] breadInfo = breadsList.toArray(new String[]{});
    return breadInfo;
}
public String[] getBread()
{
    breadsList = new ArrayList<>();

        try (BufferedReader in =
                new BufferedReader(
                new FileReader("bread.txt")))
        {
            String line = in.readLine();
            while (line != null)
            {
                String[] elems = line.split("~");
                breadsList.add(elems[0]);  
            }

        }
        catch(IOException e)
        {
            System.out.println(e);
            return null;
        }
        String[] bread = breadsList.toArray(new String[]{});
        return bread;
}
public HashMap<String, String> getMapBreadPrice()
        {
            HashMap<String, String> mapBreadPrice = new HashMap<>();
            String line, elems[];
            try
            {
                FileReader fr = new FileReader("bread.txt");
                BufferedReader br = new BufferedReader(fr);

                while ((line=br.readLine()) != null)
                {
                    elems = line.split("~");
                    mapBreadPrice.put(elems[0], elems[1]);
                }
            }
            catch(IOException e)
            {
                System.out.println(e);
                return null;
            }
            return mapBreadPrice;
        }
        }

The first readLine stands before the while and hence is not repeated. 第一个readLine位于while之前,因此不再重复。 Hence the while does not end. 因此,一会儿并没有结束。

for (;;) {
    String line = in.readLine();
    if (line == null) {
        break;
    }

It seems like you're reading the same file 3 times in order to build 3 structures. 似乎您要读取3次相同的文件才能构建3个结构。 You should build your data structures with one read of the file. 您应该通过一次读取文件来构建数据结构。

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

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