java - 如何将文本文件中的值存储到适当的变量中,以及如何使此代码块更清晰?
问题描述
我正在开发一个程序,该程序将允许用户输入他们拥有的游戏机,然后输入他们为该游戏机拥有的每个游戏。当程序加载所有以前保存的信息时,我目前卡住了,将这些信息存储在适当的变量中。我已经取得了一些进展,但我在某个点上卡住了,我想要一些关于如何前进的指导。
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");
}
}
解决方案
这是您的问题的解决方案,
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");
}
推荐阅读
- sql - ' 和 " 在 SQL 中有很大的不同吗?
- youtube-dl - Python youtube_dl 下载后立即删除文件
- python - Python pandas数据框,检查列值是否与每一行的其他列值匹配
- git - 我不小心在 master 上工作,如何将我对分支重置 master 所做的所有更改转移到最后一次提交?
- javascript - 如何在 Angular 9 中将 HTML 页面拆分为 A4 大小
- c++ - 虚函数分段错误
- blazor - 来自模板的 Blazor WebAssembly 本地 IIS
- google-apps-script - CORS 错误:由于 CORS 策略,请求已被阻止
- sql - 在oracle中从当前日期获取上一个日期的所有记录
- android-contentprovider - Android 自定义内容提供程序,如果应用程序未运行,则光标返回为 null