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
pigdog
Apr 23, 2004

by Smythe

Lysidas posted:

The correct answer is IntelliJ Community Edition.
This is the correct answer. It's stable, fast, free (CE), and has good usability. It's pretty impressive how fast a proficient person can code with it. For just Java you don't particularly need the commercial version, either.

Adbot
ADBOT LOVES YOU

pigdog
Apr 23, 2004

by Smythe

MonsterUnderYourBed posted:

simple question with the JDBC hooking into MySQL.

Is there a way I can insert an entry, and at the same time return the automatically generated key that entry is now using?

I am aware that I can insert an entry, stating the automatic key should be returnable, and then get the key for the final entry in the database, but if multiple processes are accessing the database, this could return an incorrect result due to a second entry being added in before the key is returned.
Check this out: http://dev.mysql.com/tech-resources/articles/autoincrement-with-connectorj.html

pigdog
Apr 23, 2004

by Smythe

Pucklynn posted:

I'm not the one you're speaking to, specifically, but I have pretty much the same issue. My main grump with IDEs is that when I start a new project, it asks me about a million different setup options and populates files and bits of code that I just don't understand yet. Ideally I'd like to start with as minimalist a program as possible, to be sure I understand how each piece goes together. I feel like using the IDE skips over a lot of important basics of how a Java program is set up.

Java IDEs don't necessarily do any of that. Yet if you choose to create a project structure, Ant build files and whatnot, then for projects larger than Hello World it's probably a good idea anyway. A good IDE checks syntax, integrity, imports etc on the fly and lets you fix them semi-automatically, saving you a lot of javac->edit->javac cycles. Going for commandline first is kinda similar to learning to write prose by insisting on using a pencil and paper, as opposed to word processor on a computer.

pigdog
Apr 23, 2004

by Smythe

TRex EaterofCars posted:

No one smart uses struts any more. Ugh.

I am a huge fan of Grails, which is basically lipstick on Spring + Hibernate with a bunch of other cool features.

Could you explain what's wrong with Struts? (Just curious)

pigdog
Apr 23, 2004

by Smythe
See something like this: http://totheriver.com/learn/xml/xmltutorial.html . DOM is easier to use but takes up more memory and CPU (gets important if the XMLs are in megabytes of size), SAX is a bit of a bitch to use but is faster and doesn't blow up if the XML files are huge, StAX is sort of middle ground but haven't used it much yet. Don't use a regexp to parse XML, especially other peoples' XML, it will bite you in the rear end. That's what XML parsers are there for. There's a bunch of boilerplate code, but once you have that DOM object you can do cool stuff with it like search it with XPath, modify the elements, transform it with XSLT, et cetera.

edit: If the XML in question corresponds to a concrete XML schema, then see this: http://xmlbeans.apache.org/

pigdog fucked around with this message at 10:13 on Jan 25, 2012

pigdog
Apr 23, 2004

by Smythe
Um, yeah. Java is a compiled language, you need to compile the .java file to a .class file before you can use it. The Java code that's contained in the JSP files is compiled automatically by Tomcat, but regular Java files need compiling before use.

pigdog
Apr 23, 2004

by Smythe

TRex EaterofCars posted:

Ah yeah, I never run app servers in IDE's any more for that exact reason. I just do jpda and connect remotely cause it's just less headaches imo.

also: JRebel.

pigdog
Apr 23, 2004

by Smythe
Spring configuration isn't confined to XML files for some time now. If you can, then you could just instantiate your app by, say, trying to run some of Spring's own testing framework against it. It's quite useful to invest the time to understand and get your app or parts of your app up and running in Spring's own test harnesses, anyway.

pigdog
Apr 23, 2004

by Smythe

My Rhythmic Crotch posted:

What I am trying to do is find a way to ease the process of upgrading hundreds (or thousands) of XML files when a new version of an application drops. The current way it's done is:
Why would there be hundreds (thousands) of XML files?

quote:

I have an object 'occupant.' Occupant may enter a room; however, he can also leave the room(Thus, deleting said occupant from the room). Before he leaves, he must notify that he is leaving. How could I go about doing this?

