This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Jython/Python and the fly likes Jython Grief Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Languages » Jython/Python
Bookmark "Jython Grief" Watch "Jython Grief" New topic
Author

Jython Grief

joshua weinstein
Greenhorn

Joined: Feb 20, 2003
Posts: 2
Hello,
Ok, here's my diclaimer: I am as beginner as you get with Obj Orient. prog. and know very little about Java and computers altogether, but wound up using Jython to write Scripts for a software. Anyway, here's the deal: I want to access Java methods like readInt so that I can use them in my scripts. But when I use the import command in Jython i get an error message telling me that the java module does not exist!! Can anyone help me out here? If you really have some spare time on your hands, you could download Jython from www.Jython.org and play around with it.
Thanks,
Josh


staples - serious beginner!!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hi Joshua, can you please be a little more specific and give us a code example which does not work for you? What platform are you using, what java?
I have downloaded onto Win/NT and installed Jython and can do things like:
>>> from java.io import *
>>> x = FileInputStream("Hello.txt")
>>> y = InputStreamReader(x)
>>> z = BufferedReader(y)
>>> print z.readLine()
Hello
>>> print z.readLine()
None
>>> z.close()
>>> y.close()
>>> x.close()
[ February 21, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
joshua weinstein
Greenhorn

Joined: Feb 20, 2003
Posts: 2
hey barry,
thanks for taking the time to erply to my post. ok, let me give you a more precise description of my problem. I am trying to read information from a file. I can open the desired file and read in data no problem, however I can only read data in the form of strings and not integers! I wasnt ot be able to import the readInt() method from Java in order to read data in the form of integers.
Here's the code:
from java.io import readInt()
import struct
import math
import os
file1 = open("C:/Program Files/Hec/HecDssVue/script_iofiles/prompt_pmaker.in")
nbpart = file1.readInt()
The compile/interpretor error occurs on that last line and it is:
AttributeError: instance of 'org.Python.core.PyFile' has no attribute 'readInt'
I am using the Windows/2000 platform. I think that the problem is that I am not importing the readInt() method properly. Any suggestions?
Thanks again,
Josh
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Josh, can you give an example of what your input file looks like? Is it a file written by a Java program using writeInt? Or is it a text file?
When I know I can play around a bit to get something to read it.
-Barry
josh weinstein
Greenhorn

Joined: Feb 20, 2003
Posts: 1
Hey Barry,
The file is a text file. It looks like this:
7
subbasin-3
subbasin-5
subbasin-6
subbasin-7
subbasin-8
subbasin-9
subbasin-10
2
flow
elevation
10min
The last line of the code that I gave you is supposed to read the number '7' from the file. I think that it is the import of the readInt() method that Jython is having a problem with.
Thanks,
Josh


jweins1 - serious beginner
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hi Josh,
I think the problem is that you are getting a bit confused between the files that Jython uses (Python files), and the files that Java uses. A Python file only has the read(), readline() and readlines() methods and they read Jython strings.
So you can do something like line=file1.read() to get a string (line) from the file. line will have the value None at the end of the file. You would the have to split the strings and coerce the bits that are representing integers: i = int(piece) , where piece is a string '42', say.
To do Java-like processing you would have to create a DataInputStream and use its readInt() method. But that would only work with files created with DataOutputStream.writeInt().
The important thing is to realize that the Python IO is independent from Java IO and is a viable simple alternative to the Java IO.
Now I'm going to play with your file, to see if I'm talking sense or not
-Barry
BTW you don't import methods but modules (or Java packages/classes).
Also a book: "Jython Essentials, Samuele Pedroni, Noel Rappin, O'Reilly"
[ March 01, 2003: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This is in no way a program, but more what I typed by hand. You can paste it into a file (josh.py) and do jython josh.py. Try it:
Note the use of the "slice" [:-1] to get rid of the newline character. And don't forget make sure that 10min in the data file is terminated by a new line.
[ March 01, 2003: Message edited by: Barry Gaunt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jython Grief
 
Similar Threads
Accessing Python libraries from Java
possibility of dynamic java code transfer?
Jython
How to assess the execution time
Exec'ing a string?