Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

zootm posted:

Flying somewhat blind:

code:
Source source = new DOMSource( doc );
Result result = new StreamResult( new FileOutputStream( filename ) );

Transformer transformer = TransformerFactory.newInstance().newTransformer();

transformer.transform( source, result );
That might well work. I have not tried it, however. The default "transform" is identity if you're wondering what the deal is.


The JAXP tutorial has the following example: http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPXSLT4.html

Adbot
ADBOT LOVES YOU

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

Fehler posted:

Speaking of executing commands, is there any way to do something like "mailto:foo@bar.com?subject=1" in Java?

When I do Runtime.getRuntime().exec('mailto:foo@bar.com?subject=1'); I get this exception: "java.io.IOException: Cannot run program "mailto:foo@bar.com?subject=1": CreateProcess error=2"

If you're trying to send emails, you can use the java mail API which provided you have all the correct SMTP settings can send email.

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

mister_gosh posted:

I'm unclear on the concept here. I want to put stuff into a HashMap and at a later time, retrieve the objects from the map by iterating through it and processing every object. I want it to be in the order that I originally put them in.

I realized when I did this that a HashMap is unordered, so I read up on the Collections and decided to try the TreeMap. Doing so put it in a different, but still not correct, order.

Is there another Collection I can use that allows me to store key->object pairs but maintain them during an iteration process (index(0) through index(x))?

java.util.LinkedHashMap seems to be what you want:

API Docs posted:

Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

schzim posted:

JavaSeverFaces and JavaServerPage Goons to the rescue please k thnx.

I have a string in a session attribute.

I am working with an off the shelf package which includes some jsf components.
I am using this component and want to set the value of one of the parameters to a string expression using the session variable.

....
<xxx:productView id="something"
color = "red"
filename = "C:/webdir/foo/"${session.getAttribute("fileName")}"
shape = "line"
//other stuff
</xxx:productView>

This does not seem to be able to work in any way that I have tried. I am certainly a JSP/JSF retard. So please educate me.

I believe what you're after is the pageContext.setAttribute method, to be used like so:
code:
<%
pageContext.setAttribute("fileName", "C:/webdir/foo/" + session.getAttribute("fileName"));
%>
..
..

<xxx:productView id="something"
color = "red"
filename = "${fileName}"
shape = "line"
//other stuff
</xxx:productView>

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

Startacus posted:

Hey, I can use some help here. I have a program where I have an array of objects and I want to output them to a .csv file. Right now the object just holds variables for name and grade, both of them are strings (Although I should parse an int from the grade but I can do that later).


So essentially I want to output to a fie the name and the grade separated by a comma.


I can't figure out what kind of stream I need to use or how I would set it up.

I would basically want this:

code:
 for(int i = 0 ; i < objectSize; i++)
  {
   System.out.println(Student[i].getName() + "," + Student[i].getGrade());
  }
Except, rather then printing it to the console, I want that written to a file.

Can anyone help me out please?
This should do it:
code:
//Add these at the top
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
//This writes the file
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("MyFile.txt"));
 for(int i = 0 ; i < objectSize; i++)
  {
   writer.write(Student[i].getName() + "," + Student[i].getGrade() + "\r\n");
  }
writer.close();
} catch(IOException ioe) {
   //You should probably do something more sensible here
   System.out.println("ARGH, exception: " + ioe);
}
This will write MyFile.txt *somewhere* on your file system. If your running it from the command line it'll be in the directory you're in, if you're in an IDE it'll be god knows where. You can pass an absolute path if you like, such as "C:/code/work/output.txt" (forward slashes in java on windows work just fine)

JingleBells fucked around with this message at 01:25 on Aug 9, 2008

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

Startacus posted:

That works great except for some reason it doesn't add a comma every other time. So I will have a name and grade separated by a comma on one line, like it's supposed to. Then on the next line I will just have a name and the line after that, just a grade. It does that back and forth, weird.

It might be worth adding .trim() to your calls to getName() and getGrade() then, it sounds like your values may have carriage returns/new lines in it?

code:
 writer.write(Student[i].getName().trim() + "," + Student[i].getGrade().trim() + "\r\n"); 

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

Vandorin posted:

I've got a problem just compiling my code. I wrote it in class, and on those computers it ran fine, but when I try to run it on mine, it gives me the error "cannot find symbol - method drawRectangle (int, int)"

