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
csammis
Aug 26, 2003

Mental Institution
Can one use IntelliJ or Netbeans if they're developing Java RCP applications that target the Eclipse framework itself, or does that lock one into having to use Eclipse? I'm doing this at work, but I can't friggin' stand Eclipse. It's slower than an rear end in a hat.

Adbot
ADBOT LOVES YOU

csammis
Aug 26, 2003

Mental Institution
Java 1.5:

I'm trying to figure out how to load a text file contained inside my program's JAR (into a FileReader if that matters). java.util.ResourceBundle was the first place I looked, but it seems to want to split the file into key/value pairs and that's not what I'm going for.

The JAR layout:
code:
src
  main
   +java
   |  various
   |    folders
   |      seriously
   |        ridiculously
   |          deep
   |            OhHereItIs.java
   +resources
      myfile.txt
How can I address this file? I know next to nothing about Java IO :smith:

csammis
Aug 26, 2003

Mental Institution

Ranma4703 posted:

I just started a co-op (my first job programming), and one thing I see in a lot of the code is the following:
private final int ONE = 1;
private final double FIVE = 5.0;


I always thought that was bad coding; why not just use 5.0 in the code. The only thing I can think of is if you want to be able to modify all references of "FIVE" in one location. However, it probably shouldn't be named "FIVE" if you want to do that, because if you change it to FIVE = 6.0, then the name of the variable isn't really useful anymore.

Is this bad coding practice, or am I just stupid? They also do multiple return statements in methods often, which I thought was another no-no.

It's strange, yes, and you're right to recognize it as odd. That said, they may very well have a good reason in whatever context it exists (though it had better be good for something like that). This happens a lot when you hit the Real World, it's not often black-and-white this-is-good-and-this-is-bad. Ask another developer why it is like it is, but don't sound like a stuck-up academic prick about it :)

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.

csammis
Aug 26, 2003

Mental Institution
This, this right here, is why schools that don't teach C/C++/[pointers] any more are causing Problems :colbert:

csammis
Aug 26, 2003

Mental Institution

CrusaderSean posted:

I'm reading up on design patterns and I saw this interface declaration:
public interface DrawingView extends ImageObserver, DrawingChangeListener {...stuff...}
I thought java doesn't support multiple inheritance?...

It doesn't for classes, but an interface can extend (and a class can implement) multiple base interfaces.

csammis
Aug 26, 2003

Mental Institution

shodanjr_gr posted:

Ive got a library that allows for the execution of Win32 Native Call via Java. However, i cant for the love of my life find a LIST of all the possible API calls. What i want to do, is open a file, or open the directory a file is in. (mind you that as far as native WINDOWS program goes, ive done very very little, so i dont know my way around it).

You're batshit insane if you want to do filesystem stuff via the Win32 API rather than Java (which, on Windows, just wraps the Win32 calls), but MSDN is your friend.

csammis
Aug 26, 2003

Mental Institution
Opening files in programs and opening Explorer to certain directories has absolutely nothing to do with the Win32 API. It's all about command line arguments to those particular programs. There's no comprehensive list, just google for the programs you want and how to open files with them via command line.

csammis
Aug 26, 2003

Mental Institution

shodanjr_gr posted:

If i go to the command prompt and do a "foo.jpg", the image will open in the default program that ive selected to handle that kind of file. I dont have to use the filename as an argument while running the program...(which seems pretty reasonable, since it would make any sort of search application impossible to develop - which is what i am making).

That's what i want to accomplish. But in java.

That only works because Windows Explorer has a file extension association to open things that end with .jpg by running "mspaint %filename%" :eng101:

Also, what the hell do you mean it would make a search application "impossible to develop" - how would you not have the filename as a search result? Maybe you need to be very clear about what you're doing.

csammis
Aug 26, 2003

Mental Institution

mister_gosh posted:

I'm not sure I understand the question, but why not just use a Java IDE like Netbeans (or Eclipse)? If you spell a variable wrong, it'll let you know.

Apologies if that's not what you meant and I'm stating the obvious.

He wants the code for a spell checker, not a spell checker for his code :v:

csammis
Aug 26, 2003

Mental Institution

Entheogen posted:

Perhaps there is an option in Eclipse to also add a new method to class and tell it that it is from some interface, and that should also copy/paste comments.

When you tell Eclipse to implement an interface, @see Javadoc tags are inserted that reference the interface, but it doesn't put in the comments verbatim. Alt+Shift+J on the method does the same thing.

Also why are you using @Override on an interface method?

csammis
Aug 26, 2003

Mental Institution
Implement a method StepTwo, make it a no-op in the base class, then override it in the children

code:
class ProfitModel
{
  public void StepOne() { SignUp(); }
  public void StepTwo() { /* ??? */ }
  public void StepThree() { CalculateProfit(); }
}

class AgricultureProfitModel extends ProfitModel
{
  @Override
  public void StepTwo()
  {
    PlantCrops();
  }
}

...

ProfitModel model = new AgricultureProfitModel();
model.StepOne();
model.StepTwo(); // plants crops
model.StepThree();
In C++ and C# that would be a virtual method.

csammis
Aug 26, 2003

Mental Institution

Triple Tech posted:

Also, I forgot to add, this block of code should never be called by itself or be exposed to the public. It only exists as part of the process. That's why I figured a plain old virtual method wouldn't be as classy as something more formal.

What sort of metric is "classy," and how are virtual methods informal? A virtual (abstract) method is exactly what you want here, and like 10011 said it'd just be protected if you didn't want callers to access it*.




* barring Java's retarded implementation of protected, there, I said it :colbert:

csammis
Aug 26, 2003

Mental Institution

mister_gosh posted:

What don't I understand here?

code:
public class abc {
  static String xyz;

  public void setXyz(String xyz) {
    this.xyz = xyz; 
  }
  public String getXyz() {
    return xyz;
  }
}
I have this abc class with a getter and setter (and other stuff). The parameter I pass to the setter should become the static variable value, so I'm using this.xyz = xyz. Is this wrong? Am I unclear on something?

Netbeans 6 throws a warning that this.xyz is "Accessing static variable". The same code in Netbeans 5 does not mention the warning.

(I could just call the parameter "x" and set it by saying xyz = x) but I want to call the variable what it is if possible)

It's wrong because xyz is static (it "belongs" to the class, not an instance of the class), so it doesn't exist in this. Technically you can reference it with this, but it's not semantically correct so Netbeans 6 promoted it to a warning. A static analysis tool like Findbugs would've flagged it. You can properly reference it with abc.xyz.

edit: And also you might be questioning your design when you have instance methods getting and setting a static member variable.

csammis
Aug 26, 2003

Mental Institution

Outlaw Programmer posted:

According to this site, the JVM will take care of any endian problems for you. I guess you only run into endian problems if your output is being read by a non-Java process (or your file is being generated by a non-Java process).

You could also run into problems if you were using the FloatBuffer to encapsulate data read from a binary-packed network stream, which would probably be in Network Byte Order (big endian).

csammis
Aug 26, 2003

Mental Institution

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?

What do you mean, you can't use the getLong, getShort, etc. methods? Any object returned by wrap or allocate will have those methods defined, even if what it's returning is an instance of the abstract base class. You're not supposed to need to know the precise implementation, HeapByteBuffer or whatever.

csammis
Aug 26, 2003

Mental Institution

Boz0r posted:

You, sir, are win.

Really? :crossarms:

quote:

Is it possible to assign the same value to two different destinations at the same time, like:

i=j=4;

or something?

Try it and see if a compiler error results. Should work just fine

csammis
Aug 26, 2003

Mental Institution

Boz0r posted:

Well, he knows more Java than me, so by that definition, most people in this thread are win.

What I meant was, didn't calling something "win" used to be probatable?

csammis
Aug 26, 2003

Mental Institution

Boz0r posted:

When I read the bytes, I don't get them in hex, I get the {00, 60, ff, ff} like {0, 96, -1, -1}. I'd like to work with them in hex, but I haven't really found a way to do that.

Um, this by itself is meaningless; -1 == 0xFF == 1111 1111 to the computer, numbers are numbers are numbers and the base doesn't matter. If you mean display them in the debugger as hexadecimal, that's one thing. If you mean put them into strings so that the characters represent the number in hexadecimal, that's another. What do you want to do?

Boz0r posted:

Also, how do I define a parameter to be an array of any primitive type instead of just one?

In Java, I don't think you can. The closest you can get is an array of object and let the primitive types get autoboxed.

csammis
Aug 26, 2003

Mental Institution

The Light Eternal posted:

But it creates repeats. I learned from #cobol that I need to add the numbers to a list and then shuffle the list. How do I do that?

I must not've been in #cobol at the time :v:

Shuffling isn't the answer here. You're creating a new Random on each iteration of the while loop. By default, the Random object is seeded by the system time, which in a fast loop will be the same on each iteration of the loop. Two Random objects initialized with the same seed are guaranteed to produce the same pseudorandom sequence. That's why you're getting repeats.

Always seed random objects outside of a loop unless you know why you're doing it any other way.

code:
Random r = new Random();
while (count <= num)
{
  int randint = r.nextInt(num);
  System.out.println(randint);
  count++;
}

csammis
Aug 26, 2003

Mental Institution

BELL END posted:

While that's true, it won't necessarily stop the same number coming up more than once.

True, though it's a hell of a lot less likely. Anyway, I suspect his "repeats" problem looks something like this:

4 4 4 4 4 4 4 4 4 4 4 4 7 7 7 7 7 7 7 7 7 7 7 7 3 3 3 3 3 3 3 3 3 3 3 3

Besides, how is shuffling an answer to "preventing repeats" in the first place? It might space them further apart from each other, but they'd still be repeated in the sequence. Seriously shuffling is just not a solution to anything that has to do with repeats, unless your requirement is that two identical numbers can't be next to each other - and even then it's not a guarantee.

csammis
Aug 26, 2003

Mental Institution

BELL END posted:

In my example (assuming it works) you're creating an array of [1, 2, 3, 4, 5...] and then shuffling it so you know the numbers are different.

I think that's what he was asking, maybe I'm reading the problem wrong.

The fact remains that shuffling is never ever ever going to eliminate repeats or reliably spread them around if your sequence has repeats in it in the first place.

I don't have a javac in front of me, but here's The Light Eternal's code in C#:

code:
Console.WriteLine("Enter number of players:");
int num = Int32.Parse(Console.ReadLine().Trim());
int count = 0;
while(count <= num)
{
    Random r = new Random();
    int randint = r.Next(num);
    Console.WriteLine(randint);
    count++;
}
This produces output like:
code:
Enter number of players:
4
1
1
1
1
1
code:
Enter number of players:
7
1
1
1
1
1
1
1
1
code:
Enter number of players:
2
0
0
0
Now if I seed it outside the loop:

code:
Enter number of players:
4
2
0
1
2
1
There, random numbers all below the value entered. There are still repeats in the sequence; shuffling won't solve that "problem," but I don't think that was the issue that TLE was having.

csammis
Aug 26, 2003

Mental Institution

BELL END posted:

I completely agree with what you are saying, I'm just saying that as far as I understand the problem, he needs to permute a list of distinct integers (or objects) to seed a tournament. You can't have the same team playing twice at the same stage, or leave teams out. Like I say I might be completely misunderstanding him.

I think #cobol (who uses IRC in this day and age?!) probably suggested he shuffle an array to get around this without noticing that he had his Random inside the loop.

