Java using code from method doesn't work

2019-09-21 11:52发布

I'm trying to use a specific code but it won't work for some reason. I have to methods in the same class:

public void InputEnter()
    {       
        if(Input.GetKey(getCoords)) {
            Move(GetTransform().GetPos());
        System.out.println((GetTransform().GetPos()));
        }
}

this method gives me some coordinates of Vector3f once I hit enter. The other code writes to a file.

public void ProcessText()
     {
        System.out.println("ProcessText Operational");

        String file_name = "C:/Users/Server/Desktop/textText.txt";

        try
        {           
            ProcessCoords file = new ProcessCoords(file_name);
            String[] aryLines = file.OpenFile();

            int i;
            for (i = 0; i < aryLines.length; i++)
            {
                System.out.println(aryLines[i]);    

                 if(aryLines[i].startsWith("makeGrass:")) {
                        String Arguments = aryLines[i].substring(aryLines[i].indexOf(":")+1, aryLines[i].length());
                        String[] ArgArray = Arguments.split(",");

                        this.makeGrass(Double.parseDouble(ArgArray[0]), 
                                   Double.parseDouble(ArgArray[1]), 
                                   Double.parseDouble(ArgArray[2]));                    
                }
            }

            ProcessCoords data = new ProcessCoords(file_name);      
            data.writeToFile("makeGrass:");

            System.out.println("Coordinates Saved!");

        } catch(IOException e) {            
            System.out.println(e.getMessage());
        }
     }

What I wanted to do is to use the InputEnter method in the ProcessText method so I just deleted InputEnter and used the Input code in the ProcessText method:

public void ProcessText()
     {
        System.out.println("ProcessText Operational");

        String file_name = "C:/Users/Server/Desktop/textText.txt";

        try
        {           
            ProcessCoords file = new ProcessCoords(file_name);
            String[] aryLines = file.OpenFile();

            int i;
            for (i = 0; i < aryLines.length; i++)
            {
                System.out.println(aryLines[i]);    

                 if(aryLines[i].startsWith("makeGrass:")) {
                        String Arguments = aryLines[i].substring(aryLines[i].indexOf(":")+1, aryLines[i].length());
                        String[] ArgArray = Arguments.split(",");

                        this.makeGrass(Double.parseDouble(ArgArray[0]), 
                                   Double.parseDouble(ArgArray[1]), 
                                   Double.parseDouble(ArgArray[2]));                    
                }
            }

            if(Input.GetKey(getCoords)) {
                Move(GetTransform().GetPos());
            ProcessCoords data = new ProcessCoords(file_name);                  
            data.writeToFile("makeGrass:");

            System.out.println("pressing enter doesn't work!!");

            System.out.println((GetTransform().GetPos()));
            }

            System.out.println("Input.GetKey doesn't work anymore, but why and how to fix it??");
        } catch(IOException e) {            
            System.out.println(e.getMessage());
        }
     }

however now, pressing enter does no longer give me the coordinates as it did before, I really do not understand why and I would need some help.

Thanks a lot!

标签: java methods
1条回答
Evening l夕情丶
2楼-- · 2019-09-21 12:34

Okay it took me a while but I've figured it out, It's actually very simple:

As you can see in ProcessText() I've included both the code that reads from a file and the code that writes to a file.

ProcessCoords data = new ProcessCoords(file_name);      
data.writeToFile("makeGrass:");

System.out.println("Coordinates Saved!");

My idea was then to put the Input method into the ProcessText method as you can see here:

if(Input.GetKey(getCoords)) {
Move(GetTransform().GetPos());
ProcessCoords data = new ProcessCoords(file_name);                  
data.writeToFile("makeGrass:");
System.out.println("pressing enter doesn't work!!");
System.out.println((GetTransform().GetPos()));

This is almost correct but well.. to have the input work for a gameObject I need to add the Input class as a component:

gameObject.addComponent(new InputClass());

All I had to do instead is to take it out from my ProcessText method and move it into my Input class so it looks like this:

public void Input(float delta)
{           
String file_name = "C:/Users/Server/Desktop/textText.txt";

try
{           
    ProcessCoords data = new ProcessCoords(file_name);

    if(Input.GetKey(getCoords)) {   
    data.writeToFile("makeGrass:" + (GetTransform().GetPos()));

    System.out.println("Coordinates Saved!");   
    System.out.println((GetTransform().GetPos()));
    }   

    } catch(IOException e) {
        System.out.println(e.getMessage());
    }
}

After that I was able to actually use the input for the respective gameObject and obviously get the apropriate coordinates writen to the text file only if I press enter.

And here's the result: http://www.pic-upload.de/view-27748157/AnotherExample.png.html

I hope my answer will help someone else in the future!

查看更多
登录 后发表回答