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
Drumstick
Jun 20, 2006
Lord of cacti
yay, thanks for that catch.

I have one more problem im running into. How can i check the values next to each in an array?

say, if this would be how it is saved in the array

quote:


true |true |false
false|true |true
false|false|false



I need to be able to check the value for each adjacent item in the array. Is this possible, and any hint on how to do it?

Adbot
ADBOT LOVES YOU

Incoherence
May 22, 2004

POYO AND TEAR

Drumstick posted:

yay, thanks for that catch.

I have one more problem im running into. How can i check the values next to each in an array?

say, if this would be how it is saved in the array


I need to be able to check the value for each adjacent item in the array. Is this possible, and any hint on how to do it?
Suppose the point in question is lifegame[x][y]. What are the indices of the four adjacent points (or eight, if you want to go that way)?

Your question makes me unsure whether you understand what's going on here, so I'm being intentionally general to make sure we're not doing your homework for you or something. If you're having trouble, draw a picture of one of your grids and label each row and column with an index, then try and generalize to the question I asked above.

There's one catch: you'll want to check that each of your four/eight adjacent points are inside the matrix, but what you want to do about that will depend on whether you want to wrap around on edges or just drop them.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Incoherence posted:

There's one catch: you'll want to check that each of your four/eight adjacent points are inside the matrix, but what you want to do about that will depend on whether you want to wrap around on edges or just drop them.

I've always wondered what people do when you just want to drop them. In the past I've surrounded that block of code with a try/catch that doesn't do anything when an IndexOutOfBoundsException is thrown.

Drumstick
Jun 20, 2006
Lord of cacti

Incoherence posted:

Suppose the point in question is lifegame[x][y]. What are the indices of the four adjacent points (or eight, if you want to go that way)?

Your question makes me unsure whether you understand what's going on here, so I'm being intentionally general to make sure we're not doing your homework for you or something. If you're having trouble, draw a picture of one of your grids and label each row and column with an index, then try and generalize to the question I asked above.

There's one catch: you'll want to check that each of your four/eight adjacent points are inside the matrix, but what you want to do about that will depend on whether you want to wrap around on edges or just drop them.

if i understand what your asking, you want the positions of the 8 adjacent 'boxes'

quote:


[x-1][y-1] [x-1][y] [x-1][y+1]
[x][y-1] [x][y] [x][y+1]
[x+1][y-1] [x+1][y] [x+1][y+1]


Im assuming this is what you meant by that. As for checking, they will need to drop off and not wrap around. Let me know if i understood what you were asking.

Incoherence
May 22, 2004

POYO AND TEAR

MEAT TREAT posted:

I've always wondered what people do when you just want to drop them. In the past I've surrounded that block of code with a try/catch that doesn't do anything when an IndexOutOfBoundsException is thrown.
code:
if (x >= 0 && x < max_rows && y >= 0 && y < max_cols) {
  // this point is obviously inside the matrix!
}
If I ever saw anyone use a try/catch I'd go put it in the Coding Horror thread posthaste.

Drumstick posted:

if i understand what your asking, you want the positions of the 8 adjacent 'boxes'


Im assuming this is what you meant by that.
Yes, and that should get you very close to answering your question on how to check the 8 adjacent boxes. Isn't the Socratic method wonderful?

quote:

As for checking, they will need to drop off and not wrap around. Let me know if i understood what you were asking.
Okay, then you want to guard against checking out-of-bounds boxes in much the way I posted above.

Drumstick
Jun 20, 2006
Lord of cacti
haha, yeah thanks for helping point my mind in the right direction. as soon as i drew it out and worked it out I saw a possible way to do it. Thank you so much.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Incoherence posted:

If I ever saw anyone use a try/catch I'd go put it in the Coding Horror thread posthaste.

You are right, I was too quick to post. Here is what I actually meant, I had an algorithm that would traverse the array diagonally. The problem I had was dealing with 01, 02, 04, 06, 08, and 09. So I put it in a try catch and let it keep doing it's thing.