in the leave method, I am just not sure how to return the object to a toString method, then set it to null.
Your class looks almost right. The question is, who do you suppose has the task of notifying, and whom? A room doesn't notify anyone, does it? In the real world, rooms are just a dumb spaces, made of bricks, who couldn't care less.

Some piece of code would have to be calling these enter() and leave() methods. Let's say it is God who indeed controls the way people and their rooms behave.

The SmallRoom and Person classes look like the Model part of the MVC pattern, the part which describes the properties and relations between concepts. Whereas God would be the Controller part, the code which creates, calls and manipulates them.

Usually, then, God would know where or whether he wants which people to go to which rooms. Rooms wouldn't, normally, have to notify him of it, because He knows best and He is the one setting them up as is.

Now, the rooms might require a spying device which would indeed notify someone else whenever God manipulates them. That doesn't necessarily happen too often, and I'm not quite sure if you really need it. Usually the rest of the code would just take it up with God.

pigdog
Apr 23, 2004

by Smythe
The problem is that he is confused.

Yes, the leave() could be implemented as
code:
public Person leave() {
   Person copyOfOccupant = occupant; 
   occupant = null; 
   return copyOfOccupant;
}
though better yet, have it simply set the occupant to null and return nothing. Add a getCurrentOccupant() method so that every method would do one thing. You could then also find out who the current occupant is, without making him leave, or making the field public.

code:
public Person getCurrentOccupant() {
    return occupant;
}

public void leave() {
    occupant = null;
}
I think it's not uncommon to be as confused when you start with OO programming. At least back in the I can remember I was, too. You're being taught that "objects are data and code which knows how to manipulate that data together" so you are like "whoa that's cool, lets put everything about SmallRooms and anything that has anything to do with SmallRooms together in SmallRoom", and then you're like "gently caress how do I communicate and react to other objects now, this is so confusing". :)

It's confusing because you'd be mixing the controller code, which would be the parts which create and destroy and manage and manipulate objects, into the objects that represent the data. Unlike what you might have first heard in a lecture, it's perfectly cool for some objects to not contain actionable code at all, except for simple setters/getters. The SmallRoom class works pretty fine as a model class that represents a room. It's important to remember that all code that works on it doesn't need to be contained in the SmallRoom class, but would fit better in, say, God or ApartmentBuildingManager class.

pigdog fucked around with this message at 08:11 on Apr 6, 2012

pigdog
Apr 23, 2004

by Smythe

My Rhythmic Crotch posted:

Because we have an incredibly large, elaborate product with scads of things to configure. Does it really make a difference?
Well, you know better. If it would make it easier for you to configure them, then you might not have hundreds of them.

quote:

Here's my take: a parameterless function that is not static but which returns nothing presumably alters the internal state of an instance in a way that is totally opaque to someone inspecting it without source (or is completely useless). It also makes the object stateful, something which is best isolated rather than spread out, because it gets harder and harder to reason about classes and their behavior the more the lifecycle is distributed.
The state it alters may or may not be meaningful (as the OP said, the interface was defined by the professor), but it doesn't mean it's necessarily bad. Maybe altering the state in an opaque manner is exactly what you'd want to do. The caller may not necessarily know who lives in the room.

quote:

Is grossly misleading.That doesn't copy the occupant, it copies the reference, and is probably best named something like "tempOccupant" instead of "copyOfOccupant" as it's temporarily there to hold a reference.
Yes it copies the reference, but in this context it doesn't make a difference. There's no need to copy by value, anyway. If you think tempOccupant sounds better then that's fine.

quote:

EDIT: See for yourself what the try-finally block does. You can use it to do things after something is returned, though I wouldn't recommend making it something you do all the time.
Good idea.

pigdog
Apr 23, 2004

by Smythe
Came across this pretty nice article on Java anti-patterns for newbies and not-so-newbies alike.

pigdog
Apr 23, 2004

by Smythe

iscis posted:

