首页 > 解决方案 > 如何将文本文件中的值存储到适当的变量中,以及如何使此代码块更清晰?

问题描述

我正在开发一个程序,该程序将允许用户输入他们拥有的游戏机,然后输入他们为该游戏机拥有的每个游戏。当程序加载所有以前保存的信息时,我目前卡住了,将这些信息存储在适当的变量中。我已经取得了一些进展,但我在某个点上卡住了,我想要一些关于如何前进的指导。

    public static void FileReader() 
{
    try 
    {
        BufferedReader loadReader = new BufferedReader(new FileReader("GamesList.txt"));
        while((loadText = loadReader.readLine()) != null) // Search through each line
        {
            if(loadText.equalsIgnoreCase("Xbox One" ) || loadText.equalsIgnoreCase("Nintendo Switch") || loadText.equalsIgnoreCase("Playstation 4")) // Look for these consoles
            {
                for(int x = 0; x < consolesList.size(); x++) // Search through the entire arrayList of consoles
                {
                    if(consolesList.get(x).consoleName.equalsIgnoreCase(loadText)) // If the current consoles on value x matches the loadText
                    {
                        while((loadText = loadReader.readLine()) != null) // if there are still lines left to search
                        {
                            if(loadText.equalsIgnoreCase("Xbox One") && loadText != consolesList.get(x).consoleName) // I THINK this is needed to exit the first console, and start the second
                            {
                                System.out.println("This was run " + loadText); // Originally had a recursive call here, but that lead to a stack overflow. 
                            }
                            consolesList.get(x).gamesList.add(loadText); // Add all the games found back into the list of games
                        }
                    }
                }
            }
            else 
            {
                System.out.println("We didn't find anything " + loadText);
            }
        }
    }
    catch (Exception e)
    {
        System.out.println("This did nothing");
    }
}

一旦列出了所有当前控制台的游戏,我被困的地方就是分离控制台。假设我在 PlayStation 4 上添加了三款游戏,然后在 Xbox One 上添加了三款游戏。一旦程序找到 PlayStation 的文本,它将列出所有游戏,但是,它还将包括 Xbox 及其所有游戏,作为 PlayStation 的游戏。它也永远不会继续搜索 Xbox One 游戏,并将它们列为自己的游戏。它只是停在那里并继续程序的其余部分。我知道这发生在第二个 while 语句所在的行上,我尝试在那里创建一个递归调用以使其重新开始,但同样的问题会再次发生,因为没有实际的增量,只会导致堆栈溢出。关于如何让它退出第一个控制台的任何想法,并开始列出第二个,等等?另外,我猜这段代码很难阅读,因为它是一堆嵌套的 if/for/while 循环。关于使它更清晰的任何建议?

-已解决-我可以通过编辑 Nayan Patel 的一行代码来使其工作。我使用 for 循环遍历 arrayList 中的每个元素,当找到每个控制台时,我为该数字设置一个索引,并将游戏添加到该索引。

    public static void FileReader() 
{
    try {
        BufferedReader loadReader = new BufferedReader(new FileReader("GamesList.txt"));
        //String loadText = "";
        int consoleIndex = -1; // for storing the console index
        while((loadText = loadReader.readLine()) != null) {
            // getting the index of loadText from consoleList.
            // store your consoles name in uppercase in consoleList.
            int index = -1;
            for(int x = 0; x < consolesList.size();x++) {
                if(loadText.equalsIgnoreCase(consolesList.get(x).consoleName)) {
                    index = x;
                }
            }
            if(index != -1){ // if console name is present in loadText
                consoleIndex = index;
                continue;
            }
            // put game in respected console.
            consolesList.get(consoleIndex).gamesList.add(loadText);
            System.out.println("We got to the end");
        }
    } catch (Exception e) {
        System.out.println("This did nothing");
    }
}

标签: javaeclipse

解决方案


这是您的问题的解决方案,

try {
    BufferedReader loadReader = new BufferedReader(new FileReader("GamesList.txt"));
    String loadText = "";
    int consoleIndex = -1; // for storing the console index
    while((loadText = loadReader.readLine()) != null) {
        // getting the index of loadText from consoleList.
        // store your consoles name in uppercase in consoleList.
        int index = consolesList.indexOf(loadText.toUpperCase()); // 
        if(index != -1){ // if console name is present in loadText
            consoleIndex = index;
            continue;
        }
        // put game in respected console.
        consolesList.get(consoleIndex).gamesList.add(loadText);
    }
} catch (Exception e) {
    System.out.println("This did nothing");
}

推荐阅读