01 02 03
04 05 06
07 08 09



As I posted this I realized that I could check to see if it was out of bounds and just use the continue statement. :doh:

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Would anyone know why this bit of code won't worry on a blackberry?

code:
<script>
var checkobj

function agreesubmit(el){
checkobj=el
if (document.all||document.getElementById){
for (i=0;i<checkobj.form.length;i++){  //finds call button
var tempobj=checkobj.form.elements[i]
if(tempobj.type.toLowerCase()=="submit")
tempobj.disabled=!checkobj.checked
}
}
}

function defaultagree(el){
if (!document.all&&!document.getElementById){
if (window.checkobj&&checkobj.checked)
return true
else{
alert("Please read and accept the Terms and Conditions to place the call.")
return false
}
}
}

</script>
It's to enable the form submit button on a web page only after the check box is checked.

Is there a better way to accomplish this?

Edit: I'm sorry if this isn't the right thread for this, given my code is JavaScript on a webpage.

Treytor fucked around with this message at 07:55 on Apr 11, 2008

Fehler
Dec 14, 2004

.
To rephrase my question, can anybody recommend a free Java FTP client class that supports upload progress monitoring and can abort uploads? Apache's client can let me monitor the progress, but aborting doesn't seem to work...

tef
May 30, 2004

-> some l-system crap ->

Treytor posted:

Would anyone know why this bit of code won't worry on a blackberry?

Edit: I'm sorry if this isn't the right thread for this, given my code is JavaScript on a webpage.

Please try the Small web dev questions thread.

agnitrate
Oct 20, 2002
tool fan

Fehler posted:

To rephrase my question, can anybody recommend a free Java FTP client class that supports upload progress monitoring and can abort uploads? Apache's client can let me monitor the progress, but aborting doesn't seem to work...

Check out JSCH (Java Secure Channel) at http://www.jcraft.com/jsch/. I know for sure it has progress monitoring, but I'm not certain if it lets you abort uploads and I can't find it on the site.

I use it for SFTP uploads/downloads and it works really nicely.

InvSqrt
Nov 30, 2007
Anybody out there able to guess what my university lecturer means by "shrink-wrap the window"? It's a Java Swing exercise so I was thinking maybe it's referring to some feature that me and Google don't know about.

[image removed - in fact never mind, this stupid poo poo isn't worth anybody's time]

InvSqrt fucked around with this message at 17:47 on Apr 13, 2008

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
Maybe he means calling the pack method on the window.
Window.pack()

adante
Sep 18, 2003
Hi, I am fiddling with the java scripting interface. As I understand the rhino engine is packaged with the jdk but what I don't understand is how does one reconcile the objects one gets from their engine (e.g. sun.org.mozilla.javascript.internal.ScriptableObject) versus the ones in the rhino api (e.g. org.mozilla.javascript.ScriptableObject)?

Is the one in the jdk heavily modified or wrappered up to suit their javax.scripting interface?

More specifically, how do I manipulate javascript objects from java?

e.g. suppose I have this java code:

code:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine js = mgr.getEngineByName("js");
jsEngine.eval("var foo = { x : 1, y : 'hello', z : false };");
Object o = jsEngine.get("foo");
Now o is instanceof sun.org.mozilla.javascript.internal.NativeObject, which is a subclass of a bunch of other classes in the s.o.m.j.i namespace. Being internal I can't use these directly.

Looking at the Rhino API obviously you normally get a org.mozilla.javascript.ScriptableObject and can call the .get() .getIds() methods. But I don't know how to translate the objects I am getting in my actual java code which uses the jdk engine into org.mozilla.javascript objects.

Is this even possible? Am I misunderstanding something completely? Should I just be using the rhino js jar and not the javax.scripting interface? If so, for what reason is the javax.scripting interface implemented?

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
^^ You need to look up the javax.script.Bindings class. That is how you pass variables back and forth between a script and the host java program.

