运行Java程序时黑屏

问题描述 投票:0回答:1

我正在尝试运行 java 程序,但它输出空白屏幕。该程序的目标是接收对话文本文件并使用 HashMap 输出一行对话。

我正在运行一个程序,该程序获取字符对话的输入文件,使用 stringTokenizer 将对话加载到 HashMap 中,然后检索一行对话。我尝试调试我的程序,但当输出始终为白色时,很难确定问题发生在哪里。任何帮助都会对此事有所帮助。下面是我的 main.java 和文本文件

> package Main;
> 
> import java.awt.Color;
> import logic.Control;
> import FileIO.EZFileRead;
> import java.util.StringTokenizer;
> import java.util.HashMap;
> 
> 
> public class Main{
> //Fields (Static) below...
>   public static Color c = new Color(0,153,0);
>   public static boolean isImageDrawn = false;
>   public static HashMap<String, String> map = new HashMap<>();
>   
>   
> //End Static fields...
>   
>   public static void main(String[] args) {
> Control ctrl = new Control();             
> ctrl.gameLoop();                          
>   
    }
>   
>
> 
> //Load text file into game engine in the start method. 
> 
> EZFileRead ezr = new EZFileRead ("scripts.txt");
> 
//Parse through textfile and populate the HashMap using StringTokenizer
 
> String line = ezr.getNextLine(); //read the first line from the file
> 
> while(!line.equals("END OF FILE")); { //while the text file does not end
> 
> 
> line = ezr.getNextLine(); //keep reading the lines
> 
> //populate hashMap using stringTokenizer
> 
> StringTokenizer st = new StringTokenizer(line,":");
> 
> if(st.countTokens() == 1) { //check if string Tokenizer has exactly 2 tokens
> 
> String key = st.nextToken().trim(); // acquire key from the token without white space
> String value = st.nextToken().trim(); // acquire value from the token without white space
> 
> map.put(key, value); //populate the HashMap with the key and the value
> 
> else { //check in case line does not load
> 
> System.err.println("Invalid Line: "+ line); //print out error line
>   }
> 
>   }
> 
> 
>   }
> //This is your access to the "game loop" (It is a "callback" method from the Control class
>   public static void update(Control ctrl) {
> 
> String requestLine = map.get("string1"); //retrieve dialogue associated with string1
> 
> ctrl.drawString(100,250,requestLine, Color.GREEN); //display using drawString method
> 
>   }
>   
> }
TEXTFILE:Scripts.txt

string1*Roy: ‘I want to eat food’
string2*Roy: ’However, I need to go home’
string3*Roy: ‘I can’t walk if I am hungry’
string4*Roy: ‘It will rain in an hour'
string5*Roy: ‘Food later then’ 
java eclipse
1个回答
1
投票

您的上述代码有以下问题。 你的 while 循环条件 while(!line.equals("END OF FILE")); 末尾有一个分号,将其变成空循环语句。因此,打算成为循环一部分的代码块始终作为独立块执行一次,从而产生潜力。

从文件读取并填充 HashMap 的逻辑放置在任何方法之外。此代码应该位于从 main 调用的方法内或直接在 main 方法内。

尝试一下我在 IDE 中优化过的代码

package Main;
import java.awt.Color;
import java.util.HashMap;
import java.util.StringTokenizer;
import logic.Control;
import FileIO.EZFileRead;

public class Main {
    public static Color c = new Color(0, 153, 0);
    public static boolean isImageDrawn = false;
    public static HashMap<String, String> map = new HashMap<>();
    
    public static void main(String[] args) {
        Control ctrl = new Control();             
        loadDialogues();
        ctrl.gameLoop();                          
    }
    
    public static void loadDialogues() {
        EZFileRead ezr = new EZFileRead("scripts.txt");
        String line = ezr.getNextLine(); 
        
        while (line != null && !line.equals("END OF FILE")) {
            StringTokenizer st = new StringTokenizer(line, "*");
            if (st.countTokens() == 2) {
                String key = st.nextToken().trim();
                String value = st.nextToken().trim().split(":", 2)[1].trim(); 
                map.put(key, value);
            } else {
                System.err.println("Invalid Line: " + line); 
            }
            line = ezr.getNextLine(); // Keep reading the lines
        }
    }

    public static void update(Control ctrl) {
        String requestLine = map.get("string1"); 
        if (requestLine != null) {
            ctrl.drawString(100, 250, requestLine, Color.GREEN); 
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.