I can't get any of it to compile as I'm missing the SimpleTurtle and SimplePicture classes, what else is in the Guzdial package?

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

Anunnaki posted:

How do you do input from the console to multiple variables? In C++, you would do something like cin >> var1 >> var2;, but even my professor told me you have to just write one input command for each variable. Somehow I doubt this.

Edit: Just to clarify, I'm asking here because I haven't been able to find a clear answer with Google. :)
At uni our textbook provided a class called EasyIn, MindProd's java site has a good example of it

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

Newf posted:

OK, so more trouble from the resident java noob.

...

I get "cannot find symbol class InputMismatchException" as a compile error. It's probably obvious to you now that I don't know how to work exceptions at all, but what do I do to make this work?


Have you got an import for java.util.InputMismatchException ? It's not in the java.lang package :)

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

I'm having some trouble with various JAXP implementations and some XML I've got.
I'm using JWebUnit with HTMLUnit to test a webpage which is using AJAX to retrieve some XML from the server, the XML has empty CDATA sections in it, like so:
code:
<xml>
   <node1><![CDATA[Text]]></node1>
   <node2><![CDATA[]]></node2>
</xml>
When good old Internet Explorer receives the XML it leaves it in a DOM as above, with empty CDATA section, the javascript code then refers to the text inside this section using a hard coded path (e.g. dom.getElementsByTagName("node2")[0].childNodes[0].nodeValue -treating the CDATA node as being an actual node).

JWebUnit mimics a web-browser, including down to handling XML via AJAX - however it uses a JAXP DocumentBuilderFactory implementation to get the String into an XML object.

The XML becomes as follows:
code:
<xml>
   <node1><![CDATA[Text]]></node1>
   <node2/>
</xml>
and our tests fail as the javascript breaks

I've tried the following JAXP implementations:
Built in Java 1.6
Xerces 2.9 & 2.11
Oracle XDK
Crimson
Saxon 9.2 & 9.3 (Interestingly 9.3 has deprecated the DocumentBuilderFactorImpl and suggests using Xerces)

All of them remove the empty CDATA node.

I know the problem is actually due to the way Microsoft parse XML in Internet Explorer, but I can't change the web application code, nor can I change from JWebUnit + HTMLUnit - are there any other JAXP providers I've missed? The only resources I've found referring to this seem very out of date.

Otherwise I'm going to have to break the HTMLUnit code and manually try and add empty CDATA nodes once the String has been parsed into a Document, which won't be pretty

JingleBells fucked around with this message at 00:19 on Sep 5, 2011

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

If anyone cares I've managed to find a workaround for my XML parsing issues - I found that someone had submitted a patch to Xerces for this, but it's never been implemented.

So I built myself a copy of Xerces with the patch, and then had to fix a bug in HTMLUnit where if it's mimicing Internet Explorer it treats empty DOM Text nodes as empty nodes and uses the short form XML, and CData nodes are subclasses of DOM Text nodes - so they also get shortened.

Not sure whether to submit the bug to the HTML Unit team seeing as it only appears when using a hacked XML parser

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

wins32767 posted:

Is there a tool out there that will attach to a running Servlet container and log all of the method calls that are made? I'm trying to generate some system documentation for a framework whose developer is no longer with the team and stepping through the code manually is going to be pretty painful.

You could use AspectJ and set the aspect to match all method calls, a quick google suggests this might work.

Be careful - I did this once by setting the wrong aspect, and logged every method call in all the libraries I was using too

Adbot
ADBOT LOVES YOU

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

Jose Cuervo posted:

I have been programming in Python but now am trying to modify someones code that is in Java. Is there a reason that this person would not have used this.ID instead of ID in the getID() function? I cannot tell why they would have omitted it.

Pure code style - there's no difference as there's no other ID variable in scope.

If you have a class like this:

Java code:
public class Junction {

	private int ID;	

	public int getID() {
		return ID;
	}
	public void setID(int ID) {
		this.ID = ID;
	}
}
Then in the setID method ID refers to the parameter being passed in while this.ID refers to the private variable which the Junction object owns. Note I've had to change the case of the setID parameter as variable are case sensitive - in your example setID could easily just be:

Java code:
public void setID(int id) {
    ID = id;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply