I used the information from this link to write the following code below to run a Python script and have it return a single string returned:
public static void Option_2_Output()
{
Scanner Option2_Input = new Scanner(System.in);
System.out.println("");
System.out.println("===============================================");
System.out.println(" Testing Means of Two Groups ");
System.out.println("===============================================");
System.out.println("");
String Option2_program = "python";
String Option2_path = "C:\\PathtoScript\\Python_Test.py";
try{
String Option2_datapath;
System.out.print("Please type the location of your Data : ");
Option2_datapath = Option2_Input.next();
ProcessBuilder Option2_process = new ProcessBuilder(Option2_program,Option2_path,"" + Option2_datapath);
Process Option2_Output = Option2_process.start();
BufferedReader in = new BufferedReader(new InputStreamReader(Option2_Output.getInputStream()));
String Option2_Final = new String(in.readLine());
System.out.println(Option2_Final);
}catch(Exception e){
System.out.println(e);
}
}
The object Option2_Final
streams the last printed output from the Python script back as a string. How do I get more than one value back?
Only one line is being streamed back because there's only one readLine()
function being utilized. A for-loop could be written to search for each additional line or objects could be created for each line you're intending to pass back:
BufferedReader in = new BufferedReader(new InputStreamReader(Option2_Output.getInputStream()));
String Option2_Final_1 = new String(in.readLine()); //Line 1
String Option2_Final_2 = new String(in.readLine()); //Line 2
System.out.println(Option2_Final_1); //Print Line 1
System.out.println(Option2_Final); //Print Line 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.