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
oh no computer
May 27, 2003

Ranma4703 posted:

They also do multiple return statements in methods often, which I thought was another no-no.
I've never understood the big hullabaloo about this.

Adbot
ADBOT LOVES YOU

oh no computer
May 27, 2003

Fehler posted:

Also, is it correct that Swing is the one with that ugly blue-silverish GUI? Any way around that?
That's the "Motif" look and feel. You can change the look and feel of Swing to whichever suits you best. If you go to your java folder and go to /demo/jfc/swingset2 you can run an applet that shows you some look and feel examples, although there are more than that available.

Edit: beaten

oh no computer
May 27, 2003

Whilst farting I posted:

but I can't find any information on how to make the program wait on a non-input dialog basis.
I too would love to know how to do this. I had a similar problem when doing my final year project for my CS degree, and I got around it by doing something like:
code:
while (!finished)
{
    // check to see if input has been given, if so set finished to true
    Thread.sleep(100); // to stop it using your CPU at 100%
}
but this is a hack and pretty bad practise. Something like how the Scanner class patiently waits for input would be perfect.

oh no computer
May 27, 2003

Drumstick posted:

this is what i have right now. My hope is that this will assign a random boolean to each location within the array. Will this function how I would like it to?
You'll want to set lifegame[a][b] on the last line, not i,h

oh no computer
May 27, 2003

clayburn posted:

It looks like I have another problem with my HashMap involving collisions. Looking at my data set, I know for a fact that collisions are happening, and this is causing problems for me. Every time that I search for a value at a specific key, I only get one of the values, where sometimes I may want the other value. For example, if "foo" and "bar" are stored at the same key, I will only ever get "foo" even though sometimes I may want to find any string that is not "foo." Is there any way to go about this? I have read the API entry for HashMaps over and over and cannot seem to find a solution to this.
I was under the impression that HashMaps don't allow more than one value to be stored with the same key, each subsequent call to put(k) overwrites what is already at k.

oh no computer
May 27, 2003

triplekungfu posted:

I'm not entirely clear on what you're trying to do, is that part of a JSP?
Looks like it's a javascript question and he's in the wrong thread (apologies if I'm wrong!) it's happened a few times in this thread already. Maybe zootm could add a quick "Java != Javascript" and a link to the web development thread to the OP?

oh no computer
May 27, 2003

The easist getaround for this is to move all your code from main() into the constructor, and then add a call to new ABC() in main().

oh no computer
May 27, 2003

http://www.devdaily.com/java/jwarehouse/jazzy/src/com/swabunga/spell/event/SpellChecker.java.shtml?

oh no computer
May 27, 2003

fletcher posted:

ArrayList<Double> pointsList = aggregatedPoints/dataSetSum;
You'd probably have to cast one or both of the variables as a double for it to return a double, this should work with Java5 or later.

oh no computer
May 27, 2003

code:
// Finds the nth fibonacci number
public BigInteger fib(int n)
{
	BigInteger fib1 = BigInteger.ONE;
	BigInteger fib2 = BigInteger.ONE;
	BigInteger temp;
		
	for (int i = 2 ; i < n ; i++)
	{
		temp = fib1;
		fib1 = fib2;
		fib2 = fib1.add(temp);			
	}
		
	return fib2;
}
Works fine while n is less than ~19600, but over this it wont work. I've just installed Eclipse (rather than Netbeans, which I'm used to) and it's not printing any errors in the console window, it just tries to run for a couple of seconds and says "terminated". Is it running out of memory or something? I swear I tried this exact same implementation a few months ago and got it well over a million.

oh no computer fucked around with this message at 14:11 on Aug 23, 2008

oh no computer
May 27, 2003

Ahh, it just seems to be a problem with writing stuff that big onto the console I think. I just created a text file and used a FileWriter to write the result to that and it works fine for much larger numbers.

oh no computer
May 27, 2003

MEAT TREAT posted:

I'm not sure if that's a real warning or just a false positive.
It's throwing the error because everything in the if block always gets executed, if an exeception is thrown or not. If an exception is ever thrown, input wont be given a value and thus remains null, but you're going to be calling equalsIgnoreCase() on it anyway.

oh no computer
May 27, 2003

Mill Town posted:

That won't catch a NullPointerException though. You'll have to add a second catch block for that.
You don't catch unchecked exceptions, you fix your code.

oh no computer
May 27, 2003

Capc posted:

For a small project at school we're supposed to round a number to the nearest 5 using Math.ceil().

As far as I can tell ceil just round up to the nearest integer. How can I make it so that it rounds to the nearest 5?
Divide by 5, call Math.ceil() on that to get an integer, then multiply the result by 5 - this would round up to the nearest 5 (if that was what you meant). If you want to get to the nearest 5 you'd have to use Math.round() instead of Math.ceil().

oh no computer
May 27, 2003

Try it using the full path, like "C:\\Images\\pumpkin.jpg" (or whatever) if you're on Windows.

I think if you don't it needs to be in the same directory as the .class file (bin?)

oh no computer
May 27, 2003

Lazlo posted:

When you print a Calc object, System.out.println calls its toString() method. Since you haven't defined one yourself, it calls the toString method from Object (which all java objects inherit), which (I believe) is the hash code of that object.

