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
Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
The list of new features is here. The stuff from Coin that made it in seems pretty nice, and I'm interested in playing with Gervill, a new audio synthesizer.

The Coin features include:
    * Strings in switch
    * Binary integral literals and underscores in numeric literals
    * Multi-catch and more precise rethrow
    * Improved type inference for generic instance creation (diamond)
    * try-with-resources statement
    * Simplified varargs method invocation

Adbot
ADBOT LOVES YOU

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
I can't wait to see some real benchmarks with invokedynamic in Scala, Groovy, etc.

zeekner
Jul 14, 2007

Internet Janitor posted:

I'm probably going to regret posting this, but the more I think about packing stuff into longs the more I think it might not be that bad:
code:
public class Vectors {

	static final int COMPONENT_MASK = (1 << 22)-1;
	
	public static long vector(int x, int y, int z) {
		return ((x & COMPONENT_MASK) << 42) |
		       ((y & COMPONENT_MASK) << 21) |
		        (z & COMPONENT_MASK);
	}
	
	public static int x(long v) { return (int)((v >> 42) & COMPONENT_MASK); }
	public static int y(long v) { return (int)((v >> 21) & COMPONENT_MASK); }
	public static int z(long v) { return (int)(        v & COMPONENT_MASK); }
	
	public static long add(long a, long b) {
		return vector(x(a) + x(b), y(a) + y(b), z(a) + z(b));
	}

	public static long scale(long a, int b) {
		return vector(x(a)*b, y(a)*b, z(a)*b);
	}

	public static int dot(long a, long b) {
		return x(a)*x(b) + y(a)*y(b) + z(a)*z(b);
	}
}
You'd have 21 bits of precision per component, which is actually enough for a lot of things. Then just import all these utility methods statically and you're set. I think I need to stop programming in assembly for a while.

There's also ByteBuffer, which is really nice for packing various data formats into an array of bytes.

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

Aleksei Vasiliev posted:

Java 7 was just released.

Give me closures or give me death. Don't care about Java 7 since they pushed that back.

The Javadocs don't look like rear end that came out of the mid-1990s web though, that's nice.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."

Geekner posted:

There's also ByteBuffer, which is really nice for packing various data formats into an array of bytes.

ByteBuffers are really spiffy, but remember that the original problem involved avoiding the creation/destruction of objects. Longs are primitive and passed on the stack, but an array would need to be heap allocated.

...Unless you're suggesting you have a couple big arrays and essentially write your own malloc() for it, using ByteBuffers as a way to conveniently unpack different types from the arrays.

Paolomania
Apr 26, 2006

TRex EaterofCars posted:

I can't wait to see some real benchmarks with invokedynamic in Scala, Groovy, etc.

I'm betting that the effects of invokedynamic are going to be pretty exciting.

Brain Candy
May 18, 2006

Java 7 fixed some bugs

code:
public class Foo<X extends Foo<X>>
{
  private final int num;

  public boolean compileError(X x)
  {
    return this.num == x.num; //You can't access a private field here! Explosions!
  }

  private int what(Foo<?> foo)
  {
    return foo.num;
  }  

  public boolean totallyOkay(X x)
  {
    return this.num == what(x);
  }
}

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Brain Candy posted:

Java 7 fixed some bugs

The also introduced some bugs, if your code has loops in it.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.

Jabor posted:

The also introduced some bugs, if your code has loops in it.
The bug was actually in Java 6 but the optimization was disabled. Java 7 enabled it by default.

Max Facetime
Apr 18, 2009

Brain Candy posted:

Java 7 fixed some bugs

Haha, I got the impression from reading the note for this change that it was easier to change the compiler than rewrite part of the specification.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Jabor posted:

The also introduced some bugs, if your code has loops in it.

What bug is that?

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.

MEAT TREAT posted:

What bug is that?
http://www.lucidimagination.com/sea...olr_with_java_7

Exxon Hess
Nov 12, 2008

TRex EaterofCars posted:

I can't wait to see some real benchmarks with invokedynamic in Scala, Groovy, etc.

I don't know if Scala will be able to take advantage of it, since it's not a dynamically typed language, but Groovy, Rhino, and JRuby will probably get a significant bump, as Hotspot will get a much better shot at JITting.

zootm
Aug 8, 2006

We used to be better friends.

Exxon Hess posted:

I don't know if Scala will be able to take advantage of it, since it's not a dynamically typed language, but Groovy, Rhino, and JRuby will probably get a significant bump, as Hotspot will get a much better shot at JITting.
Scala can probably use them for structural types, and I wouldn't be surprised if there were some type things you can do in Scala but not in Java that would be benefit, but in general you're right.

1337JiveTurkey
Feb 17, 2005

Scala's probably going to get more use out of the MethodHandles as a way to handle eta expansion without generating new anonymous classes. That's also the direction that Java 8 looks to be going with lambda definitions with some way to make a single MethodHandle with a given MethodType impersonate an interface or abstract class with a single abstract method of the same MethodType.

Also, the escape analysis being enabled by default in Java 7 (and I think Java 1.6u23 and up as well) will probably have a bigger impact on Scala programs. Being able to stack allocate millions of tiny objects with short lifespans turns them from cheap into effectively free. This has caused some (highly contrived) microbenchmarks to run several times faster in tests but the average Scala program's still going to benefit somewhat since it does allocate a lot of the aforementioned tiny objects with short lifespans.

Icesler
Jul 7, 2005
I just started my attempt at learning to program. I am having a problem with one of my exercises in my book though. I am trying to have a console application that asks for an input of either U or L. Depending on what is entered, it should display the alphabet in either upper or lower. This is what I have under my getChoice method but its not working.
code:
	public static char getChoice()
	{
		char choice;
		Scanner scan = new Scanner(System.in);
		System.out.println("Display the alphabet in uppercase or lowercase? \n Enter U or L:");
		choice = scan.nextChar();
		return choice;
	
	}
The problem is at choice = scan.nextChar(). The compiler says it is undefined for that type of scanner.
Can someone help me out? I suck at this.

Tamba
Apr 5, 2010

quote:

The compiler says it is undefined for that type of scanner.
Can someone help me out? I suck at this.
That's because Scanner really doesn't have a nextChar()-Method
http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html

If you still want to use a Scanner to do this, use scan.next() and then get the first char from that String
code:
scan.next().charAt(0);
You could also use a Reader to read System.in, those read single characters.
http://download.oracle.com/javase/6/docs/api/java/io/InputStreamReader.html

e: or you could just work with Strings instead of characters

Tamba fucked around with this message at 22:22 on Aug 2, 2011

HFX
Nov 29, 2004
I'm actually happy to here about NIO.2. I just implemented a project that relies on concurrent and nio heavily. Now if I just had closures I would be in heaven.

Blacknose
Jul 28, 2006

Meet frustration face to face
A point of view creates more waves
So lose some sleep and say you tried
I have a loving retarded question. I have a double, lets aay the value is 160.0, and I need it to be formatted as 160.00 (yeah, the cardinal sin of using doubles for currency, don't blame me, it wasn't my idea).

I have tried Math.round, DecimalFormat etc and I'm pretty well out of ideas. There has to be a way of forcing it to 2dp and staying that way whilst also keeping it as a double but I can't for the life of me think what and it's now starting to really gently caress me of. Ideas?

baquerd
Jul 2, 2007

by FactsAreUseless

Blacknose posted:

I have a loving retarded question. I have a double, lets aay the value is 160.0, and I need it to be formatted as 160.00 (yeah, the cardinal sin of using doubles for currency, don't blame me, it wasn't my idea).

I have tried Math.round, DecimalFormat etc and I'm pretty well out of ideas. There has to be a way of forcing it to 2dp and staying that way whilst also keeping it as a double but I can't for the life of me think what and it's now starting to really gently caress me of. Ideas?

code:
DecimalFormat currencyFormat = new DecimalFormat("##0.00");

Blacknose
Jul 28, 2006

Meet frustration face to face
A point of view creates more waves
So lose some sleep and say you tried
Already tried that and it returns a string, I need to end up with a double, same as when I started. I'm not actually 100% convinced it's possible.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Blacknose posted:

Already tried that and it returns a string, I need to end up with a double, same as when I started. I'm not actually 100% convinced it's possible.

A double is just a number. "160.0" and "160.00" are both strings, that represent a number.

There is no such thing as a double that has a particular formatting. It's just a double. A collection of bits that represent a number. You decide when you print it out how you actually want to display it.

You are asking a question that has no answer - not because what you want to do is impossible, but because it's meaningless.

What are you actually trying to achieve?

baquerd
Jul 2, 2007

by FactsAreUseless

