Search Amazon.com:
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 $3,400 per month for bandwidth bills alone, and since we don't believe in shoving popup ads to our registered users, we try to make the money back through forum registrations.
  • Post
  • Reply
Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



ComptimusPrime posted:

Thank you. That did the trick. And I'm sure that tidbit will help me a whole hell of a lot in the future.

Just so that you know, if you really do need that kind of precision in your calculations checkout the BigDecimal class.

Adbot
ADBOT LOVES YOU

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



The reason you won't see too many operations with floats is because of the constant casting you have to do since Java treats all literal floating point numbers as doubles.

If you mix a float with a double it will return a double because if will convert the float to a double to do the operation. But it won't convert the result back down to a float because of the possible loss of precision, so you have to do it manually with a cast.

Here's a quick example. If you just write 2.5 Java treats it as a double, you have to either cast it to a float or append an f to the end 2.5f. If you aren't careful you can do things like like this.

code:
float x = 2.5f;
float y = x / 5.0;
Luckily most of the time the compiler will throw an error or warn you, but it can be a huge wtf the first time you deal with it.

Here is some more reading if you are interested. http://mindprod.com/jgloss/floatingpoint.html

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



karuna posted:

Ok I think I'm understanding you, is there an issue with casting the Math.random()?

When I change

code:
Thread t = new PopulateArray(list, 0 , 50 , (int)Math.random()* 100);
to

code:
Thread t = new PopulateArray(list, 0 , 50 , 2);
The output of the whole thread is 2 but it is only meant to apply to the first 50 elements?
Your cast is truncating the Math.random() to a 0 then multiplying by 100.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



In one of my classes they had us make a multi threaded expression solver. It was really neat because it entailed finding all the parts of the expression that could be solved independently, then solving them, and then putting it all back together again.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



I was going to say that I've never found a use for a LinkedList over an ArrayList, but then I remembered that it implements the queue and deque interfaces.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Compiling from the command line is loving annoying. I suggest you use an IDE or learn to use Ant/Maven to do your compiling for you.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



LOL no, but it only gets worse as your project grows in size and I feel it's valuable to learn it sooner rather than when you really need it and are pressed for time.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



code:
Connection connection = NewConnection.getInstance();

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



I have no loving clue how that helps you.... that just simulates rolling a dice.

Use this

InternetJanitor posted:

Random randy = new Random();
double value = ((randy.nextBoolean() ? 1 : -1) * (randy.nextDouble()));

Or this which is probably what he would expect.

InternetJanitor posted:

double value = ((Math.random() * 2) -1);

e: I just remembered I had this exact same assignment but it was on problem on our test and no one had a clue how to solve it.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Parantumaton posted:

One gotcha I'd like to mention, Entry contains apparently only weak references to the key and value it holds so the following code may throw NPE if you get a bit unlucky with GC:

This is the first I've ever heard of these, how interesting.
http://weblogs.java.net/blog/enicho...standing_w.html

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Hey guys I have a question about GlassFish that I can't really find the answer to. I just want to clarify if it's necessary to use a load balancer with a cluster.

What happens when I try to just access the cluster without a load balancer? Or is that not even possible, I would have to specify a specific server?

In the case that it is required, what software load balancer would you recommend?

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



HondaCivet posted:

Quick noob sorta question: Is there a way to compare strings that is case AND space insensitive? Like something that would say "ooga booga" == "OOGA BOOGA" == "oogaBooga" is true?

I don't know what you are doing but maybe strip all the whitespace characters from the string and convert to lower case.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Try just Transport t in.readObject();

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Mustach posted:

code:
LinkedList<ArrayList<String>> result = new blah blah blah;
for(ArrayList<String> l : sourceList)
  result.add(new ArrayList<String>(l));

That's not a deep copy.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



How are you actually sending the object to the servlet?

If you are doing it from an HTML formm you pass each one of the fields in the object as an http parameter and then get them back out using the getParameter method of the request object. This is obviously tedious and is one of the main reasons you use a framework to do this mapping automatically.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



mister_gosh posted:

Ok, after trying for 8+ hours, I thought I better try here. I have two .war files which reference a couple of external jars in a Tomcat application.

I keep getting java.lang.NoClassDefFoundError: com/neva/COMException

Which, I believe, means it is not finding com.neva.COMException and likely none of the other external jars.

So, tried a number of attempts to get my war to read it, each one more desperate than the next:

1. modified setclasspath.bat in tomcat to include my jars in the classpath
2. modified catalina.properties (same as above)
3. deleted tomcat work directory
4. numerous machine restarts
5. numerous tomcat service restarts (after each configuration change)
6. put external jars directly in java home and next to rt.jar
7. put the jars in numerous and countless locations in the tomcat area including webapps/ROOT/lib
8. modified war's manifest to specify a classpath (and was careful to add the required end of line)

