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
the onion wizard
Apr 14, 2004

fletcher posted:

Ok cool, that led me to this

What's wrong with my code? It's telling me type mismatch, cannot convert from int to string.

What type is name in the Product class? If it's a String already, you don't need those casts to String. It's only a minor issue, but every time I see stuff like that at work I die a little.


Along these lines, is all programming in a corporate environment completely soul crushing, or is there hope?

Adbot
ADBOT LOVES YOU

the onion wizard
Apr 14, 2004

FearIt posted:

Is it more complicated if my class serves as a ArrayList container for sub classes?

Probably; as you'll need to manually copy any data you require in the copy-contructor.

If you're not concerned with performance you could use the apache commons SerializationUtils; as mentioned in the javadoc, all objects you want cloned must implement Serializable (which ArrayList implements already).




vvv I agree 100%, I was considering adding a disclaimer to that effect. vvv

the onion wizard fucked around with this message at 22:51 on Mar 25, 2008

the onion wizard
Apr 14, 2004

Anphear posted:

The problem that I'm currently having is the scanner class isn't 'working right' and that my array remains empty. With the toString method simply printing [] and if I remove the isEmpty() method I get a NaN return.

First, you're only ever reading in one number. To read in more than one number you'll need to loop on the input. Something like this:
code:
while (s.hasNextInt()) {
    n.add(s.nextInt());
}
Can you post calcAverage() and toString() from NumberList? Edit: or the whole thing if it's not too long.

Anphear posted:

code:
  public static int readInt(String message){
    Scanner sc =  new Scanner(System.in);
    return sc.nextInt();
  }
What's this method for?

Edit: I'm going to bed, but I wrote this while looking at Scanner; it might help, or it may just confuse matters further.

the onion wizard fucked around with this message at 14:44 on Mar 27, 2008

the onion wizard
Apr 14, 2004

fletcher posted:

Is there a way to do this without int count?

code:
Iterator accountIds = accounts.keySet().iterator();

while (accountIds.hasNext()) {

    qry += "'"+accountIds.next()+"'";
    
    if (accountIds.hasNext()) {
	qry+=",";
    }
}

How's that?




epswing & Brain Candy: I was under the impression that the compiler will take care of substituting StringBuilder during string concatenation?

http://java.sun.com/developer/technicalArticles/Interviews/community/kabutz_qa.html posted:

When Strings are added using the + operator, the compiler in J2SE 5.0 and Java SE 6 will automatically use StringBuilder

the onion wizard
Apr 14, 2004

Whilst farting I posted:

I'm writing an applet which plays a card game...
I may be missing something, but shouldn't the flow go something like this:
[list=1]
[*]Display 2 cards
[*]Accept user's bet
[*]Final card and winnings calculation
[/list]
Now, #1 is triggered by "startround", #2 by "bet", and #3 by #2?

the onion wizard
Apr 14, 2004

Whilst farting I posted:

Out of curiousity for the future, though, does anyone know a better way for making a program wait on a non-input dialog basis besides the sleep method BELL END mentioned?

The technique BELL END mentioned is called polling, and is usually a bad idea.

If you've got an event driven UI (which is the case here), then you don't need to poll; actions are triggered by events.

An alternative to this is to use queues, and when attempting to read from a queue the consumer will be blocked until something is available (same goes for the producer putting stuff into the queue if the queue is full). Once a message becomes available in the queue, the consumer will be awoken and handed the message.

I used the above a couple of times in Uni assignments, rolling my own queue in Java 4. Java 5 includes BlockingQueue, which probably would have saved me a bit of time.



Also, TRex EaterofCars and epswing thanks for the clarification on StringBuilder.

the onion wizard
Apr 14, 2004

setTimeout is a bit weird, I believe it always executes in global scope or something to that effect. Someone else can probably give you a proper explanation.

the onion wizard
Apr 14, 2004

CrusaderSean posted:

The temperature model does not call hasChanged() at all and it works just fine.

No, but is does call setChanged(), which means that any subsequent calls to hasChanged() will return true until clearChanged() is called.

Observable API posted:

public void notifyObservers(Object arg)

If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed.

Edit: To clarify, for now, you probably want to call setChanged before every call to notifyObservers.

the onion wizard fucked around with this message at 10:09 on Apr 29, 2008

the onion wizard
Apr 14, 2004

I'm not entirely clear on what you're trying to do, is that part of a JSP?

You should probably throw some apostrophes or quotes around your attributes, and the parameters for your form don't look right at all ('ACTION= METHOD=GET').

Edit: and your semicolon's inside the bracket in the onclick.

the onion wizard
Apr 14, 2004

drunkill posted:

I'm stupid aren't I?

Don't worry, it'll all come together and start making sense pretty quickly.

drunkill posted:

edit: I'm also JCreator to compile the code, but since I installed it at home today, when I run the program or file, I don't get the black box which I got at uni wehn using it. I get the build output so I can view any errors, but when at Uni the black box would pop-up and display the errors as well, if the program worked it'd be where you input stuff to make the program run etc. But for some reason it's not working at home.