adante
Sep 18, 2003

TRex EaterofCars posted:

^^ You need to look up the javax.script.Bindings class. That is how you pass variables back and forth between a script and the host java program.

:confused: sorry, but can you give me a concrete example of what you mean? As far as I can tell getting the Bindings will allow me to get a java reference to the javascript object - but I already do this using the jsEngine.get("foo"). What I want to know is how do I manipulate it? For instance, how would I get a value given a key, or list the keys?

ShinAli
May 2, 2003

The Kid better watch his step.
Not anything Java specific but Eclipse is a Java thing sort of so whatever.

Eclipse at this point is the only IDE I'll ever use in a long time, yet this bug pops up once in a while and I waste about an hour loving with it.

It pops up randomly, but sometimes it doesn't recognize a class in the entire code or just a single line. I clean and build and everything but it doesn't go away. The code will still compile because javac isn't as retarded as Eclipse but I depend on IntelliSense quite a bit when I don't have a javadoc on hand.

If it still compiles I suppose it isn't a big problem, but it happened one too many times and I just want to get rid of it.

EDIT: I'll throw in a real problem too, I guess.

I'm using a Combobox on a GUI I've created, and whenever I fill the combobox up, the little down arrow button on the right of it disappears. I do a repaint on the componenet but nothing. Then again, I really don't understand repaint that well as everytime I tried to use it, it never really works for me.

ShinAli fucked around with this message at 18:01 on Apr 15, 2008

Red Oktober
May 24, 2006

wiggly eyes!



ShinAli posted:

It pops up randomly, but sometimes it doesn't recognize a class in the entire code or just a single line. I clean and build and everything but it doesn't go away.



I might have the error too, if you add a space in front of the 'package a.b.c;' to make it ' package a.b.c;', does it go away?

Because if so, that's the only means I've found of getting rid of it.

Twitchy
May 27, 2005

ShinAli posted:

I'm using a Combobox on a GUI I've created, and whenever I fill the combobox up, the little down arrow button on the right of it disappears. I do a repaint on the componenet but nothing. Then again, I really don't understand repaint that well as everytime I tried to use it, it never really works for me.

Have you tried calling revalidate on the panel it's in?

ShinAli
May 2, 2003

The Kid better watch his step.

Twitchy posted:

Have you tried calling revalidate on the panel it's in?

I'll try it right now.

ShinAli fucked around with this message at 21:14 on Apr 15, 2008

ShinAli
May 2, 2003

The Kid better watch his step.

ShinAli posted:

I'll try it right now.

That didn't work :( This is how I fill up the combobox

code:
cbxServiceList.setModel(new javax.swing.DefaultComboBoxModel(serviceNames));
serviceNames being an array of Strings. I have it do the revalidate right after it gets filled.

clayburn
Mar 6, 2007

Cammy Cam Juice
Simple question I think. I have a very long string, that I want to take all n-length substrings out of and place into an array. For example if n is 5 and the sentence is "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt whatever", I would want string one to be "Lorem ipsum dolor sit amet,"
, string two to be "ipsum dolor sit amet, consectetur", string three to be "dolor sit amet, consectetur adipisicing", and so on. What would be the best way to go about this? I am having a hard time getting anything to work really.

epswing
Nov 4, 2003

Soiled Meat

clayburn posted:

Simple question I think. I have a very long string, that I want to take all n-length substrings out of and place into an array. For example if n is 5 and the sentence is "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt whatever", I would want string one to be "Lorem ipsum dolor sit amet,"
, string two to be "ipsum dolor sit amet, consectetur", string three to be "dolor sit amet, consectetur adipisicing", and so on. What would be the best way to go about this? I am having a hard time getting anything to work really.

Smells like homework. You probably want to take a look at the String.split method, or the StringTokenizer class. Before writing code, however, you should think about the problem and devise a strategy to tackle it. Think about what loops you'll need where (assuming recursion isn't in the picture yet).

