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
epswing
Nov 4, 2003

Soiled Meat

DholmbladRU posted:

However I want to implement this without the use of arrays.

Uh what? Why??

Adbot
ADBOT LOVES YOU

epswing
Nov 4, 2003

Soiled Meat

Kamakaze9 posted:

code:
for(int k=0; k <= accounts.length; k++)
  if(accnum == accounts[k].getId())
  {more code

Seeing accounts[k].getId() makes me skin crawl. Never let this kind of statement sit out in production, even if you're "pretty sure" you know what k will be. It should be something like

code:
Account a = accounts[k];
int id = a != null ? a.getId() : -1;
or more likely

code:
Account a = accounts[k];
if (a == null)
    throw new Exception("Account not found.");
int id = a.getId();
Anyways, your loop condition is k <= accounts.length which means k will equal accounts.length at some point which is out of bounds.

epswing fucked around with this message at 14:43 on Mar 24, 2011

epswing
Nov 4, 2003

Soiled Meat

DholmbladRU posted:

example of files I created to test

Edit in an example output file, based on your example input files.

epswing
Nov 4, 2003

Soiled Meat
Congrats, your software just crashed in production!

epswing
Nov 4, 2003

Soiled Meat
Oh please, you can say that about any exception ever. The point is to cut down on cases where they are more likely to occur.

I don't obsessively null-check every possible reference, but array[number].method() scares me. Am I really sure that (A) the array contains what I think it does, (B) the index is in bounds, and (C) the index is pointing to the object I want? Ok, I'm "pretty sure," but there are several things that can go wrong here. I'm just protected myself from myself.




vvv Edit: The -1 was just an example for his toy homework assignment. Maybe a bad one. Yeah.

Also, null may very well be a valid state.
Also, time to read more about assertions, thanks.

epswing fucked around with this message at 20:28 on Mar 24, 2011

epswing
Nov 4, 2003

Soiled Meat

Jabor posted:

You could, you know, check for null when you put something into the array. If you're inserting nulls into the array when they shouldn't be, your software is broken and you should crash instead of loving up important data by pretending everything's just dandy. Once your software is in a hosed-up state, crashing is better than most of the alternatives.

And if nulls are valid in the array, you're probably doing something wrong. If they're used to indicate an invalid object at that position, then you're probably better off with the null-object pattern, because then you have the option of attaching additional information later on about why it's invalid without having to rewrite everything that deals with that data.

Who says I put anything into the array? Maybe I was passed the array from some part of the program that some other guy in my company wrote. Or from some dll written by some other guy in some other company. What world do you live in where you magically know everything about the data you're receiving?

Look, all I'm saying is seeing array[number].method() while iterating makes me :crossarms: take a closer look.

epswing fucked around with this message at 15:35 on Mar 25, 2011

epswing
Nov 4, 2003

Soiled Meat

Geekner posted:

Also, you will not be proficient in Java after this

That's an understatement...

http://norvig.com/21-days.html

epswing
Nov 4, 2003

Soiled Meat

Jam2 posted:

I am (learning on the side) and I do (have the motivation). If it weren't for my 150% college course load and research obligations, I would spend every waking minute solving Euler problems and writing programs. I had an extra credit assignment for a Calculus class. I used logmein to write a monte carlo simulation from my iphone. It ran on the first try. I actually didn't even know it was called a monte carlo simulation until my professor graded it and gave me feedback. I'm a complete newb, but programming comes naturally to me and I really enjoy doing it. I want to do this for a living, so all of these plans are being made with this goal in mind.

I understand the view that it is not possible to become proficient in under X years. While I appreciate that feedback (and I have accepted the reality), no one here is really in a position to judge my intent/motivation/capability.

Ahh you'll be fine. People love to look back and chuckle from their ivory towers about how hard it was to get where they are now. They're not wrong, but I wouldn't fret, they did it and so can you. I think you're only mistake here was using the word "completely" which you've already understood and retracted.

To me, a couple of the key attributes of a worthwhile programmer are intelligence and curiosity, and from across the internet it seems you might have both, so keep at it and have fun.

epswing
Nov 4, 2003

Soiled Meat

tef posted:

I'd say that the following are more important:

Sure, I'll agree that attitude is also a key attribute, but I don't necessarily think it's "more important than" intelligence or curiosity. If any of these three are lacking, you're not going to get very far.


vvv Edit: Yep, I was just referring to tef's post above (which I just quoted now)

epswing fucked around with this message at 19:17 on Apr 8, 2011

epswing
Nov 4, 2003

Soiled Meat

tef posted:

actually no matter how smart you are or curious, if you're a dick to people, and can't write for poo poo, you won't get far. real software is written by groups, collaborating.

What did I just say. If you're lacking in any of those, you're not going to get far. I can obviously come back with "it doesn't matter how great your attitude is, if you're not intelligent and curious, you won't get far."

So I'm not sure why you posted.

epswing
Nov 4, 2003

Soiled Meat
So because I'm saying that a worthwhile programmer should have intelligence and curiosity on par with attitude, I have a childish naive view of programming?

Sorry but you're probably one of those dicks you were talking about earlier.

epswing
Nov 4, 2003

Soiled Meat

tef posted:

Programmers are frequently arrogant jerks.

Not in my experience...

tef posted:

Simply put the idea that a career (in java)

Who said anything about java. We happen to be in the java thread, but I haven't based anything I've written on that language.

epswing fucked around with this message at 17:45 on Apr 10, 2011

epswing
Nov 4, 2003

Soiled Meat

TheFatController posted:

For :eng101: I want to store the request body as an xml object

Grab jdom and jaxen, put them on the classpath, and go to town.

code:
Element root = new Element("root");
root.setText("here's some text");

Document doc = new Document(root);

XMLOutputter out = new XMLOutputter();
out.output(doc, System.out);
gets you
pre:
<root>here's some text</root>
(Completely untested)

epswing fucked around with this message at 03:45 on Apr 20, 2011

epswing
Nov 4, 2003

Soiled Meat

TaintedBalance posted:

code:
public class Creature {
	Object obj1, obj2;
	Profession myProfession = (Profession)obj1;
	Race myRace = (Race)obj2;

Can you explain what you're trying to do here?

epswing
Nov 4, 2003

Soiled Meat
Don't ask to ask, just ask! You'll either be ridiculed or aided or both. It's on the internet, what do you have to lose, really. :)

epswing
Nov 4, 2003

Soiled Meat

Hidden Under a Hat posted:

What's the best way to keep track of elapsed time in a loop while still reading a constant inputstream of data?

The loop reading from the device isn't where you want to keep track of time. You don't have control over how often readLine returns.

I would create two threads. One constantly loops over the output of the device, the other counts to 10, tells the first thread to gather data for a while, passes it to the ui thread for display, and counts to 10 again.

pseudocode:

code:
gather = false
line = ""
lines = new LinkedList()

while ((line = reader.readLine()) != null)
  if (gather)
    lines.append(line)
code:
timer = new Timer()
timer.interval = 10 seconds
timer.thingToDo = runMeWhenTimerElapsed;
timer.start()

void runMeWhenTimerElapsed()
  timer.pause()
  lines.clear()
  gather = true
  sleep for the number of seconds you want to gather data
  gather = false
  someDisplayMethod(a copy of lines)
  timer.start()
Note: There may be race conditions here that you need to take care of. I'm just vaguely illustrating how you might tackle this problem by moving the 10-second counter away from the read-from-device loop.

epswing
Nov 4, 2003

Soiled Meat
This should be some sort of UI benchmark. How hard (easy?) is it to append to the bottom of some UI control like a multiline textarea, listbox, datagrid, table, etc, and automatically scroll to the bottom.

I've run into this problem several times in several languages and it's infuriating :(

epswing
Nov 4, 2003

Soiled Meat

crazyfish posted:

Of course you'll probably have to handle the case where fc.close() throws an exception too but I don't feel like writing that out.

I usually write some sort of static 'silent close' method like...

code:
public class Util {
  public static silentClose(Closeable c) {
    if (c != null) {
      try { c.close(); }
      catch (IOException e) {}
    }
  }
}
...because I often don't care that close() didn't work. I think there's an apache library that contains something similar. So that finally becomes

code:
	finally
        {
            Util.silentClose(fc);
            Util.silentClose(fis);
        }

epswing
Nov 4, 2003

Soiled Meat

Thermopyle posted:

What is this called?

varargs

epswing
Nov 4, 2003

Soiled Meat

Ulio posted:

Ok thanks I got it after this.

This is what I wrote, seems to work

Success!

Although the readability of your code needs some work. Readability is very important, even though it looks like it doesn't matter in this small example. When you've written a larger program, and you want someone else to read/debug it, it helps immensely if all your brackets and indented code lines up nicely. Like this:

code:
	if (num1 % 2 == 0)
	{
		System.out.print("Number is even");
	}
	else
	{
		System.out.print("Number is odd");
	}
or this:

code:
	if(num1 % 2 == 0) {
		System.out.print("Number is even");
	}
	else {
		System.out.print("Number is odd");
	}
e: or this (thanks Adeptus):

code:
	System.out.print((num1 % 2 == 0) ? "Number is even" : "Number is odd");

epswing fucked around with this message at 04:47 on Jun 30, 2011

epswing
Nov 4, 2003

Soiled Meat

Ranma posted:

if(numbers.length < 0)

I haven't written java in a while, but I'm pretty sure this is wrong.

epswing fucked around with this message at 19:16 on Jul 5, 2011

epswing
Nov 4, 2003

Soiled Meat

Jam2 posted:

Harvard CTO, Jim Waldo is coming in to speak to my intro java class tomorrow. He was on the team that developed the Java programming language at Sun back in the day.

What question, if anything at all, should I ask him? Is there anything you guys want to know about Java or Sun?

So what did you ask?

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

epswing
Nov 4, 2003

Soiled Meat
For your particular situation what would have been your alternative?

epswing
Nov 4, 2003

Soiled Meat
Yep. JDOM!

Adbot
ADBOT LOVES YOU

epswing
Nov 4, 2003

Soiled Meat
What do you mean by "doesn't work"? What specifically isn't working? What are you expecting to see in that screenshot that you aren't seeing?

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