I'm having trouble implementing some methods of a doubly linked list, if anyone could point me in the right direction I'd appreciate it.
code:
public class p_02<T> extends p_02Node {

Don't use names like p_02, p_02Node, temp etc. Name things so looking at a name would tell the reader what they are.

Any particular reason you wouldn't just use LinkedList? That is, if you really need a linked list in the first place, as opposed to normal ArrayList.

pigdog
Apr 23, 2004

by Smythe
Are you sure you even need to throw an exception, rather than implementing, say, Strategy pattern to choose a different algorithm to handle different states?

pigdog
Apr 23, 2004

by Smythe

Doctor w-rw-rw- posted:

One thing that many people don't grok about Java is that programming it effectively is, essentially, computer-aided programming:

IDEA is even smarter than Eclipse when it comes to autocompleting and programming aides :)
http://blog.codeborne.com/2012/03/why-idea-is-better-than-eclipse.html

pigdog
Apr 23, 2004

by Smythe
Fair enough. IDEA does have some Android support even in the free edition, but I've no idea how it compares.

pigdog
Apr 23, 2004

by Smythe
With the caveat of not knowing the context, it doesn't look wrong at all. Are you sure you need to use foreach loops rather than say a HashSet, or something like someInstanceOfA.getSomeKindaB(someParameter).getSomeKindOfC(someOtherParameter)?

pigdog
Apr 23, 2004

by Smythe
It'd be weird for a method that's named getAge() return an animal object rather than some number representing age. :) If you'd replace getAge() with something like getCritter() then yes, you could do that. With the caveat that it's usually not nice form, as 1337JiveTurkey explained. The implementation might be something like public Animal getCritter() { Cat critter = new Cat(); return critter } where it may only use a concrete Cat object internally, but doesn't make the specific claim of returning a Cat; just some kind of Animal.

I'd guess it's not a typo, just something they would expect you to be mindful of when implementing methods.. anyhow a modern IDE would pick up and alert you of the incompatible return type right away.

pigdog fucked around with this message at 09:50 on May 10, 2012

pigdog
Apr 23, 2004

by Smythe

Aleksei Vasiliev posted:

Is there any specific idiom for "iterate over a list, run a function per object, return the result of the complete iteration"?
...

There are other alternatives, but we use LambdaJ a lot.

pigdog
Apr 23, 2004

by Smythe
Also check out Flyweight pattern if it's applicable in your context.

pigdog
Apr 23, 2004

by Smythe
It depends on the context, too. Sometimes option 1 is right.

pigdog
Apr 23, 2004

by Smythe
Get rid of the comments on obvious things, they only clutter the code without adding anything, or even create confusion where there wasn't any in case of int counter = 1; // initialize counter to zero stuff. The names in your code do a decent job of explaining what they're about, on their own.

Don't reuse variables for different contexts or purposes in a method, particularly input parameters like start in your case. while (start != end){ sounds a bit ridiculous, doesn't it? Introduce another variable, let inputs be inputs and output(s) be output(s). It's usually quite a good idea to consider inputs immutable, if possible.

intHexValue is never used.

pigdog fucked around with this message at 11:47 on Nov 6, 2012

pigdog
Apr 23, 2004

by Smythe

carry on then posted:

== only tests for reference equality. You'll want input.equals("File") for string equality.