Blacknose posted:

Already tried that and it returns a string, I need to end up with a double, same as when I started. I'm not actually 100% convinced it's possible.

As a double, there is no distinction between 160.0 and 160.00. Until the number is put into a String there isn't a way to "format" it.

code:
if (160.0d == 160.00d) {
  System.out.println("Math is a bitch");
}
Edit: maybe you mean you want to truncate any additional places?

code:
DecimalFormat currencyFormat = new DecimalFormat("##0.00");
myDecimal = Double.valueOf(currencyFormat.format(myDecimal));

baquerd fucked around with this message at 13:55 on Aug 3, 2011

Blacknose
Jul 28, 2006

Meet frustration face to face
A point of view creates more waves
So lose some sleep and say you tried
Yeah I was really scraping the bottom of the barrel by asking. It has to remain as a double, which obviously is a binary representation of a value, whih is then passed to an external api (jxl I believe) which then sticks it in an excel file. I didn't know if there was a way to attach formatting data to a double without using a string. The client is almost certainly going to want a numeric value with formatting which as far as I can tell just isn't possible.

It's one of those situations where you're basically boned but I thought someone might know some miraculous way of doing it that defies the laws of coding.

Ensign Expendable
Nov 11, 2008

Lager beer is proof that god loves us
Pillbug
If it gets stuck into an excel spreadsheet, you can set the formatting for those cells to have two decimal places displayed programmatically through Excel, but it's a giant pain in the rear end to get Office Interop working with Java in the first place, let alone actually doing useful things with it.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
If it's going into Excel, why isn't a string suitable?

As far as I'm aware Excel treats a cell containing the string "160.00" pretty much the same as one containing the number 160 when it comes to calculations?

epswing
Nov 4, 2003

Soiled Meat

Blacknose posted:

I didn't know if there was a way to attach formatting data to a double without using a string. The client is almost certainly going to want a numeric value with formatting which as far as I can tell just isn't possible.

I don't think you're getting it. To "attach formatting data to a double" is senseless. You can round the value, or floor/ceil it, but numbers don't have "formats".

Strings do, though, so when you print your double, you can say "print two decimal places" so it looks like what you want.

If you want some way of keeping this information "with" the double, you could make a little class...

code:
class FormattedDouble {
    
    public double value;
    public DecimalFormat format;
    
    public FormattedDouble(double value, String format) {
        this.value = value;
        this.format = new DecimalFormat(format);
    }
    
    public String toString() {
        return format.format(value);
    }
}

FormattedDouble d = new FormattedDouble(42.5, "##0.00");
d.toString(); // the string "42.50"
d.value // the double 42.5

baquerd
Jul 2, 2007

by FactsAreUseless

epswing posted:

If you want some way of keeping this information "with" the double, you could make a little class...

code:
class FormattedDouble {
    
    public double value;
    public DecimalFormat format;
    
    public FormattedDouble(double value, String format) {
        this.value = value;
        this.format = new DecimalFormat(format);
    }
    
    public String toString() {
        return format.format(value);
    }
}

FormattedDouble d = new FormattedDouble(42.5, "##0.00");
d.toString(); // the string "42.50"
d.value // the double 42.5

He's then got to code support for this into JXL though, that would be "fun".

Blacknose
Jul 28, 2006

Meet frustration face to face
A point of view creates more waves
So lose some sleep and say you tried

Jabor posted:

If it's going into Excel, why isn't a string suitable?

As far as I'm aware Excel treats a cell containing the string "160.00" pretty much the same as one containing the number 160 when it comes to calculations?

The client wants a numeric field in the spreadsheet for the software that will then read the excel file. Not my choice unfortunately.



epswing posted:

I don't think you're getting it. To "attach formatting data to a double" is senseless. You can round the value, or floor/ceil it, but numbers don't have "formats".

Trust me I get it; I know they don't. That's why I said attach formatting to it as opposed ot 'format it'. I could probably have worded it better however.

I'm not about to start pulling JXL apart, the client is just going to have to live with either a text field in excel or no dp, their choice.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
Doesn't JXL have the concept of a cell format? I mean, that's basically the "right way" to solve this problem.

Blacknose
Jul 28, 2006

Meet frustration face to face
A point of view creates more waves
So lose some sleep and say you tried
I thought the JXL formatters only dealt with fonts, borders etc but turns out that hidden away is the ability to format numeric cells. To get 2 decimal places on a numeric value use:

code:
WritableCellFormat floatFormat = new WritableCellFormat(NumberFormats.FLOAT);
and you should be golden. If I knew more about JXL or excel its self that would probably have taken 2 minutes, not 2 hours, but never mind.

Freeze
Jan 2, 2006

I've never seen it written so neatly

How do I install a new package for java? I'm trying to create a plugin for OSS (open search server) and it won't compile because it's missing a package. I set my java classpath in my bash_profile file and moved what I think is the package there (these files, but compiled to .class : http://opensearchserve.svn.sourceforge.net/viewvc/opensearchserve/trunk/src/java/com/jaeksoft/searchlib/index/). I still get an error when I attempt to compile.

The missing package is com.jaeksoft.searchlib.index, so in my classpath I have com/jaeksoft/searchlib/index/<.class files>. I also have basically no idea what I'm doing in both linux and java so please tell me that's wrong and how to set this up correctly.

Contra Duck
Nov 4, 2004

#1 DAD
If the class files are located at <BASE_LOCATION>/com/jaeksoft/searchlib/index you want to have <BASE_LOCATION> on the classpath, not the whole thing.

Freeze
Jan 2, 2006

I've never seen it written so neatly

Contra Duck posted:

If the class files are located at <BASE_LOCATION>/com/jaeksoft/searchlib/index you want to have <BASE_LOCATION> on the classpath, not the whole thing.

I had that set correctly, it looks like I just had to restart for it to actually update. I don't know why I didn't try that earlier but at least it works now.

mister_gosh
May 24, 2002

I have a Spring/JMS/ActiveMQ/Tomcat question:

Our group has multiple projects/wars to deploy to a tomcat install. We want a single ActiveMQ install with a couple of queues set up in it. Submissions to the queues will come from the various projects.

Deploying the ActiveMQ web console appears to automatically start the broker up, but it shouldn't necessarily be required to run ActiveMQ. In fact, the ActiveMQ Manning book doesn't even really mention the web console.

Anyways, I'm trying to figure out how the queue setup should work. Should I develop a separate project/war which manages the broker startup and other configuration issues and then have each Spring project submit to the brokerURL independently?

Abrahamsen
Jul 1, 2009
Problem:
I'm in the beginning phase of creating my first game using Java with JLWGL and Slick. My problem is sort of generic in programming nature.
I am building an entity/component system and I would like to have a list of entity templates with associated components, so that I can have just one method for creating entities with the parameter being the ID of the entity template.
Pseudo code example:

code:
entityManager.createEntity("LIGHT_TANK2");
this should find the template with matching ID "LIGHT_TANK2" and create an entity with the right components added.

The question: How do I make these different entity templates efficiently, and without giving the user the ability to edit them?

Please note: I am a beginner programmer, but catching on fast - some solutions might be too advanced for me.

Edit: Do I use enums? I have no experience with those so I don't know!

Abrahamsen fucked around with this message at 21:21 on Aug 14, 2011

Max Facetime
Apr 18, 2009

The simple if-else-if -chain (or string-switch with Java 7) gives plenty of flexibility:

code:
static Entity forID(String id) {
  if("LIGHT_TANK2".equals(id)) {
    return Entity.newLightTank2();
  } else if(...
}

static Entity newLightTank2() {
  return new Entity(Component.newHealth(100), ...));
}
Using static methods rather than constructors allows you to add subclasses and special cases without affecting other entities.

Enums have a quite rigid structure in comparison and if what you want to do doesn't fit it exactly they can turn into a huge mess.

def snow leppard
Sep 12, 2010

Is it possible to have a java program listen for global keypresses? For example have the program running while browsing the web or something and have the program do something when a certain key is pressed. The whole time the java program not having focus. Is this possible in java?

baquerd
Jul 2, 2007

by FactsAreUseless

Clank posted:

Is it possible to have a java program listen for global keypresses? For example have the program running while browsing the web or something and have the program do something when a certain key is pressed. The whole time the java program not having focus. Is this possible in java?

You would have to also implement an OS level virtual machine in the same program and so basically the answer is no for all practical purposes.

Edit: or the native interface apparently. At any rate, Java's not a great choice for a keylogger.

Adbot
ADBOT LOVES YOU

Tamba
Apr 5, 2010

Not with just Java. But here's a library that should do what you want:
https://github.com/tulskiy/jkeymaster

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