Wouldn't it be nice if all the words in your string were in an array that you could loop over? ;)

epswing fucked around with this message at 07:50 on Apr 17, 2008

clayburn
Mar 6, 2007

Cammy Cam Juice
Haha it might be homework. I really just needed a point in the right direction like you gave, just some methods to look at. I've considered putting the words in some sort of array, I'm just worried about the efficiency of putting them there in the first place. I'll definitely check those methods out though, thanks.

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

clayburn posted:

Simple question I think. I have a very long string, that I want to take all n-length substrings out of and place into an array. For example if n is 5 and the sentence is "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt whatever", I would want string one to be "Lorem ipsum dolor sit amet,"
, string two to be "ipsum dolor sit amet, consectetur", string three to be "dolor sit amet, consectetur adipisicing", and so on. What would be the best way to go about this? I am having a hard time getting anything to work really.
I don't know anything about Java's regex libraries, but I would think that a regex could do this fairly easily.

Incoherence
May 22, 2004

POYO AND TEAR

clayburn posted:

Haha it might be homework. I really just needed a point in the right direction like you gave, just some methods to look at. I've considered putting the words in some sort of array, I'm just worried about the efficiency of putting them there in the first place. I'll definitely check those methods out though, thanks.
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson

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

adante posted:

:confused: sorry, but can you give me a concrete example of what you mean? As far as I can tell getting the Bindings will allow me to get a java reference to the javascript object - but I already do this using the jsEngine.get("foo"). What I want to know is how do I manipulate it? For instance, how would I get a value given a key, or list the keys?

Bindings implements Map, so you'll be able to iterate over the entries just like Map:

code:
for (Map.Entry<String,Object> e:bindings) {
...do stuff...
}
.get() and .put() both work just like Map as well.

GameCube
Nov 21, 2006

Popping in not for Java coding help, but NetBeans: Anybody know how to set the contents of a JComboBox from an array or list? There's a box to type your "pre-generation code," but I really don't know what to put there, and it's surprisingly hard to find clear instructions online.

Twitchy
May 27, 2005

clayburn posted:

Haha it might be homework. I really just needed a point in the right direction like you gave, just some methods to look at. I've considered putting the words in some sort of array, I'm just worried about the efficiency of putting them there in the first place. I'll definitely check those methods out though, thanks.

You could tokenize the string like epswing said, or you can use the Matcher and Pattern classes (Regex) to do it for you, might aswell learn in now since it's incredibly useful. Basically what you want to search for is: 1 or more non-space characters, followed by an optional white space character, repeated 5 times.

http://java.sun.com/docs/books/tutorial/essential/regex/index.html is an ok place to learn Regex in Java.

I had a little shot at it and it's a pretty simple regular expression so once you know the basics it will be a piece of cake.

ShinAli
May 2, 2003

The Kid better watch his step.

Werthog posted:

Popping in not for Java coding help, but NetBeans: Anybody know how to set the contents of a JComboBox from an array or list? There's a box to type your "pre-generation code," but I really don't know what to put there, and it's surprisingly hard to find clear instructions online.

ShinAli posted:

code:
cbxServiceList.setModel(new javax.swing.DefaultComboBoxModel(serviceNames));
serviceNames being an array of Strings.

Like every other Java Swing component, you'd have to jump through hoops and set up a model for ComboBox. Luckily, there is a default model that'll just take in an array of Strings in the constructor. Declaring or using an DefaultComboBoxModel object in the custom code text box should make it work.

GameCube
Nov 21, 2006

ShinAli posted:

Like every other Java Swing component, you'd have to jump through hoops and set up a model for ComboBox. Luckily, there is a default model that'll just take in an array of Strings in the constructor. Declaring or using an DefaultComboBoxModel object in the custom code text box should make it work.