I'm not familiar with JCreator, but are you able to compile and run your code from the command line?

eg:
javac Exercise1.java
java Exercise1

the onion wizard
Apr 14, 2004

adante, I'm fairly sure you'll need to create M readers, however you can service them with less than M threads.

I whipped this up for my own understanding, it might help point you in the right direction.

the onion wizard
Apr 14, 2004

adante posted:

if you mean I'll need to create M reader threads, then yes I agree. Otherwise.. well I still agree but you are reiterating the problem spec :)

No, that's not what I meant, in the example I gave earlier I had three threads servicing 15 objects.

I got bored and whipped this up. There's 5 threads servicing brs.length BufferedReaders.

the onion wizard
Apr 14, 2004

adante posted:

looks like 5 threads performing a SINGLE service of 15 BufferedReaders, once each. It also looks like if the first 5 BufferedReaders are not written to, but the remainder are, nothing will ever happen.

Here is some code which demonstrates. Try and run that against my M thread test example to see how it should work. If you write any more code, try it against the test code to see if it works.

I just took your original example and the code from the ExecutorService api and mashed them together. I didn't realise each reader needed to call service() more than once

If it's possible/likely for the readers to block forever, I agree that this solution isn't going to work. But, to be fair, your original solution with N threads will never terminate either.

Edit: I was pondering this further today, and I'm positive a thread pool won't work if the threads never terminate.

the onion wizard fucked around with this message at 12:39 on Aug 27, 2008

the onion wizard
Apr 14, 2004

The Coordinate class should probably implement Comparable<Coordinate>, or throw a ClassCastException if the supplied object isn't a Coordinate.
However, I don't think compareTo is the cause of your issue, as the worst that would do is overwrite existing keys when it's not supposed to (or vice versa).

You must be calling put with a null value at some point; I can't see any other way that there would be a null value for a key present in the map.

the onion wizard
Apr 14, 2004

Boz0r posted:

Ok, but when I've used wrap and gotten a ByteBuffer with the bytes in it, how do I get it out in the different types, as I still can't use the getLong(), getShort()... methods?

wrap and allocate appear to actually return an instance of HeapByteBuffer, which presumably implements those abstract methods.

the onion wizard
Apr 14, 2004

Sarah Sherman posted:

Also, did you guys ever have a teacher who forced you to code in a certain style? My Java teacher is forcing us to use Allman indenting (which I use anyway so it doesn't really matter), and having us put those large comments before each method or class. I was surprised that he is making us do this; I thought that an important aspect of programming was being able to code in your own style.

Yep, in one of my earlier classes we could lose up to 20% per assignment for incorrect formatting, and every class except the earliest couple would dock marks for missing or poor javadoc.

the onion wizard
Apr 14, 2004

MEAT TREAT posted:

My only counter argument to this, how often do you go back and look at your own code from semesters past?

These things don't really pay off while you're still at uni. But they're very important skills once in the workforce.

the onion wizard
Apr 14, 2004

sonic bed head posted:

I know that I can send a parameter using the include, but I don't know how best to use that parameter to control "text-decoration:underline." Thanks.

Your best bet would probably be to add a css class of some sort.

the onion wizard
Apr 14, 2004

I'm assuming printPoly() prints all of the Polynomial's nodes each time it is called? How many times is printPoly() being called in your example?

the onion wizard
Apr 14, 2004

Sarah Sherman posted:

however, for some reason it doesn't delete the node. This is how my teacher told the class to delete a node, but I guess it's not working. Any help?

MEAT TREAT posted:

You shouldn't compare objects with ==. Trying using the .equals method of the string.

if( direction.equals("E") )

the onion wizard
Apr 14, 2004

Clanpot Shake posted:

Is there a way to force the queue to reorder itself, or is it decided what item to pop off when poll() is called?
poll() will always return the head, I don't believe you can (or need to) force a sort.

Clanpot Shake posted:

Is it okay to dynamically add and remove items from a PQ and have it maintain order?
Provided only one thread at a time accesses the queue, yes.


It might be time to post more code...

the onion wizard
Apr 14, 2004

karuna posted:

The output of the whole thread is 2 but it is only meant to apply to the first 50 elements?

code:
	//Print array
	for(int i : list){
		System.out.print(list[i] + " ");
		
	}
Take another look at this section.

the onion wizard
Apr 14, 2004

osama bin diesel posted:

why? that is fine, it is printing out the same number 50 times because he is passing an int as x and only gets set once per thread

He's gotten his loops confused. He probably wants either:
code:
for (int i : list) {
	System.out.print(" " + i);
}
or
code:
for (int i = 0; i < list.length; i++) {		
	System.out.print(" " + list[i]);
}
Edit: or he's trying to print it in a random order, and I'm the one that's confused.

the onion wizard fucked around with this message at 11:30 on Oct 17, 2009

Adbot
ADBOT LOVES YOU

the onion wizard
Apr 14, 2004

SortedMap perhaps?

edit: oh, sorting on value, nevermind.

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