Better yet "File".equals(input), so that in case input is null (which won't happen here but would in most code), the comparison would simply return false rather than throw a NullPointerException.

pigdog
Apr 23, 2004

by Smythe
IDEs can optionally just visually collapse and fold simple accessor methods for you, too, so at least they clutter a bit less.

pigdog
Apr 23, 2004

by Smythe
It's hard to understand the context, though it appears you're doing something wrong. Can you post the code so far and/or the text of the assignment? Are you trying to print each object's cost and volume, or the sum of the ones contained in the array?

pigdog
Apr 23, 2004

by Smythe
JUnit seems more prevalent, but from my cursory experience with TestNG, there isn't a huge lot of difference.

Mocking frameworks however can be pretty remarkably different to work with.

pigdog
Apr 23, 2004

by Smythe
You might want to wrap the DAO exceptions into custom, application-specific exceptions such as HorrificUnrecoverableException or ShobonMerelyNotSuccessfulException (not literally), so the classes down the line needn't worry about, say, any SQL-specific stuff showing up. For example if your app uses the normal DAO pattern of ApplicationClass -> SomeService -> SomeDAO, then usually the sensible exceptions the Service can throw are defined at the Service level, and DAO exceptions are wrapped into these.

From there on it's not a bad idea at all to prefer passing exceptions down the line and handling them all, if possible, in one or few locations, where it makes best sense for your application. Certainly in your case, you'd want to pass the exceptions and the try/catch loop be near the part which decides which HTTP codes to pass.

Whenever catching and rethrowing exceptions locally, never remove the original exception, but pass it along in the new exception, merely adding information to it instead of replacing or removing.

pigdog fucked around with this message at 09:44 on Feb 10, 2013

pigdog
Apr 23, 2004

by Smythe
Until then you can use LambdaJ

code:
import static ch.lambdaj.Lambda.*;
...

List<Butt> sortedButts = sort(butts, on(Butt.class).getCheekSize());

pigdog
Apr 23, 2004

by Smythe

Hard NOP Life posted:

Easiest way, copy the mysql.jar to the default java installation folder, on my CentOS server it's at /usr/java/default/jre/lib/ext
It would work, but it'd be kinda like installing everything into C:\Windows\System32. :ohdear:

The simplest if not terribly convenient way is to add the driver .jar into classpath like

java -cp mysql.jar MyApp

or append it to CLASSPATH environment variable

export CLASSPATH=$CLASSPATH:./mysql.jar
java MyApp

Any serious app or server would take care of that stuff itself in some way or another, i.e. a startup script.

pigdog
Apr 23, 2004

by Smythe

HatfulOfHollow posted:

I've been tasked with a project to produce a report on methods nearing the 64k size limit. Our application uses a lot of jsps and generated code and we've seen issues in the past where a small change in one place leads to problems in other pages that import the new code. Does anyone have any idea how I could go about determining the size of a method compiled in a class file?

:stare: You are doing it wrong.

pigdog
Apr 23, 2004

by Smythe
Sorry for the unhelpful post, I saw "64k methods" and kinda knee-jerked. If we're talking JSP's that are compiled into such monster methods, then that's another thing.

Try to have less crap stuff in your .jsp files, move the business logic to proper java classes, Javascript files to separate .js and such. While it's down to any particular JSP framework, perhaps try replacing static includes such as <%@ include file="blah.jsp" %> with dynamic like <jsp:include page="blah.jsp"/>. Anyhow, it depends a lot on your context. Could you decompile some of the .class files the JSP gets compiled into and see what takes up all the space?

pigdog
Apr 23, 2004

by Smythe

Pollyanna posted:

The superclass Items has one parameter: [amount]. The Weapons subclass of Items, however, has two parameters: [amount, power]. It inherits [amount] from Items (all Items have an amount), and innately has [power] (all Weapons have damage dealing power, but not all Items do).

Similarly, the Potions subclass only has the parameter [amount], inherited from Items. It has no inherent parameters. Certain Objects of class Potions, however, have the parameters [amount, bar, points], where [points] is how many points of which [bar] (HP or MP) is recovered upon using the item. Both [bar] and [points] are inherent parameters of the Objects Health and Mana.

On the other hand, the Object Water has no parameters either, as it is neither associated with HP or MP, nor restores any points. It has only an [amount] of Water potions held.
The [amount] doesn't really make sense and seems pretty unnecessary. A bottle of water is a bottle of water, it doesn't have or need an "amount" property. A villager might have an inventory of Item-s of which she has an amount, and we might be talking about the amount of water bottles in the whole gameworld, but the potion itself doesn't need it. [bar] and [points] are rather nebulous as well; are you sure you wouldn't a HealingPotion subclass with an amountHealed property or such?

A rule of thumb is that class inheritance is for sharing common implementations of functionality, but as far as behavior, you should take a good look at interfaces once your inheritance topology starts feeling limited and strange. A subclass can only extend one parent class, but can implement several interfaces and thus "be" many things at once. For example a Cloak and a Blacksmith can both implement Burnable and needn't have anything else in common rather than having implemented burn() or whatever the Burnable interface requires, at some point of their respective class hierarchy. Definitely read up and mess around with interfaces, they're more useful than they appear. :)

