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
magnetic
Jun 21, 2005

kiteless, master, teach me.
Have two css classes for the Nav links;

code:
there { text-decoration: none;}
here {text-decoration: underline;}
In the header include:
onload
Set all links cssClass="there"
look at the current page and which ever link it matches set the cssClass="here"

I am sure there is a better way, but that jumped out at me.

Adbot
ADBOT LOVES YOU

sonic bed head
Dec 18, 2003

this is naturual, baby!

magnetic posted:

Have two css classes for the Nav links;

code:
there { text-decoration: none;}
here {text-decoration: underline;}
In the header include:
onload
Set all links cssClass="there"
look at the current page and which ever link it matches set the cssClass="here"

I am sure there is a better way, but that jumped out at me.

You mean doing it in javascript? I would rather there be no flicker or anything like that. I feel like there should be a way to standardly pass a bean in the scope of the parent page to the code in the included page, but I can't figure out how to do that. Whenever I try to use <bean:define> in the parent page and use it in the included page, I get errors saying that the bean does not exist in the included page. I thought that <jsp:include>s just basically copied and pasted the text into the place where the tag is used, but I guess it doesn't.

zootm
Aug 8, 2006

We used to be better friends.

sonic bed head posted:

You mean doing it in javascript? I would rather there be no flicker or anything like that. I feel like there should be a way to standardly pass a bean in the scope of the parent page to the code in the included page, but I can't figure out how to do that. Whenever I try to use <bean:define> in the parent page and use it in the included page, I get errors saying that the bean does not exist in the included page. I thought that <jsp:include>s just basically copied and pasted the text into the place where the tag is used, but I guess it doesn't.
No, they don't. That's what the include directive does but the directive stuff is very old-school and sucks a whole lot; don't use it.

<jsp:include> can take arguments, though; so you can do something like this:
code:
<jsp:include page="nav.jsp">
  <jsp:param name="page" value="mortgages" />
</jsp:include>
These will appear as request parameters on the included page. Then you can just emit a different class depending on the current page/nav entry.

For what it's worth though JSP is probably the worst templating engine ever (other than perhaps "PHP", to which it is very similar), the XML-based version makes it only marginally better, and this sort of pain will haunt you until you stop using it.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

zootm posted:

No, they don't. That's what the include directive does but the directive stuff is very old-school and sucks a whole lot; don't use it.

<jsp:include> can take arguments, though; so you can do something like this:
code:
<jsp:include page="nav.jsp">
  <jsp:param name="page" value="mortgages" />
</jsp:include>
These will appear as request parameters on the included page. Then you can just emit a different class depending on the current page/nav entry.

For what it's worth though JSP is probably the worst templating engine ever (other than perhaps "PHP", to which it is very similar), the XML-based version makes it only marginally better, and this sort of pain will haunt you until you stop using it.
While JSP by itself is definitely poo poo to use, do you really think JSP with JSTL and EL is that bad?

zootm
Aug 8, 2006

We used to be better friends.

TRex EaterofCars posted:

While JSP by itself is definitely poo poo to use, do you really think JSP with JSTL and EL is that bad?
Yeah. It bugs the crap out of me. JSTL and EL make it infinitely better, though, especially once you can externalise your own stuff out into custom taglibs.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

zootm posted:

Yeah. It bugs the crap out of me. JSTL and EL make it infinitely better, though, especially once you can externalise your own stuff out into custom taglibs.

Can I ask what you prefer using?

zootm
Aug 8, 2006

We used to be better friends.

TRex EaterofCars posted:

Can I ask what you prefer using?
Rails :dance:

In seriousness though, most other things are better, although I've not too much experience with the Java ones. I hear really good things about Tapestry and Wicket, though, and I quite liked Lift's templating the last time I played with it, although that's Scala rather than Java.

zootm fucked around with this message at 20:15 on Oct 17, 2008

LakeMalcom
Jul 3, 2000

It's raining on prom night.
Wicket is excellent. I haven't personally used Tapestry, but in my opinion, Wicket is more community driven. Tapestry is controlled by just one guy. And I hear he's a douche.

Moto42
Jul 14, 2006

:dukedog:
I'm having a very newbie problem with NetBeans.
I'm learning Java, and have just reached the point in my book that discusses the ImageIcon class.
The following is the code I'm typing in:
code:
public static void main(String[] args) {
   JFrame frame = new JFrame();
   ImageIcon icon = new ImageIcon("pumpkin.jpg");
   JButton button = new JButton(icon);
   frame.add(button);
   frame.setVisible(true);
}
The button shows up just fine, but it's blank. When I replace "new JButton(icon)" with "new JButton("String goes here")" the words "String goes here" comes up just fine as well.
What I think is happening, is that I'm not putting the file "pumpkin.jpg" in the correct location. I've been putting it in the src folder, and I tried putting it in the same package as main.java but it still doesn't show up.

I guess what I'm asking is. Where should I stick this?

oh no computer
May 27, 2003

Try it using the full path, like "C:\\Images\\pumpkin.jpg" (or whatever) if you're on Windows.

I think if you don't it needs to be in the same directory as the .class file (bin?)

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
If you just give a filename like that, it'll be interpreted relative to the process current directory, which is determined in some system-dependent way. If you're launching from an IDE, it should be one of the standard options, and it probably defaults to the project directory. If you're running from a command line, it's whatever directory you're in when you launch Java.

If you want to fetch something relative to the path that a class is stored in, use Blah.class.getResource(filename), which will give you a URL.

rjmccall fucked around with this message at 13:21 on Oct 19, 2008

Lazlo
May 1, 2002
Lately I've come to realize that I have certain materialistic needs.
I'm trying to learn some Java, and I've started by playing with Sun's tutorials. I've gone through the example at http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html and implemented it as written.

To better understand it, I'm trying to write a simple program that implements the class/subclass but I'm having trouble when it comes to creating an instance of the subclass. What am I doing wrong?

This is how I'm trying to create a MountainBike.
code:
   public static void main(String[] args)
{
   
    int myCadence=0, mySpeed=0, myGear=0, myHeight=0;

	    MountainBike myMBike = new MountainBike(myHeight, myCadence, myGear, mySpeed);

}
And this is what the compiler tells me:

code:
----------
1. ERROR in Bicycle.java (at line 116)
        MountainBike myMBike = new MountainBike(myHeight, myCadence, myGear, mySpeed);
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
No enclosing instance of type Bicycle is accessible. Must qualify the allocation with an enclosing instance of type Bicycle (e.g. x.new A() where x is an instance of Bicycle).
----------

crm
Oct 24, 2004

what does your bicycle and mountainbike code look like?

Lazlo
May 1, 2002
Lately I've come to realize that I have certain materialistic needs.

crm posted:

what does your bicycle and mountainbike code look like?

Basically the same as in sun's example:

code:


public class Bicycle
{

 
	//Variables that define a bike
	public int cadence;
	public int speed;
	public int gear;

	//Class Constructor
	public Bicycle(int initcadence,int initspeed,int initgear)
	{
	    cadence = initcadence;
	    speed = initspeed;
	    gear = initgear;
	}

	//Methods for bicycle Class

	public int getCadence()
	{
	    return cadence;
	}

	public void setcadence(int newCadence)
	{
	    cadence = newCadence;
	}
	
	public int getGear()
	{
	    return gear;
	}

	public void setGear(int newGear)
	{
	    gear = newGear;
	}
	
	public int getSpeed()
	{
	    return speed;
	}

	public void applyBrake(int decSpeed)
	{
	    speed -= decSpeed;
	}

	public void speedUp(int incSpeed)
	{
	    speed += incSpeed;
	}
    
    
    //Create subClass inherits and extends on bicycle class
    public class MountainBike extends Bicycle
    {
	//one extra field for mountainbike class
	public int seatHeight;

	//Subclass Constructor
	public MountainBike(int startHeight, int initgear, 
			    int initcadence, int initspeed)
	{
	    super(initcadence, initspeed, initgear);
	    seatHeight = startHeight;

	}
	public void setHeight(int newValue)
	{
	    seatHeight = newValue;
	}

    }

Mill Town
Apr 17, 2006

Oh. You've placed your MountainBike class inside the Bicycle class, which I assume is not what you meant to do.

Put the MountainBike class in its own separate MountainBike.java file and try again.

zootm
Aug 8, 2006

We used to be better friends.

Mill Town posted:

Oh. You've placed your MountainBike class inside the Bicycle class, which I assume is not what you meant to do.

Put the MountainBike class in its own separate MountainBike.java file and try again.
You can also mark MountainBike as a "static" class, but the above is almost definitely what you want to do.

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

There's no standard library for fast fourier transforms, is there? I need to write a program to do some audio analysis and I haven't used Java since version 2, so if anyone can reccomend a good library it would help out a lot.

Starving Autist
Oct 20, 2007

by Ralp
I've been trying to avoid actually writing any code for this cryptography class I'm in but alas, I actually have to, and now my avoidance of programming has bitten me in the rear end. I'm trying to implement RSA in java and it won't let me write functions:

import java.math.BigInteger;

BigInteger RSAencrypt(BigInteger exponent, BigInteger modulus)
{return this.modPow(exponent, modulus);}

won't compile. Anyone know what's up?

Mill Town
Apr 17, 2006

Bill O'Riley is GENIUS posted:

I've been trying to avoid actually writing any code for this cryptography class I'm in but alas, I actually have to, and now my avoidance of programming has bitten me in the rear end. I'm trying to implement RSA in java and it won't let me write functions:

import java.math.BigInteger;

BigInteger RSAencrypt(BigInteger exponent, BigInteger modulus)
{return this.modPow(exponent, modulus);}

won't compile. Anyone know what's up?

Functions have to be inside objects in Java. You can't have them sitting around on their own.

If it's not a function that does something to an object, you can use the static keyword to make it a class method instead of an instance method, like so:
code:
class RSA{
  public static BigInteger RSAencrypt(BigInteger exponent, BigInteger modulus)
  {return this.modPow(exponent, modulus);}
}
and then you call it by going c = RSA.RSAencrypt( a, b ); or whatever.

Edit: judging by that this.modPow you have in there, you should probably change modPow to a static method too and remove the this.

Actually it's not very clear what you are trying to accomplish here. Can you post the code in its entirety, the actual error you get when you try to compile, and a summary of what you think your code is actually doing?

Mill Town fucked around with this message at 03:39 on Oct 21, 2008

Starving Autist
Oct 20, 2007

by Ralp

Mill Town posted:

Functions have to be inside objects in Java. You can't have them sitting around on their own.

If it's not a function that does something to an object, you can use the static keyword to make it a class method instead of an instance method, like so:
code:
class RSA{
  public static BigInteger RSAencrypt(BigInteger exponent, BigInteger modulus)
  {return this.modPow(exponent, modulus);}
}
and then you call it by going c = RSA.RSAencrypt( a, b ); or whatever.

Thanks. Can someone direct me to a better programming language that supports large integers and lets me define whatever kind of functions I want? I think it's way too big of a pain that I have to define a class just to define functions on a pre-existing class. I know C++ lets you do this but it doesn't support large enough integers.

Starving Autist fucked around with this message at 03:42 on Oct 21, 2008

Mill Town
Apr 17, 2006

Bill O'Riley is GENIUS posted:

Thanks. Can someone direct me to a better programming language that supports large integers and lets me define whatever kind of functions I want?

There's no reason you can't implement this properly using objects. Implying that a language is "worse" for insisting on an object-oriented structure is... wrong.

Rather than flame you, though, I'm going to recommend python. It's a scripting language, meaning there's no compiling, which is nice for quick-n-dirty projects like this, it doesn't insist on objects, and it has bigint support built in.

Startacus
May 25, 2007
I am Startacus.
I have a real quick question. I'm writing a Java program for an address book and I need to be able to modify an entry in the database where it will overwrite what's saved now with whatever the user enters to replace it with but if the user just hits enter it doesn't overwrite anything and just leaves the current value in. In short, how can I read that a user just hit enter from the keyboard so I can tell it to leave the current value rather then overwriting the variable with a blank space?


Thanks


Edit: Another thing, is there a good way to write nodes to files, like from a binary tree?

Startacus fucked around with this message at 07:27 on Oct 21, 2008

zootm
Aug 8, 2006

We used to be better friends.

Bill O'Riley is GENIUS posted:

Thanks. Can someone direct me to a better programming language that supports large integers and lets me define whatever kind of functions I want? I think it's way too big of a pain that I have to define a class just to define functions on a pre-existing class. I know C++ lets you do this but it doesn't support large enough integers.
There's bound to be a BigInteger library for C++ if you really must drop two lines of code and encapsulation. Making things attached to classes is one of the better decisions in the design of Java, though; I'm pretty sure a "better" language isn't really what you want.

Alternatively you can run .scala files in an interpreted mode that'd let you do something like this, but unless you're just hacking something together you'd be better structuring your code properly in most languages.

Edit: Actually there's no real harm in doing this sort of thing in Python as Mill Town suggests.

Mill Town
Apr 17, 2006

Startacus posted:

I have a real quick question. I'm writing a Java program for an address book and I need to be able to modify an entry in the database where it will overwrite what's saved now with whatever the user enters to replace it with but if the user just hits enter it doesn't overwrite anything and just leaves the current value in. In short, how can I read that a user just hit enter from the keyboard so I can tell it to leave the current value rather then overwriting the variable with a blank space?


Thanks


Edit: Another thing, is there a good way to write nodes to files, like from a binary tree?

Hm, this sounds suspiciously like a homework question.

I'll just say that you can use the equals method on a string object to check that it's equivalent to something (for example, a blank string).

Starving Autist
Oct 20, 2007

by Ralp

zootm posted:

There's bound to be a BigInteger library for C++ if you really must drop two lines of code and encapsulation. Making things attached to classes is one of the better decisions in the design of Java, though; I'm pretty sure a "better" language isn't really what you want.

Yeah but I have no idea how to use others' code, every time I have tried it has ended in horrible failure and my compiler hates me.

ynef
Jun 12, 2002

Startacus posted:

Edit: Another thing, is there a good way to write nodes to files, like from a binary tree?

The topic you want to Google for is called Serialization. Preferably, serialization to XML. Like someone else said, this sounds like homework, so I'm just going to point you in the right direction.

Startacus
May 25, 2007
I am Startacus.
Yea, this is for a homework problem but I figured both of them out. The writeObject method actually let me just write the entire node to a file and then I just used the .equals on a string to determine if it was null. Thanks for the help.

crm
Oct 24, 2004

Mill Town posted:

Hm, this sounds suspiciously like a homework question.

I'll just say that you can use the equals method on a string object to check that it's equivalent to something (for example, a blank string).

speaking of blank strings, why did it take until 1.6 for Sun to implement isEmpty() on Strings?

teen bear
Feb 19, 2006

Working on an assignment for school I needed to create a program that would add the digits of a number until it was only one character long (88 > 16 > 7) when I came up with this:

code:
while (score>9){

	collapse = Integer.toString(score);

	runSum=score;

	score=0;

			
	for (int i=0; i <= collapse.length(); i++){

		score += runSum%10;

		runSum = runSum/10;

	}

}

It seems to work, but looks a little sloppy. Is there a better way of writing this? For instance, is there a way to determine the amount of characters in an int without converting it to a string?

Save the whales
Aug 31, 2004

by T. Finn

Capc posted:

It seems to work, but looks a little sloppy. Is there a better way of writing this? For instance, is there a way to determine the amount of characters in an int without converting it to a string?

If you're talking about the base 10 string you can get the number of characters in an int by dividing it by 10 until it's zero. This seems to work:

code:
public static int sumDigits(int n)
{
  while (n >= 10)
  {
    int sum = 0;
    
    while (n > 0)
    {
      sum += n % 10;
      n /= 10;
    }
    
    n = sum;
  }
  
  return n;
}

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Anyone know any good mapping library out there for java? For a project at work I'm going to feed some longitude/latitude/height data along with other data for that point, say air temperature. Going to want graph the data out as a topography, both 2D and 3D. Think something that looks like a dobbler radar.


Really doubt there is anything out there, just thought I would give it a shot.

zootm
Aug 8, 2006

We used to be better friends.

crm posted:

speaking of blank strings, why did it take until 1.6 for Sun to implement isEmpty() on Strings?
Whoa, I didn't even realise that'd been done! Thanks. It's surprising how quickly "".equals( str ) gets ingrained in your head as being synonymous.

sonic bed head
Dec 18, 2003

this is naturual, baby!

zootm posted:

Whoa, I didn't even realise that'd been done! Thanks. It's surprising how quickly "".equals( str ) gets ingrained in your head as being synonymous.

"".equals( str ) is NullPointer proof though, isn't it?

lamentable dustman
Apr 13, 2007

🏆🏆🏆

sonic bed head posted:

"".equals( str ) is NullPointer proof though, isn't it?

Yea because "" isn't technically null, it's just empty.

zootm
Aug 8, 2006

We used to be better friends.
Yeah, str.isEmpty() will fail when str is null. You should be using @Nonnull and @CheckForNull annotations to check for that stuff though ;)

necrobobsledder
Mar 21, 2005
Lay down your soul to the gods rock 'n roll
Nap Ghost
Most of the time when I write stuff, I treat a null string and empty string as the same thing. That is, it's the exception more than the norm that null and empty are distinguishable to me (but these days I'm not doing any SQL stuff, so I imagine that's the real reason to distinguish typically).

And really, an annotation for that? Wouldn't a straight-up method call to some static isNullOrEmpty method be faster anyway?

Mill Town
Apr 17, 2006

This seems to work:

code:
while (score>9){

	runSum=score;

	score=0;

			
	while(runSum != 0){

		score += runSum%10;

		runSum = runSum/10;

	}

}

Edit: Although both your code and my code will fail hilariously on negative numbers.

Editx2: Quick explanation: The trick here is in realizing that you don't need to count all the digits in the string for your inner loop, you just need to loop until you run out of digits to add. How do you know when you've run out of digits? When runSum is zero, you know you've divided away all its digits!

Mill Town fucked around with this message at 18:45 on Oct 22, 2008

zootm
Aug 8, 2006

We used to be better friends.

necrobobsledder posted:

Most of the time when I write stuff, I treat a null string and empty string as the same thing. That is, it's the exception more than the norm that null and empty are distinguishable to me (but these days I'm not doing any SQL stuff, so I imagine that's the real reason to distinguish typically).
Well, I do think there is a world of difference between an empty string and a null string; if there's not, you're probably using a String for something that you shouldn't be.