If that's the problem to be solved then that makes perfect sense. I think I might have skipped over the "tournament" part and focused on the "random" - if he truly wants randomly placed distinct integers, the shuffle you posted is definitely the way to go and the Random should've been taken out of consideration entirely. #cobol really dropped the ball here <:mad:>

quote:

#cobol (who uses IRC in this day and age?!)

I do, when my cable modem can go five minutes without pissing itself :(

csammis
Aug 26, 2003

Mental Institution

The Light Eternal posted:

Shuffling should solve the problem. Here's what my output looks like now:
code:
Enter # of players:
8
3
4
5
3
2
1
6
7
I want it to look like this:
code:
Enter # of players:
8
4
5
2
3
1
6
7
8

Do exactly what BELL END posted, starting at 1.

e: unless you're trying to describe some hidden pattern, I can't tell if that '6 7 8' at the end was intentional. What precisely is it that you want? BELL END's method will get you a sequence of numbers from 1 to num, inclusive, that are shuffled randomly.

csammis
Aug 26, 2003

Mental Institution

The Light Eternal posted:

What do I put for the second List? I'm getting an error there that says something about List being a parameterized type.

What second list? There's only one in the code you posted. Is it a compiler error or an exception? Copy and paste it here.

e: Oh

code:
List<Integer> array = new ArrayList<Integer>();
ArrayList<E> is Java, right? e2: yes it is, that should work

csammis
Aug 26, 2003

Mental Institution

The Light Eternal posted:

ArrayList cannot be resolved to a type.

Make sure you're importing java.util, and also using Java 5 or higher.

ArrayList javadoc.

csammis
Aug 26, 2003

Mental Institution

zokie posted:

And I can't for the love of all that is unholy understand why you would want to use the get and set methods while still in the class.

What if your set method did this:

code:
public void setVariable(int variable)
{
  int oldValue = this.variable;
  this.variable = variable;

  // Alert registered listeners that the value of variable has changed
  for(IVariableChangedListener listener : variableChangedListeners)
  {
    listener.variableChanged(oldValue, variable);
  }
}
If you did what you suggest and accessed variable directly, the change listeners would never get notified, or you'd have to invoke the change listeners everywhere you set variable.

csammis
Aug 26, 2003

Mental Institution
import statements have to go at the top of the file. I don't know how it got into the middle, but that's very wrong; the compiler even says exactly that (illegal start of expression).


e: What probably happened is the author intended each class to be their own files.
e2: Or maybe not because you defined GameHelper to be an inner class. Was that the author's code or your own?
e3: Hell, your braces don't line up to anything that lets me figure out what you or the author were going for here. Post complete code if there's a problem with it.

csammis fucked around with this message at 22:29 on Jan 6, 2009

csammis
Aug 26, 2003

Mental Institution

Moon Whaler posted:

Yeah but telling me my program finishes 3ms before it starts seems a little off

Edit: Yeah, i'm subtracting the time before from the time after, it just sometimes gives negative results

Is it possible that the data type you're using to store the nanoTime result (or the subtraction result) is overflowing and wrapping?

csammis
Aug 26, 2003

Mental Institution

Huckle posted:

Have any ideas for the randomization feature? I can't just randomize it all - I need the computer to make random legal moves. I'm lost here.

code:
Generate solved puzzle
From set of squares adjacent to empty square, randomly choose one
Shift chosen square
Repeat for fifteen moves (or whatever)

csammis
Aug 26, 2003

Mental Institution

ayb posted:

I tried and it gave me a whole host of errors.

Exception in thread "main" java.lang.NoClassDefFoundError: Lab11a_me
cause by: java.lang.ClassNotFoundException: Lab1a_me
at java.net.URLClassLoader$1.run(Unknown Source)

and so on for a few more lines

This means there's either something wrong with your code or with the command line you're using to run it. Post both.

csammis
Aug 26, 2003

Mental Institution

capr1ce posted:

What I need to do is store it in a database, and then it needs to be searched against and displayed, and maybe edited in an html form. Is there anything I can do to convert this string back to the 硼 character in the Java code, or is it perfectly fine to put the HTML String into the database and search on that?

Think about this: if all 硼 characters appear to your application as &#30844, then when your user searches for "硼," your application will see "&#30844." Why would you need to convert it into something human-readable when it's the machine doing the searching?

csammis
Aug 26, 2003

Mental Institution

Warblade posted:

This question is kind of language agnostic, but I'm dealing with Java so maybe there's some kind of Java convention for doing this. Say you write a class that accepts some kind of listener. At some point you'll be iterating through all of your listeners to notify them that something significant happened. Maybe the method call is listener.sendEvent().

Is there some kind of best practice, or convention for what the sender and receiver should and shouldn't do?

I guess it would be good defensive programming to spawn a worker thread that actually calls listener.sendEvent(). But, as a receiver, should you also strive not to do things like sleeping for some indeterminate amount of time, or is it simply up to the sender to protect itself against that kind of situation?

I can't speak for Java, but in the .NET asynchronous model the responsibility for defensive programming rests with the receiver, as it can make no guarantees about the behavior of the sender. The sender should most likely not use a worker thread to raise events, because then the receiver has to contend with events happening out-of-expected-order (or the sender has to ensure that one event has been fully dispatched before raising another).

csammis
Aug 26, 2003

Mental Institution

Warblade posted:

I like putting a try/catch in the finally for closing streams. Except the exception handler doesn't do anything aside from maybe printing out some kind of warning. There's really no way to recover from that. You're closing the stream anyway. Just move on.

Which of course begs the question of why streams were engineered to throw exceptions on close in the first place, since you're right and there's basically nothing to be done at that point. This is something I've never figured out for myself but maybe there's a good reason v :) v