Thanks. While waiting for this answer, I was screwing around with some other part of my project, and just happened to glance at some auto-generated code that helped me figure that out for myself, but having the full answer here helps, too. Now I've graduated to trying to make a table model for a JDBC connection. Fun stuff. :suicide:

adante
Sep 18, 2003

TRex EaterofCars posted:

Bindings implements Map, so you'll be able to iterate over the entries just like Map:

code:
for (Map.Entry<String,Object> e:bindings) {
...do stuff...
}
.get() and .put() both work just like Map as well.

ok, I am feeling a little silly to ask this, but could you provide me with EXACT CODE WHICH WILL COMPILE to do what I want, because I cannot see how to apply the bindings method (which is an engine method to the object o in my previous code listing.

hey wiz
Jun 18, 2005

adante posted:

ok, I am feeling a little silly to ask this, but could you provide me with EXACT CODE WHICH WILL COMPILE to do what I want, because I cannot see how to apply the bindings method (which is an engine method to the object o in my previous code listing.
What else do you want, he pretty much gave you the code. If you want to iterate over each entry, probably you will want something like this:

code:
for (Map.Entry<String,Object> e: bindings) {
    String key = e.getKey();
    Object value = e.getValue();  // equivalent to bindings.get(key)
    System.out.println("key: " + key + "\tvalue: " + value);
}
Be careful if you need to remove any elements from this map. You either need to do it using an iterator, or by copying all the keys to a second list/set and then call first.removeAll(second) after you finish iterating. Otherwise you will see a ConcurrentModificationException.

adante
Sep 18, 2003

hey wiz posted:

What else do you want, he pretty much gave you the code. If you want to iterate over each entry, probably you will want something like this:
ConcurrentModificationException.

well as I said, I basically want the exact code which will compile.

Either I have a fundamentally flawed view of the scripting API (and I am certainly not discounting this) or you guys are misunderstanding what I am asking for. I want to manipulate a javascript object by getting its keys and values.

The only way I can see how to use the code you guys have given me is to manipulate the javascript ENGINE, allowing me to get its variables. This is great, I got the variable I wanted, a javascript object, in my first post, under the code line which goes "Object o = jsEngine.get("foo");". Now I want to get the keys and values of that object.

I'm no expert with java and maybe you can use the Bindings class in a way to interrogate this object o. Unfortunately I have absolutely no idea how to do this because I cannot see any way to get a Bindings object relevant to o.

Hence, I asked for some exact code which I could compile so that I could either figure out exactly how to use the bindings class (if this is infact the case) or confirm that there has been a misunderstanding and I need to clarify.

In order to hopefully clarify exactly what I want, here's a skeleton template:

code:
import java.io.*;
import java.util.*;
import javax.script.*;

public class test {

	public static void main(String [] args) throws Exception
	{
		
        // TODO code application logic here
		ScriptEngineManager mgr = new ScriptEngineManager(); 
		ScriptEngine jsEngine = mgr.getEngineByName("js");
		jsEngine.eval("var foo = { x : 1, y : 'hello', z : false };");
		Object o = jsEngine.get("foo");
		
		interrogationFunction(o);
	}
	
	public static void interrogationFunction(Object o)
	{
		System.out.printf("Object is of type %s", o.getClass().getName());
		// implement me
	}
}
The objective is to implement the function interrogationFunction() to produce this output (or equivalent):

code:
This object has the following key/type/value pairs:
   x : java.lang.Double : 1.0
   y : java.lang.String : hello
   z : java.lang.Boolean : false
Just to show I am trying to make an effort here, the only way I can see how to implement the code you guys are suggesting is like this:

code:
		for (Map.Entry<String,Object> e: jsEngine.getBindings(ScriptContext.ENGINE_SCOPE).entrySet())
		{
			String key = e.getKey();
			Object value = e.getValue(); // equivalent to bindings.get(key) System.out.println("key: " + key + "\tvalue: " + value); }
			System.out.println("key: " + key + "\tvalue: " + value);
		}
Obviously this will get the engine variables, and produce this output:
code:
key: context	value: javax.script.SimpleScriptContext@b23210
key: foo	value: [object Object]
key: print	value: sun.org.mozilla.javascript.internal.InterpretedFunction@f4f44a

ShinAli
May 2, 2003

The Kid better watch his step.
Not really a question but I thought it'd be a useful little tid bit to post.

In all of my time of using Java, I've always assumed passing Objects in method parameters will pass by reference and primitives pass by value. This is actually not the case at all.

Everything is passed by value. This doesn't mean what you think about Objects though, as the references to the Objects themselves are the ones that are passed by value. Here is an example:

code:
// Point is an object containing two integers called X and Y
Point pnt = new Point(2,5);
System.out.println(pnt.toString());
// over ridden toString method returns 'X = 2, Y = 5'
changePoint(pnt);
System.out.println(pnt.toString());
// prints 'X = 6, Y = 3'

void changePoint(Point temp) {
  temp.setX(6);
  temp.setY(3);
  temp = null;
}
In the changePoint method the passed value of temp's reference is changed to null, not temp's actual reference. The passed reference value can still modify the same Object via methods and the sort.

Sorry if most of you all already knew this, but I think it's something useful to know for a the newbie passer-by Java programmers. It blew my mind to the point I'll always remember this and will never make a mistake like that.

Twitchy
May 27, 2005

^^ Edit: Misunderstood you!

I think you've got it a little confused, primitives are passed by value, objects are passed by reference. Passing by value means the method has it's own copy, so even if the variable is changed during the method, it will not have an effect on the parameter that was passed in. Objects on the other hand, pass by reference, meaning any changes you make to them inside a method will change the same object you passed into the method.

When you set temp to null in that method, you are only saying that the variable no longer references the object. temp basically says "if you want to see what i contain, goto this location in memory", so when you it's set to null it just says "i don't refer to anything :(", but the location in memory still exists because there is another reference to it in the other method.

Jesus... I could never be a teacher.

Twitchy fucked around with this message at 22:11 on Apr 18, 2008

csammis
Aug 26, 2003

Mental Institution

Twitchy posted:

I think you've got it a little confused

Actually he's got it exactly right. The reference to the object is passed by value; you can modify the object, but not the reference to it.

Twitchy
May 27, 2005

csammis posted:

Actually he's got it exactly right. The reference to the object is passed by value; you can modify the object, but not the reference to it.

I think it was me being a little confused about what he meant then heh. It's still passed by reference though really, saying it's passed by value just because it's reference is stored in a variable is more likely to confuse a new programmer than help them, that's all. I thought he thought (I thought that he thought that he thought) that because he set the variable to null but the other variable reference was fine that each method had it's own copy of the object / value.

Twitchy fucked around with this message at 22:12 on Apr 18, 2008

ShinAli
May 2, 2003

The Kid better watch his step.
Yeah I think I need to re-write it :psyduck:

Does "pass by reference" usually mean passing by value the copy of the reference? Or does real "passing by reference" mean that you can modify the original reference? If it's the former, then Objects passing by reference is actually correct and I'm just retarded :saddowns:

Adbot
ADBOT LOVES YOU

Twitchy
May 27, 2005

ShinAli posted:

Yeah I think I need to re-write it :psyduck:

Does "pass by reference" usually mean passing by value the copy of the reference? Or does real "passing by reference" mean that you can modify the original reference? If it's the former, then Objects passing by reference is actually correct and I'm just retarded :saddowns:

Yeah, passing by reference is assigning a variable to a location in memory, so setting it to null will not actually remove it (unless no other variables reference it, in which case it would be garbage collected).

You are right though, technically the reference IS passed by value, meaning you can set it too null and other references will be fine, but I think it can be a rather confusing thing to get, especially for new programmers where there needs to be a clear distinction between values (primitives) and objects.

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