pigdog fucked around with this message at 15:34 on Sep 19, 2013

pigdog
Apr 23, 2004

by Smythe
Yeah, you already have int[] rolls so you don't need roll1 .. roll4 in the first place.

pigdog
Apr 23, 2004

by Smythe

KildarX posted:

code:
        while (valid = false);
There's your problem.


vv No it is.
Instead of comparing, you're assigning a false value to valid. Mixing up == and = is a classic mistake, most IDEs would warn you about it there.

pigdog fucked around with this message at 23:04 on Oct 3, 2013

pigdog
Apr 23, 2004

by Smythe
Regular expressions are precisely the right tool here. Just not one regular expression that attempts to match every rule.

pigdog
Apr 23, 2004

by Smythe

rhag posted:

Regular expressions are just as good here as taking out the tank to kill a fly. It will work, but it will require too much effort and will waste too much energy in the process versus what's required.
Regexes are awesome and meant precisely for this sort of thing, are really useful and a part of standard Java (as well as other languages), you really should learn to use them. They can also be very fast for more complex matching. Instead of rolling several loops you could replace the whole thing with

code:
do {
      System.out.println("Please input a password, passwords must have atleast 8 characters, one upper and lower case letter, and one digit");
      pwd = scan.next();
      if ( pwd.length() >= 8	// is at least 8 characters
 	&& pwd.matches(".*[a-z].*") // at least one lowercase
	&& pwd.matches(".*[A-Z].*") // at least one uppercase
	&& pwd.matches(".*[0-9].*") // at least one digit
      ) break;
} while (true);

pigdog fucked around with this message at 21:55 on Oct 4, 2013

pigdog
Apr 23, 2004

by Smythe

Max Facetime posted:

Those regexps don't work either.
Ah, so much for my blackboard programming. Forgot matches() in Java requires an exact match from start to finish a la /^whatever$/ in Perl.
Instead of pwd.matches("[a-z]") you'd need to do Pattern.compile("[a-z]").matcher(pwd).find() , then.

In any case, there's no reason to reinvent the wheel for matching patterns in strings.

ps: \p{Lu} and [\p{L}&&[^\p{Lu}]] instead of [A-Z] and [a-z] match these fancy Unicode strings too, if that's the requirement.

pigdog fucked around with this message at 22:03 on Oct 4, 2013

pigdog
Apr 23, 2004

by Smythe
You should never be writing Java in vim in ssh session. And what do you mean it needs to run on a specific server? You should e.g. compartmentalize the server-specific stuff in its own class or abstract class, override it for development.

pigdog
Apr 23, 2004

by Smythe
Are you sure you actually need these terabytes of data, or is it just the dataset you've used to test it against? I mean, using terabytes of data is as such a pretty intensive deal, even simply reading a terabyte from a hard disk takes hours. Are you sure your code cannot be expressed with a smaller dataset? You should make the effort to do so. Perhaps if your code merely uses some of that data for some of its input rather than it being the integral part of it, then you should look at mock versions of the component that reads them, and needn't use the database altogether.

I mean it's a really good idea to be able to run your code on your own workstation, not have it tied to peculiarities of that data on that system.

quote:

Why are you guys still using Eclipse?? IntelliJ IDEA shits all over it.
I agree, but that's a religious argument. :shrug: Eclipse is better for Android I guess.

Adbot
ADBOT LOVES YOU

pigdog
Apr 23, 2004

by Smythe

Sagacity posted:

I'm about to start work on my first non-trivial Java project in a long time. Coming from a C# background most concepts are thankfully similar, but I still need to make a few choices.

* I want to use IntelliJ as the IDE, since I like Jetbrains stuff from the C# world. Sane?
Quite.

quote:

* Maven or Gradle? Maven is more verbose but it integrates pretty seamlessly with IntelliJ. Do you really spend so much time editing these files that Gradle would be a plus?
Matter of preference and nature of your project.

quote:

* Does anyone still use Spring in a non-ironic way for DI (vs Guice), Data (vs Hibernate, Morphia, whatever) and Security (vs Shiro or roll-your-own)?
Of course.

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