0

I create simple python statement my_utils.py

def adder(a, b):
    c=a+b
    return c

I would like to assign value to python in Java.

public class ParameterPy {
public static void main(String a[]){
try{

int number1 = 100;
int number2 = 200;

ProcessBuilder pb = new ProcessBuilder("C:/Python27/python","D://my_utils.py",""+number1,""+number2);
Process p = pb.start();

BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));

System.out.println(".........start   process.........");  
String line = "";     
while ((line = bfr.readLine()) != null){
    System.out.println("Python Output: " + line);
}
System.out.println("........end   process.......");


}catch(Exception e){System.out.println(e);}
}
}

However, process builder is not able to pass the parameter value to a, b in python and display the result. enter image description here

How to give the parameter value to python? If the numeric value is worked? How about if I pass the non-numeric value such as a string to python

def str(myWord):
    if myWord=="OK":
        print "the word is OK."
    else:
        print " the word is not OK."
2
  • Possible duplicate of stackoverflow.com/questions/27235286/… Commented Oct 6, 2016 at 7:21
  • 1
    You got the input stream from the process, not the output stream that is where the print statements are written to Commented Oct 6, 2016 at 7:27

2 Answers 2

2

The Python sys module provides access to any command-line arguments via the sys.argv. But the type of arguments is string always. Here is the example how I would like to check numeric values:

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

def adder(a, b):
    c=a+b
    return c

def is_number(x):
    try:
        float(x)
        return True
    except ValueError:
        return False

p1 = sys.argv[1]
p2 = sys.argv[2]

if is_number(p1) and is_number(p2):
    print "Sum: {}".format(adder(float(p1), float(p2)))
else:
    print "Concatenation: {}".format(adder(p1, p2))

UPDATED: As @cricket_007 mentioned, you can use isdigit if you want to check integers only. But it does not work for float:

>>> "123".isdigit()
True
>>> "12.3".isdigit()
False
Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't the python string class provide isdigit? Why try to cast the value?
isdigit() is also OK if you expect integers only. But it returns False for float.
0
import sys
# print sys.argv

print "sys.argv is:",sys.argv
# ['D://my_utils.py', '100', '200', 'google']

a= sys.argv[1]
b= sys.argv[2]
print "a is:", a
print "b is:", b
a= int (a)
b= int(b)

def adder(a, b):
    c=a+b
    return c

print adder(a,b)

searchTerm=sys.argv[3]
print searchTerm  ##google

def word(searchTerm):
    if searchTerm=="google":
        print " you get it"
    else:
        print " the word is different."

word(searchTerm)

in java

int number1 = 100;
int number2 = 200;
String searchTerm="google";
ProcessBuilder pb = new ProcessBuilder("C:/Python27/python","D://searchTestJava//my_utils.py",""+number1,""+number2,""+searchTerm);
Process p = pb.start();

BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));

System.out.println(".........start   process.........");  
String line = "";     
while ((line = bfr.readLine()) != null){
    System.out.println("Python Output: " + line);

the output result:

Python Output: a is: 100
Python Output: b is: 200
Python Output: 300
Python Output: google
Python Output:  you get it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.