The Artima Developer Community
Sponsored Link

Java Answers Forum
help!!

5 replies on 1 page. Most recent reply: Oct 29, 2003 10:18 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Subha

Posts: 16
Nickname: nikki
Registered: Sep, 2003

help!! Posted: Oct 28, 2003 11:04 AM
Reply to this message Reply
Advertisement
I need to direct the output of my java program to an excel/notepad file.
I have the current o/p as follows..

No of visits to location ( 1,1) >> [0, 2, 4, 28, 2]
No of visits to location ( 1,2) >> [0, 4, 8, 9]
No of visits to location ( 1,3) >> [0, 82, 2]
No of visits to location ( 1,4) >> [0, 7, 10]
No of visits to location ( 1,5) >> [0, 17]
and this goes on till location (10,10)

I want to save them in such a way that each value in particular location should be saved in each row. ...like:
( 1,1) ( 1,2) ....so on till (10,10)
0 0
2 4
4 8
28 9
2

can you help me get this done in java.
thanks.


Subha

Posts: 16
Nickname: nikki
Registered: Sep, 2003

Re: help!! Posted: Oct 28, 2003 11:07 AM
Reply to this message Reply
Sorry,this is how i want it...

I want to save them in such a way that each value in particular location should be saved in each row. ...like:

( 1,1) ( 1,2) ....so on till (10,10)
0 0
2 4
4 8
28 9
2

can you help me get this done in java.
thanks.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: help!! Posted: Oct 28, 2003 5:08 PM
Reply to this message Reply
Huh? What format is the data in? Is it already in a 2d array in Java? In a database? In a text file? How do you want to get it into Excel? Are you using some Java library to access COM Automation? By writing to text file (comma separated, tab separated, fixed-width...) and import that into Excel? Why do you want to do this with Java? Depending upon where it is coming from, it may make a lot more sense to do the whole task with Excel itself, or with Python and win32com, etc.

Subha

Posts: 16
Nickname: nikki
Registered: Sep, 2003

Re: help!! Posted: Oct 29, 2003 5:38 AM
Reply to this message Reply
yes it is in a text file right now....if not in excel ...is it possible to get the output in the required format in a txt file....?
i want it in a text file or excel so that i can load this data in matlab and use it for further calculations.since matlab can load only numerical values without any special charecters and strings.
is it possible??

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: help!! Posted: Oct 29, 2003 9:38 AM
Reply to this message Reply
So, I think you are saying the exact format of the input text file looks like this:
No of visits to location ( 1,1) >> [0, 2, 4, 28, 2]
No of visits to location ( 1,2) >> [0, 4, 8, 9]
No of visits to location ( 1,3) >> [0, 82, 2]
No of visits to location ( 1,4) >> [0, 7, 10]
...

And you want to transform it to a fixed-width text format something like this:

(1,1) (1,2) (1,3) ...
0 0 0 ...
2 4 82 ...
4 8 2
28 9
2


Is that correct? If so, this would be a pretty easy thing to do in Python, Perl or Ruby. I wouldn't choose Java (or C/C++) for this task, unless I believed it should be the one-and-only language for all tasks ( http://www.artima.com/forums/flat.jsp?forum=106&thread=14196 ).

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: help!! Posted: Oct 29, 2003 10:18 AM
Reply to this message Reply
Here is a sample in Python. (I haven't tried it with Jython, but that should work too (I think it does the list comprehensions and stuff, but I'm not certain...), if you insist on using Java somehow.) You can download Python from http://www.Python.org, if you haven't already got it.

# Read a text file, if it was specified (otherwise, use sample):
import os,sys
if len(sys.argv) > 1 and os.path.isfile(sys.argv[1]):
lines = [l.strip() for l in file(sys.argv[1]).readlines()]
else:
sampleInput = """ No of visits to location ( 1,1) >> [0, 2, 4, 28, 2]
No of visits to location ( 1,2) >> [0, 4, 8, 9]
No of visits to location ( 1,3) >> [0, 82, 2]
No of visits to location ( 1,4) >> [0, 7, 10]
No of visits to location ( 1,5) >> [0, 17]
No of visits to location ( 1,6) >> [0, 1, 2, 3, 4, 5, 6, 7]"""
lines = [l.strip() for l in sampleInput.splitlines()]

stuff={}
for l in lines:
key,values = l[l.find('('):].split('>>')
stuff[eval(key)] = eval(values)

keys = stuff.keys()
keys.sort()

# Determine which point has longest list of values:
longest = max( [len(stuff[k]) for k in stuff] )

elementFormat = '%-6s' # You might tweak this if you have bigger values.

# Print headers:
for k in keys: print elementFormat % str(k),
print

# Print data:
for i in range(longest):
for k in keys:
if len(stuff[k]) > i:
value = str(stuff[k][i])
else:
value = ''
print elementFormat % value,
print

Flat View: This topic has 5 replies on 1 page
Topic: Input from Clipboard Previous Topic   Next Topic Topic: Copy WMF to MSWORD

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use