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
Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
Here's how it's done manually Adding Classes to the JAR File's Classpath and using an IDE just makes it easier.

Adbot
ADBOT LOVES YOU

My Rhythmic Crotch
Jan 13, 2011

Sorry if I'm being an idiot here, but this is the exact situation I find myself in: https://github.com/zeromq/jzmq/issues/29

So I don't think this problem is quite as simple as adding a jar. I have lots of other jars working without issue. This is the first time I've done anything with a native library. So I don't understand why I have to tell the JVM where to find the native library, but, whatever, I will figure out something one way or another.

My Rhythmic Crotch
Jan 13, 2011

On a somewhat related topic, this ZMQ stuff is pretty cool. But the guy who wrote the intro document is a huge douchebag. It basically goes something like this:

:smug: Brokerless messaging is where it's at. If your architecture uses a broker, it's slow and you suck balls.

...

:smug: So here is our optimal architecture, it includes a broker right in the middle, and as you can see, it makes everything really way super awesome.

Sab669
Sep 24, 2009

Can anyone recommend an easy-to-use UML-generating plugin for Eclipse? I really hate making the myself (or at least I just really hate Visio), but they're so helpful at times.

Sab669 fucked around with this message at 00:56 on Dec 1, 2011

DholmbladRU
May 4, 2006
I am trying to read from a file and place one like into an array. Which will eventually be enclosed in tags and written to an xml file.

But for now I just need help breaking up this string properly



code:
THIS IS WHAT A LINE OF THE FILE LOOKS LIKE
2011-10-18 14:01:13.853+05:30 [aaa][bbb][ccc][ddd][eee][fff] ggg

Below is how I would like to store this in an array
arr[2011-10-18]
arr[aaa]
arr[bbb]
arr[ccc]
arr[ddd]
arr[eee]
arr[fff]
arr[ggg]
This is what I have come up with so far, but it does not work..

code:
while(br.read()!= -1){
			String temp = br.readLine();
		        //out[0] = temp.substring(0,temp.indexOf("["));
			out = temp.split("/[(.*?)/]");
			
		}


I figured I could handle the time stamp which isnt enclosed in [] by the line that is commented out. However I was then unsure how to perform .split() but start at the array position 1.


Edit: I have to rethink this as some of the entrys are 7 parts and some are 8.. And sometimes the last portion of the entry is multiple lines of text long.