Nothing seems to work, and I'm very frustrated and would be very grateful if anyone has any ideas. Thanks!

Try putting the jars in the tomcat/lib folder.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



epswing posted:

mister_gosh said he's got two war files, so two web apps. Isn't MEAT TREAT's suggestion exactly what tomcat/lib is for (ie multiple webapps having access to the same set of jars)?

Exactly, if it's a common library that you are going to share with multiple applications you should place it there. This only becomes a problem when you need two different versions of the same library. But really I've never had a problem with deploying my wars with a lib directory, I believe the application server will use those first which will override whatever is in the tomcat/lib folder.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Someone on my team was fooling around with maven for that, but it turns out that that's something best suited for ant.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



I kind of am, that sounds like something I'm about to do

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



OddObserver posted:

Your code wasn't "imperfect" --- it has the sort of indentation that's at most acceptable in the first introductory course and only because the grader is a softy.
If you really produce code like this, and it's not just some weird copy-paste artifact, I weep for people who have to work with you.

In Netbeans ALT-SHIFT-F will make you happy.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Magicmat posted:

I'm trying to figure out the best way to share an Eclipse project. The folder is on a shared drive (shared via Dropbox), with one computer on Mac OS X and the other using Windows 7.

When I try just pointing Eclipse to the folder as the workspace to open, I get bunch of "file not found" errors, and an empty project.

I'd rather not put the project into version control right now since it's just a small, personal project at the moment. Also, my VCS of choice (Git) kinda sucks on windows.

Use mercurial with tortoisehg if you still want a decent DVCS on windows. There isn't any excuse.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



danishcake posted:

A quick question from a C++ guy who is dipping his toes into Java. If I have two preallocated objects, is there a convenient way to do a deep copy of one into the other, avoiding allocating a new one? I really want something a bit like clone() but with an existing target.

If you are not opposed to using an external class the Apache Commons BeanUtils class has the copyProperties method that does what you want. But that if you have non primitive fields inside, it will probably just copy the references and not do a deep copy like you would expect.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Which profiler do you guys recommend for remote J2EE applications?

I'm really interested in viewing which queries are taking the longest to return when the system is under heavy load. I have a few stress tests setup that I'm going to run from like 10 machines and each one is going to simulate like 50 clients. I know it's not enough to kill the server, but I hope to see some interesting data.

I also need to be able to save the data that it generates to later review it.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



This isn't tested, but this should get you going:
code:
        int randomNumbers = 100;
        int[] generatedNumbers = new int[randomNumbers];
        int[] most = new int[6];
        Random generator = new Random();
        for (int i = 0; i < randomNumbers; i++){
                int number = generator.nextInt(6);
                most[number]++;
                generatedNumbers[i] = number+1;
        }

        int firstSix = 0;
        int number = 0;
        for (int i = 0; number != 6 && i < randomNumbers; i++){
                number = generatedNumbers[i];
                firstSix = i;
        }
        List mostOcurrences = Arrays.asList(most);
        Collections.sort(mostOcurrences);
        int mostOcurred = (Integer)mostOcurrences.get(0);

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Zaxxon posted:

well then what's the normal way to organize your libraries?


here is what I'm doing (on a windows machine.)

with C I just put all the libraries in a folder called libraries. I've tried continuing this tradition with java, so I put the jar files in a javalibs directory and when I want to import from them I compile with the command:

code:
javac -cp "./;c:\javalibs\whatIwant.jar;..."
then when I run whether I put that classpath in or not it whines about not being able to find main.
Most professional developers use some tool and don't compile or run by hand, especially in large projects. I recommend you just use an IDE like Eclipse or NetBeans (which use ant) if you just want it to work. It's useful to know how this all works behind the scenes, but how much time are you willing to spend on dicking around with it. If you feel strongly about not using an IDE then at least learn how to use ant.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Jewbert Jewstein posted:

Doing more Google Maps work with the site I'm making and now it appears that I need to embed a dynamic map to it instead of a static one. With the static map I was able to just embed a browser and send it a url.

Example:
GoogleMap = new Embedded(GoogleUrl);
GooglePanel.addComponent(GoogleMap);

Now I'm grabbing some coordinates and putting them in an html file. How do I make my embedded browser display this file using java?

For example, I want the website generated from this code to be displayed in the frame:
code:
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<style type="text/css"> 
  html { height: 100% }
  body { height: 100%; margin: 0px; padding: 0px }
  #map_canvas { height: 500px;width: 400px }
</style> 
<title>Google Maps JavaScript API v3 Example: Map Simple</title> 
<script type="text/javascript"> 
  function initialize() {
    var myLatlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  }
  
  function loadScript() {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
    document.body.appendChild(script);
  }
  
  window.onload = loadScript;
