Führen Sie die Funktion aus der Textdatei in Java ausJava

Java-Forum
Anonymous
 Führen Sie die Funktion aus der Textdatei in Java aus

Post by Anonymous »

Ich möchte ein Programm implementieren, das eine Textdatei liest und jede Zeile darauf ausführt.

Code: Select all

{
t.getP().getName()
t.getP().getAge()
t.getP().getLocation()
}
< /code>
und das folgende Java -Programm: < /p>
public class Test {
private Person p;

public Test(){
p = new Person("Jhon", "19", "New York", "Mathematician");
}

public Person getP() {
return p;
}

public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException{
Test t = new Test();

// Read txt file with methods
String line1 = "t.getP().getName()";
String line2 = "t.getP().getAge()";
String line3 = "t.getP().getLocation()";

// Execute methods
Method m = t.getClass().getMethod(line1);
m.invoke(t);

}
}
< /code>
Ich erhalte den folgenden Fehler: < /p>
Exception in thread "main" java.lang.NoSuchMethodException: Test.t.getP().getName()()
at java.lang.Class.getMethod(Class.java:1786)
at Test.main(Test.java:24)
Ich verstehe, dass das Programm keine Methode findet, aber ich weiß nicht, wie ich es beheben soll.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post