DholmbladRU fucked around with this message at 22:49 on Dec 2, 2011

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
code:
while(br.read()!= -1){
			String temp = br.readLine();
why are you doing this

DholmbladRU
May 4, 2006
I was altering a method that was origonally using a scanner, the while was checking for EOF. Im probably doing it wrong...

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

DholmbladRU posted:

I was altering a method that was origonally using a scanner, the while was checking for EOF. Im probably doing it wrong...

Yup.

readLine returns null at EOF - so you should be doing something like this instead:

code:
//read the first line
String line = reader.readLine();
while(line != null) {
  //do stuff with line, and then:

  //read the next line
  line = reader.readLine();
}

Thom Yorke raps
Nov 2, 2004


personally I prefer the:
String line;
while((line = scanner.nextLine) != null) {
}

syntax.

zootm
Aug 8, 2006

We used to be better friends.

Ranma posted:

personally I prefer the:
String line;
while((line = scanner.nextLine) != null) {
}

syntax.
I vehemently hate that syntax, but it tends to be the nicest way of writing such a thing.

Sarcophallus
Jun 12, 2011

by Lowtax

DholmbladRU posted:

code:
while(br.read()!= -1){
			String temp = br.readLine();
		        //out[0] = temp.substring(0,temp.indexOf("["));
			out = temp.split("/[(.*?)/]");
			
		}


I figured I could handle the time stamp which isnt enclosed in [] by the line that is commented out. However I was then unsure how to perform .split() but start at the array position 1.


Edit: I have to rethink this as some of the entrys are 7 parts and some are 8.. And sometimes the last portion of the entry is multiple lines of text long.

Well, you can do this with regular expressions! I assume you have an idea of how to use them, since you're using one in your split, so I'll just leave this here:
code:
(.*?) .*? \\[(.+?)\\]\\[(.+?)\\]\\[(.+?)\\]\\[(.+?)\\]\\[(.+?)\\]\\[?(.+?)?\\]? (.*)
That works for 7 or 8 groups of the [***] occurences. For your multiple lines problem, that's something you have to work around before you use the regular expression. I'd recommend using a StringBuilder and while the line isn't finished, you can simply yourStringBuilder.append(things) to the end.

Quick note on your split expression though, you're using the wrong '\' to escape your brackets. Also, you need to escape the escape character. So "\\[". If you end up using my regular expression you don't need to split anything, all of the groupings are set. You're going to be using a Pattern class for the regular expression, and a Matcher to match your input string with the Pattern. Then you call yourMatcher.group(#), and that gives you the string with your desired value to shove into the array. Ahh, I guess I ended up explaining how to use regex after all..

edit: Cleaned up the giant unnecessary quote.

Sarcophallus fucked around with this message at 20:19 on Dec 5, 2011

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Now he has two problems.

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

Jabor posted:

Now he has two problems.

Three if you count java.

Newf
Feb 14, 2006
I appreciate hacky sack on a much deeper level than you.
from http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html:

toString() - Returns a String object representing this Integer's value.

toString(int i) - Returns a String object representing the specified integer.

The Integer class has a no arg toString method. But doing something like this
code:
int i = 4;
String s = i.toString();
returns an error in compilation that says "int cannot be dereferenced", while doing this
code:
int i = 4;
String s = Integer.toString(i);
works just fine.


What am I misunderstanding about the no-arg method?

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
int is not the same as Integer.

Ensign Expendable
Nov 11, 2008

Lager beer is proof that god loves us
Pillbug
Technically, int is a primitive and Integer is a wrapper class, but autoboxing allows you to treat them basically the same way (aside from calling instance methods from them).

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

String also has the valueOf() method which is overloaded to take any of the primitives and return a string representation.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
code:
Integer i = 4;
// This returns the string, but is only useful when working with Integers not ints.
i.toString();
The fact that String has a valueOf method and Integer has a toString method is confusing. I can't decide which is better, but i'm leaning towards Integer.toString.

Ensign Expendable
Nov 11, 2008

Lager beer is proof that god loves us
Pillbug
toString() is the standard method of getting a string representation from an object, so use that, even if they do the same thing internally.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

But the problem is that in the case of primitives you're still not getting the string representation of the object you're dereferencing, but of the parameter you're passing in. Probably just a case of use what's been used earlier in the project or use whichever you prefer if there's no precedent; certainly the JDK documentation doesn't comment on either or.

zootm
Aug 8, 2006

We used to be better friends.
The Integer implementation has an 2-argument overload Integer.toString(int i, int radix) which lets you generate non-base 10 strings (there's also toHexString, toOctalString, and toBinaryString). If you ever plan on using that functionality, it might be worth using the Integer.toString variant just so code doesn't look weird.

Having said that although it's untidy that there's a couple of ways to do this, if you're agonising over which is the "right" one to do, you're probably focusing on the wrong thing.

Max Facetime
Apr 18, 2009

Use Integer variants when converting text to ints and vice versa:

int value = Integer.parseInt(splitText[0]);
String valueHex = Integer.toHexString(value);

Use String variants when creating debug output:

log("Important value is '" + (someCondition == false ? "" : String.valueOf(variable)) + "'");


It's not a big deal.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
String concatenation is so passé these days.

code:
log("Important value is '%d'", variable);

Newf
Feb 14, 2006
I appreciate hacky sack on a much deeper level than you.
I'm not sure I understand exactly what's going on with 'int' not being the same as 'Integer', but thanks for the discussion. Things to think about!

Another question, this one purely debugging:

code:
public static ArrayList<Integer> qSort(ArrayList<Integer> a){
	if (a.size() <= 1)
		return a;

	int pivot = a.get(0);
	ArrayList<Integer> lesser = new ArrayList<Integer>();
	ArrayList<Integer> greater = new ArrayList<Integer>();

	for (int i=0; i<a.size(); i++){
		if (a.get(i) < pivot)
			lesser.add(a.get(i));
		else
			greater.add(a.get(i));
	}
	return qSort(lesser).addAll(qSort(greater));
}
I'm getting an 'incompatible types' error on the last return statement, even though qSort seems to me to consistently return ArrayList<Integer> objects.

Any suggestions on how I'm misreading this?

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!

Newf posted:

I'm getting an 'incompatible types' error on the last return statement, even though qSort seems to me to consistently return ArrayList<Integer> objects.

Any suggestions on how I'm misreading this?

addAll() doesn't return what you think it returns.

Newf
Feb 14, 2006
I appreciate hacky sack on a much deeper level than you.
I see!

Thanks.

Paolomania
Apr 26, 2006

MEAT TREAT posted:

The fact that String has a valueOf method and Integer has a toString method is confusing. I can't decide which is better, but i'm leaning towards Integer.toString.

If you look at the source, they basically do the same thing with IIRC Integer.toString bottoming out at Integer.valueOf. As mentioned above, the various overloaded forms of Integer.valueOf give you more flexibility with formatting. The utility of the toString method is more indirect via the polymorphic behavior of calls dispatched from Object.toString - so loggers and debuggers have a consistent way of dumping the string form of an arbitrary object. If you have explicit knowledge that a reference is an Integer type, I believe valueOf is the "proper" way, as a line of code that reads Integer.valueOf(foo) reads to a human as unambiguously typed, whereas foo.valueOf() requires the extra cognitive leap of looking up the type of foo to know that it is an explicit call to Integer.toString (as opposed to Object.toString or any other toString). In actuality it is just incredibly nit-picky to worry about.

iscis
Jan 8, 2007
im the coolest guy you know
How's it going guys. I'm trying to write a Person class that provides a few methods, then write a test class that creates some person objects, and I'm having trouble with a certain method. I'm supposed to implement: public boolean isLike ( Person otherPerson) - compares the current object person to otherPerson. The method will return true if the first and last names of both objects are the same; otherwise it will return false.

what I have for this is
code:
public boolean isLike(Person otherPerson)
	{
	if(otherPerson!=null && 
	otherPerson.getClass().equals(this.getClass()))
	{
	return (getFirstName().equals(((Person)otherPerson).getFirstName())
	&& getLastName().equals(((Person)otherPerson).getLastName()));
	}
	return false;
	}
How can I then call on the method (provided this code works) in my test program?

After this I need to call the isLike method, but for whatever reason I cannot how should I go about doing this?

iscis fucked around with this message at 05:50 on Dec 12, 2011

baquerd
Jul 2, 2007

by FactsAreUseless

iscis posted:

How's it going guys. I'm trying to write a Person class that provides a few methods, then write a test class that creates some person objects, and I'm having trouble with a certain method.

How can I then call on the method (provided this code works) in my test program?
The test program is very simple:
code:
public class TestPerson {
	public static void main(String[] args) {
		Person person01= new Person("first","middle","last");
		Person person02= new Person("first","middle","last");
		System.out.println(person01);
		System.out.println(person02);
After this I need to call the isLike method, but for whatever reason I cannot how should I go about doing this?

You have a number of issues going on. First is that isLike() is taking the place of equals() which may be necessary if you're not clear on class casting yet. Next is your indentation. Your code looks like sweaty monkey balls. It should look something like this:
code:
public boolean isLike(Person otherPerson) {
  if(otherPerson!=null && otherPerson.getClass().equals(this.getClass())) {
    return (getFirstName().equals(((Person)otherPerson).getFirstName())
      && getLastName().equals(((Person)otherPerson).getLastName()));
  }
  return false;
}
But not really. It should actually look like this:
code:
public boolean equals(Object obj) {
  if (obj!= null && objinstanceof Person) { 
    Person otherPerson = (Person)obj;
    return (getFirstName().equals(otherPerson.getFirstName())
      && getLastName().equals(otherPerson.getLastName());
  }
  return false;
}
And you would call it like this:

code:
public static void main(String[] args) {
  Person person01= new Person("first","middle","last");
  Person person02= new Person("first","middle","last");
  if (person01.equals(person02)) {
    System.out.println("Equality for everyone!");
  } else {
    System.out.println("I'm being oppressed by incompetence!");
  }
}

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
It's not an equals method. It's comparing only the first and last names, but a Person also has a middle name.

iscis
Jan 8, 2007
im the coolest guy you know

baquerd posted:

useful stuff
Thanks, everything is working great now. Now I have another question about a different class I'm working on. I'm writing a cylinder class that takes the radius and height from a cylinder object and returns the surface area and volume, and all the methods work, it's just that I'm supposed to use the mutators in the class to check for reasonable values (non-negative is all, I'm assuming), and I have no idea how to do this.
code:


	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {

		this.radius = radius;

	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;

	}

Should I throw some kind of conditional statement in the setter? Or what is the best way to use the setter to check values?

iscis fucked around with this message at 05:49 on Dec 12, 2011

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
code:
/**
 * @throws IllegalArgumentException if the given height is negative
 */
public void setHeight(double height) {
    if(height < 0.0){
        throw new IllegalArgumentException("Can't have a negative height.");
    }                
    this.height = height;
}
Whenever you throw a RuntimeException you should indicate it in the documentation of the method.

iscis
Jan 8, 2007
im the coolest guy you know

MEAT TREAT posted:

code:
/**
 * @throws IllegalArgumentException if the given height is negative
 */
public void setHeight(double height) {
    if(height < 0.0){
        throw new IllegalArgumentException("Can't have a negative height.");
    }                
    this.height = height;
}
Whenever you throw a RuntimeException you should indicate it in the documentation of the method.

I implemented this code, however it will not throw the exception and simply outputs cylinder information: Radius: -2.1
Height: -8.6
Surface Area: 141.18
Volume: -119.15

Contra Duck
Nov 4, 2004

#1 DAD
Does your constructor also check for valid values? Might want to call setHeight/Radius in the constructor so you get the validation there too rather than just setting the values directly.

FateFree
Nov 14, 2003

I've been spoiled by web frameworks for too long and now I need to write a gritty ServletFilter myself and I need a little help on how to do it.

The purpose of the filter is to run last in the filter chain, take all the html that is meant to be returned as the response, and strip out all the content except for the elements that match a given id, and return the fragmented response.

Parsing aside, how do I go about getting the html body from the response, and resend a modified version of it? How do I ensure that the response won't get partially flushed while I'm working on it?

Kreczor
Oct 30, 2011

I'd still rather play with medieval knights online then go outside and "hang out".

MEAT TREAT posted:

code:
/**
 * @throws IllegalArgumentException if the given height is negative
 */
public void setHeight(double height) {
    if(height < 0.0){
        throw new IllegalArgumentException("Can't have a negative height.");
    }                
    this.height = height;
}
Whenever you throw a RuntimeException you should indicate it in the documentation of the method.
public void setHeight(double height) throws IllegalArgumentException{
if (height > 0)
this.height = height;
else
throw new IllegalArgumentException("Can't have a negative height.");
}

This may just simply do the same thing but this is how I've been writing my mutator methods.

Edit: I'm an idiot for not reading before posting. On a better note it gives me reason to ask: why do we "throws IllegalArgumentException" in the method header?

Kreczor fucked around with this message at 17:36 on Dec 13, 2011

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
Because otherwise other people that use your code won't have any clue that the method will throw that exception until someone actually passes in a negative value.

Max Facetime
Apr 18, 2009

FateFree posted:

I've been spoiled by web frameworks for too long and now I need to write a gritty ServletFilter myself and I need a little help on how to do it.

The purpose of the filter is to run last in the filter chain, take all the html that is meant to be returned as the response, and strip out all the content except for the elements that match a given id, and return the fragmented response.

Parsing aside, how do I go about getting the html body from the response, and resend a modified version of it? How do I ensure that the response won't get partially flushed while I'm working on it?

Maybe something like this high-level design would work:

- Create MyHttpServletResponse, which wraps the original Response and forwards or buffers the data that would have gone to it.
- Create MyOutputStream which buffers the rendered HTML. This will be given out by MyHttpServletResponse.
- Put MyFilter first in the filter chain so it can wrap all or some of the responses.
- Do the parsing and processing and sending after the wrapping outputstream or response has been closed or committed.

Caveat: this could break some badly coded filters or servlets if they cast ServletResponse to something other than HttpServletResponse.

FateFree
Nov 14, 2003

Thanks thats a great high level explanation, I'll get looking into that. Appreciate it

Adbot
ADBOT LOVES YOU

FateFree
Nov 14, 2003

rhag posted:

The most common answer I've seen on the forums from the devs is to use a Set. I can see their reasoning, and it's a sound one. However, using a Set poses a different set of problems (namely, one cannot just add children to the parent without an ID set. That is...before the children are saved).

This doesn't have to be a limitation. My solution to the equals/hashcode problem with hibernate is two have two primary keys, an autoincremented pk, and a String UID which is just mapped as a unique field. However my hashcode/equals only bothers looking at the uid, which gets created on object creation through UUID.randomUID.toString.

So this way you can put any object in a set before persisting it, and the equals will work after you persist it because hibernate will set the UUID it received from the table. Make sense?

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