</script> 
</head> 
<body> 
  <div id="map_canvas"></div> 
</body> 
</html>
Disclaimer: I've never used javascript before. Any help would be appreciated!

Javascript isn't Java

You want to ask in the Web Design/Development Small Questions

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Elos: are you using OpenJDK or the Sun JDK on Linux?

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Elos posted:

That method just loops trough an array asking the tiles to draw themselves. I don't have any real reason for drawing it constantly besides wanting to see how it's done and hell, I might want to make it a pseudo-realtime like Diablo or something.

I'm doing all the transparency and color mask stuff before the gameloop so it just draws simple 16x16 images on the screen and checks for key events. There must be something strange going on in the linux JRE.

EDIT: Is there an easy way to find out which method is hogging all the CPU?

Use a profiler, any new JDK will have one included. You can also try JProfiler or Yourkit.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



I think the solution to that is to store it as a timestamp which MySQL stores internally as the number of miliseconds since Jan 1, 1970. Then when you ask it for the time it will return it untouched, and in your Java program you can apply the correct offset.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Spring is a web framework, and Swing will be supported for a long time to come. The SwingLabs has some cool stuff that you can use, but I don't see too many people using it in the future for any serious desktop applications.

Java FX was supposed to be this awesome thing that would save desktop Java applications but that didn't pan out. My opinion is that Java desktop applications are dead, and if not that at least a second class citizen on the platform.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



You need to use the certificate you created in your client and add it to the truststore for the SSLConnectionFactory or whatever it's called before trying to connect to the server. Or you can disable the check for the server's certificate but that's not a good idea in the real world.

Oh also make sure that your certificate includes your hostname in the common name field.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Flobbster posted:

What's really fun is teaching students in a second-semester Java data structures class the proper way to implement equals and hashCode, especially when the concept of hash tables hasn't been introduced yet so a hash code seems pointless to them. (Why teach hashCode without mentioning hash tables? If we're teaching them to override equals, it's better that we mention hashCode at the same time since the two are so closely related.)

I saw so much of this last semester:

code:
public boolean equals(Object other)
{
    return hashCode() == other.hashCode();
}


Some students at least understood that you could use the hashcodes to short-circuit for inequality but that it doesn't say anything about equality, but even that makes my skin crawl a little bit for some reason.

I always thought the contract for hashCode was that it had to match the same things that equals did, but after reading the spec that's not the case. God drat that's a terrible way to introduce subtle bugs.

That code is still inexcusable, they should at least check for nulls and that's its the same type!

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



The real solution is to have your model live outside the scope of that method so that when you refresh the model the JSpinner updates automatically.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



fletcher posted:

While we're on the topic of concurrency, I discovered ExecutorService the other day and it is awesome. Made it a piece of cake to make my little utility app multi-threaded, and now it runs 8x faster.

Yes it is awesome, I used it to test and reproduce a race condition in another part of my code.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



I might be completely wrong on this but don't the newer JDBC drivers deffer the execution until you actually start scrolling the resultset?

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Aleksei Vasiliev posted:

Quick question about HashSets (which are backed by HashMaps):

The doc says "This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time." Is it possible for the iteration order to change while I am iterating? In other words, is it possible for me to run into duplicates of one item and not see another, or am I fine with a HashSet if I don't care about order?

No multithreading in my code.

I'm pretty sure the only way it could change while you're iterating is if you add or remove elements in another thread. Even then it would throw a ConcurrentModificationException. With a single thread it will iterate as you expect. The warning means that after a bunch of inserts into the HashMap the next time you iterate it it might have changed the order of the elements.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Parantumaton posted:

"We're an Apache project, therefore we must use Apache Commons Logging!" would be an excellent example of that.

Just no.

slf4j or

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Right but you're not an Apache Project. That's who my comment was directed at.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



Because a lot of Apache projects already switched.

quote:

Apache Archiva
Apache Directory
Apache FTPServer
Apache Geronimo
Apache Graffito
Apache Jackrabbit
Apache Mina
Apache Qpid
Apache Sling
Apache Solr
Apache Tapestry
Apache Wicket

Adbot
ADBOT LOVES YOU

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean



covener posted:

Seems fallacious, different people on different projects use different logging. Who could possibly give a gently caress?

I gave a gently caress when I had to include 5 projects and each one used a different logging API. Thankfully now I've consolidated all of that poo poo using slf4j adapters and redirect it to whatever my container uses.

My point was that now that slf4j exists there isn't any reason to use the commons logging API in new projects. Parametrized logging is loving awesome and you don't have to deal with the classpath issues of the JCL.

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