necrobobsledder posted:

And really, an annotation for that? Wouldn't a straight-up method call to some static isNullOrEmpty method be faster anyway?
What I'd really prefer is non-nullable types, but in the absence of them I find the annotations work very well at avoiding you killing your code readability you having to put null checks all over the place, while helping you out by highlighting where you could dereference things which are potentially null when you don't realise you're going to.

They are a little verbose from time to time, but I like them. I was mostly joking with that suggestion, though. And something like an actual non-nullable type would be preferable.

Save the whales
Aug 31, 2004

by T. Finn

zootm posted:

Well, I do think there is a world of difference between an empty string and a null string; if there's not, you're probably using a String for something that you shouldn't be.

I do web development and I almost always treat them as the same thing, especially in situations where the string is some HTML snippet that will get printed to the page. For instance, if your string is the inner text of some table cell, you probably don't want "null" to end up in there. Or if your string is the CSS class name of the table cell, you probably want both null and the empty string to have your method generate <td> rather than <td class=""> or worse <td class="null">.

Adbot
ADBOT LOVES YOU

zootm
Aug 8, 2006

We used to be better friends.

Save the whales posted:

I do web development and I almost always treat them as the same thing, especially in situations where the string is some HTML snippet that will get printed to the page. For instance, if your string is the inner text of some table cell, you probably don't want "null" to end up in there. Or if your string is the CSS class name of the table cell, you probably want both null and the empty string to have your method generate <td> rather than <td class=""> or worse <td class="null">.
Really you don't want to ever have an empty string there, though, right? Null means no value for class, an empty string means an empty string for class, which is most likely a bug in your code.

In seriousness I can see where this is inconvenient (Java for web programming can be a pain just because you don't tend to want to be so strict with definitions), so that makes some sense. But when you're writing code which does something less trivial than constructing a website from a template the semantic distinction is more important, and the difference between an empty string and a null value is more likely to be caused by a bug than anything else.

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