Basically if you want to be able to print the object in a way that makes sense, you need to define your own toString() method in Calc:

public String toString()
{ return name; }

or whatever best suits your needs.

oh no computer
May 27, 2003

Collections.shuffle() shuffles a List object. You'll probably want an Arraylist.

oh no computer
May 27, 2003

Not at a compiler so I can't check this, but something along the lines of:

code:
ArrayList<Integer> array = new ArrayList<Integer>(num);
for (int i = 0 ; i < num ; i++)
{
    array.add(i)
}
Collections.shuffle(array);

oh no computer
May 27, 2003

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

oh no computer
May 27, 2003

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.

oh no computer
May 27, 2003

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.

There are other ways to achieve this, I was just showing him how to use Collections.shuffle().

oh no computer fucked around with this message at 20:36 on Nov 4, 2008

oh no computer
May 27, 2003

List is an interface, you can't instantiate it. What errors was my code throwing out of interest?

oh no computer
May 27, 2003

Really common beginner error, never use == on Strings. Use .equals() :)

Edit: as in if (choice1.equals("yes") || choice1.equals("Yes"))

oh no computer
May 27, 2003

I haven't seriously coded in Java for about 10 years, but I'm looking for a new job in the next few months so I'd like to get back into it in case any Java Dev jobs come up (that I'll still probably get rejected for due to the aforementioned lack of recent experience).

I still have a couple of Java programming books I could use to refresh my memory, but they are for Java 5, and a cursory Google shows that Java is now on version 10. Has the language changed enough that it would be worth me looking into using some more up-to-date learning materials, or would I be alright re-learning from the books I have and then just learning about the more recent changes afterwards?

oh no computer
May 27, 2003

I have Core Java by Cay Horstmann which is written for people already familiar with programming, though not necessarily OOP so it does go over those concepts again. It also has lots of notes throughout as to how Java differs from C++ if that's helpful.

There are two volumes (Fundamentals and Advanced Features) and they are quite big but most of it is code examples that can be skimmed. He also has a version which apparently combines the two volumes into a condensed version called Core Java for the Impatient, though I don't know if that's any good.

oh no computer
May 27, 2003

FYI if you do go for that Heads First book note it's currently in a humble bundle with a bunch of their other books in the $15 tier: https://www.humblebundle.com/books/head-first-books?hmb_source=navbar&hmb_medium=product_tile&hmb_campaign=tile_index_2

oh no computer
May 27, 2003

I'm trying to get back into Java coding and to this end I'm making a clone of the game 2048. I have a working console version and I'm just trying to build a simple GUI for it, but it's been years since I've done any GUI stuff and it's not working properly.

I have a class GameTile that extends JPanel and whose paintComponent method that just draws a String (which is the value of the tile) and a thin rectangular border. I have a GameBoard which extends JPanel and is set up to use a 4x4 GridLayout, this panel is then added to a JFrame's contentPane.

GameBoard stores the state of the board as a 2D GameTile array where each element either points to a GameTile object or null. The paintComponent method of the GameBoard is below:
code:
public void paintComponent(Graphics g)
{
    removeAll(); // clear the panel of the previously added tiles
    for (GameTile[] row : board)
    {
        for (GameTile tile : row)
        {
            if (tile != null)
                add(tile);
            else
                add(new JPanel());
        }
    }
}
i.e. each non-null GameTile object in the array is added to the GameBoard panel, each null reference just adds a dummy JPanel in order to skip to the next cell of the GridLayout. This correctly draws the board when the program starts up, showing the two initial tiles placed in a random location on the 4x4 grid.

GameBoard also implements KeyListener to listen for up, down, left or right arrow keys, and calls the move method for that direction before repainting:
code:
public void keyPressed(KeyEvent e)
{
    switch (e.getKeyCode())
    {
        case 37: // LEFT KEY
        {
            board.move(0);
            break;
        }
        case 38: // UP KEY
        {
            board.move(1);
            break;
        }
.... etc etc
    }
    repaint();
}
But as you press the keys, the only tiles showing on the screen are the two initial ones, no new tiles are added. If you make sufficient moves that cause one of the initial tiles to merge with another tile, the value updates accordingly but the location of that tile never changes. However if you minimise or resize the frame, it updates to show the correct status of the board.

I know that this means that the call to repaint() above isn't doing what I think it's doing and I should be calling repaint() somewhere else but I really can't think where. Since resize and minimise fix it I thought that maybe I should be repainting the JFrame, but I tried adding a manual call to that but it didn't help.


edit: revalidate() you loving idiot

oh no computer fucked around with this message at 16:33 on Feb 20, 2019

oh no computer
May 27, 2003

Yeah I'm not trying to make games only getting some general practice in with GUI stuff.

Although on a related note I haven't really coded in Java since version 5 - is Swing completely dead now? If so I'm guessing JavaFX has replaced it? I guess I should probably look into learning that?

Adbot
ADBOT LOVES YOU

oh no computer
May 27, 2003

Edit: nevermind, worked it out

oh no computer fucked around with this message at 13:51 on Apr 10, 2019

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