csammis
Aug 26, 2003

Mental Institution
I think rjmccall and HatfulOfHollow might know all that, and I think it might have been covered repeatedly in this thread :ssh:

rhag posted:

Please please, go back to CS101 and brush up on these very simple concepts. Come back after that and we'll talk.

Didn't call him a gay babby, still not up to AD's standards

HatfulOfHollow posted:

I loving love this argument. What round is this?

43 pages mod 5ish, probably round 8.

csammis
Aug 26, 2003

Mental Institution
Gonna throw out that a CS101 class that uses Java is absolutely worthless

csammis
Aug 26, 2003

Mental Institution
You write JUnit tests to ensure that calling the print method doesn't alter the set :colbert:


And the first Google hit on "java const correctness"

quote:

There is no way to declare that you will not modify the object pointed to by a reference through that reference in Java. Thus there are also no const methods. Const-correctness cannot be enforced in Java, although by use of interfaces and defining a read-only interface to your class and passing this around, you have a means to ensure that you can pass your objects around the system in a way they cannot be modified.

csammis
Aug 26, 2003

Mental Institution

zootm posted:

I suppose so. I guess if you're writing platform-specific code, though, the devs are just gonna have to have that platform. Interesting that there isn't an obvious way to do this generically, I guess domains maybe don't generalise all that well.

There are definitely cross-platform LDAP connectors, but I don't know of any in Java (the one my company uses is C). A little bit of native library + JNI could solve the problem.

csammis
Aug 26, 2003

Mental Institution
Actually I just realized I read his problem incorrectly...LDAP can tell you if a user exists in a domain (as long as it is based on Active Directory, I think) but I don't believe it can tell you if the user is logged in. Oh well :)

Adbot
ADBOT LOVES YOU

csammis
Aug 26, 2003

Mental Institution

fart factory posted:

Is there a way to remove all text from a Java Swing TextArea component? It seems like there should be, but on looking through the documentation I can't find a method to do so.

setText(""); ?

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