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
Nippashish
Nov 2, 2005

Let me see you dance!

Puppet Pal Claudius posted:

Is there another way to check if a rectangle is being selected? Or at least some way for my program to know which rectangle is above another?

If you don't keep track of it then neither of your rectangles is above the other. If you do keep track of it then your problem is solved because you can just check which one is on top.

Adbot
ADBOT LOVES YOU

Schism.
Mar 19, 2009
Absolutely losing it here, any ideas how I can write these results to a text file? I've spend several hours trying to get it to work and this is my last hope

code:
public void save() {   File file = new File("fgfg.txt");

            try {
               FileWriter writer = new FileWriter(file);
               for (Observable g : GameBoard.yellowObjects)
                  Writer.write(??????)
                  
                  
               }
   
    catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
       }
      
      
         
}
}

yellowObjecs being this
static ArrayList<Observable> yellowObjects;
Looks like write only works with ints or something, genuinely no idea what's going on

Schism. fucked around with this message at 21:05 on Mar 13, 2011

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
You can only really write out characters, and sequences of characters.

Thankfully, there is a built-in way to convert any arbitrary object to a sequence of characters - the toString method.

Most standard-library types that you'd want to print have a reasonable implementation of it already, if you want to print out a class that you've defined then you'll need to override the default with something that's reasonable for your class.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Schism: For the love of god indent your code properly.

code:
public void save() {
	File file = new File("fgfg.txt");
	try {
		FileWriter writer = new FileWriter(file);
		for (Observable g : GameBoard.yellowObjects) {
			writer.write(g.toString());
		}
		writer.close();
	}
	catch (Exception e) {
		e.printStackTrace();
	}
}
Also close() anything that deals with an OutputStream when you're done with it.

deviler
Feb 20, 2007

Let's take this star craft out of dry dock, shall we?
Dunno where else I'd ask, but does anyone do Android development or have experience with the ADK and ADK plugin for Ecplise? If this is the wrong thread, could I have a finger point in a better direction?

If this is the right place, I think my problem is a simple one since I'm just starting out. So far it's been a pretty simple procedure getting Eclipse and setting it up with the Android SDK. I've got as far as being able to write this simple Hello World application, but when I compile, it starts the virtual device (Android emulator), but never displays the application. As far as I know I have everything set up correctly in Eclipse and the SDK manager.

Once I know if I'm asking the right crowd or not, I can elaborate more.

Thanks!

Schism.
Mar 19, 2009
Thanks guys, got it working. Shouldn't have been loving around with it at 1am last night :ughh:

brosmike
Jun 26, 2009

deviler posted:

Dunno where else I'd ask, but does anyone do Android development or have experience with the ADK and ADK plugin for Ecplise? If this is the wrong thread, could I have a finger point in a better direction?

There is a separate thread for Android development, yes. It's not particularly active, but someone will probably help you if you have a specific question.

deviler posted:

...but when I compile, it starts the virtual device (Android emulator), but never displays the application. As far as I know I have everything set up correctly in Eclipse and the SDK manager.

Try looking at the Console window in eclipse (this will tell you if it had a problem installing the application to the emulator) and at the LogCat window (LogCat is the tool for looking at the logs from the android device/emulator itself, so this will give you a stack trace if your program is installing right but crashing immediately or anything like that).

I often find that if the emulator isn't running already when I try to run an app, Eclipse just gives up on running the app since startup takes so long. You might try running the app again once the emulator is fully loaded. If you have a real device, that's usually much more useful for testing with - the emulator is pretty slow.

deviler
Feb 20, 2007

Let's take this star craft out of dry dock, shall we?
Hey, the problem was I just wasn't patient enough! Thanks, that was actually what was up. Also thanks for the link to the thread. I looked and looked but didn't see anything. I'm sure I'll be using it a lot as I work on more stuff :)

chippy
Aug 16, 2006

OK I DON'T GET IT

deviler posted:

Hey, the problem was I just wasn't patient enough! Thanks, that was actually what was up. Also thanks for the link to the thread. I looked and looked but didn't see anything. I'm sure I'll be using it a lot as I work on more stuff :)

Yeah the emulator takes aeons to actually start up and run your program.

Sab669
Sep 24, 2009

This is a pretty simple (I think) question, since I'm pretty retarded.

code:
    ArrayList<Dog> dList = new ArrayList<Dog>(numDogs);
    
    for (int i = 0; i < numDogs; i++)
    {
        Dog d1 = new Dog();        
        
        ....

        dList.add(d1);
    }
    
    Collection.sort(dList);

In the for loop I just have the user enter the the data for the Dog class.
The API says to use sort I need to implement the Comparable interface, which Dog does. But when I do that, I get an error saying

"Dog is not abstract and does override abstract method compareTo(java.lnag.Object) in java.lang.Comparable"

So yea, I'm trying to just alphabetize (and then display) an Array List. Not really sure what I'm doing wrong.

Paolomania
Apr 26, 2006

Sab669 posted:

In the for loop I just have the user enter the the data for the Dog class.
The API says to use sort I need to implement the Comparable interface, which Dog does. But when I do that, I get an error saying

"Dog is not abstract and does override abstract method compareTo(java.lnag.Object) in java.lang.Comparable"

So yea, I'm trying to just alphabetize (and then display) an Array List. Not really sure what I'm doing wrong.

When you tell the compiler that "Dog implements Comparable<Dog>" you are agreeing to implement all the methods defined by that interface. Because Comparable<T> is a generic interface you must specify what your Dog class is comparable to, and thus are obligated to implement the compareTo(T) method for the type you specify. That compiler error looks like you probably defined Dog as "Dog implements Comparable<Object>" and implemented "public int compareTo(Dog d)". When you say "implements Comparable<Object>" you are telling compiler that you are going to implement "public int compareTo(Object o)" as opposed to "public int compareTo(Dog d)". Make sure you said what you actually meant which is probably "Dog implements Comparable<Dog>".

Eflas
Sep 24, 2010
While learning Java for the first time, I had a chance to compare one of my lab assignments to the suggested solution. This is just the error-checking portion of the whole code that I have a question with. It is supposed to keep asking for an input if the input is not in the desired range.

code:
//my version
boolean retry = true;
do {
  //get input
  if (desired input)
    retry = false;
} while (retry)

code:
//suggested version
while (true) {
  //get input
  if (desired input)
    break;
}
As can be seen, both versions do essentially the same thing. However, the suggested version while seemingly more compact and neat, is obscure to me at first glance. Can anyone tell me how does the suggested version work? Thanks in advance!

baquerd
Jul 2, 2007

by FactsAreUseless

Eflas posted:

code:
while (true) {       //loop forever 
  //get input
  if (desired input)
    break;           //stop the loop I'm getting off!
}
Can anyone tell me how does the suggested version work?

It loops until the input gathered in the loop matches the desired input, then breaks out of the loop. As a professional, I would generally prefer and I am used to seeing the suggested version for neatness and readability.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.

Eflas posted:

As can be seen, both versions do essentially the same thing. However, the suggested version while seemingly more compact and neat, is obscure to me at first glance. Can anyone tell me how does the suggested version work? Thanks in advance!
break terminates execution of a loop immediately. So in that while loop, it constantly gets the input until it meets the desired condition. Immediately upon it reaching that condition, it exits the loop.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
If you've read 'Loop Patterns' you'll recognize that idiom as "Loop and a Half".

Contra Duck
Nov 4, 2004

#1 DAD
In my workspace right now there are 6,181 java source files and I can't find a single instance of a do/while loop. Do/while works perfectly well in your case, and you certainly wouldn't be marked down on it if you used it in an assignment, but in general the while/break idiom is more powerful so people tend to use it for everything.

Diametunim
Oct 26, 2010
I was wondering if anyone could recommended me some good Java books to sharpen my skills with. I find myself having to refer back to old code that I've written previously to remember how to do the easiest of things and it's really getting old, especially since I plan on majoring in computer science when I go off to college. The only book I have right now (and I can't even find it) is the standard computer science book written by Leon Schram that was given to me when I started Computer Science 1 two years ago.

mcw
Jul 28, 2005

Diametunim posted:

I was wondering if anyone could recommended me some good Java books to sharpen my skills with. I find myself having to refer back to old code that I've written previously to remember how to do the easiest of things and it's really getting old, especially since I plan on majoring in computer science when I go off to college. The only book I have right now (and I can't even find it) is the standard computer science book written by Leon Schram that was given to me when I started Computer Science 1 two years ago.

Well, it depends on what you're looking to do with Java. I personally love Effective Java, but that's more of a general book. You might prefer to read about a specific Java technology in a recipe-style book, like Spring Recipes.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Java Puzzlers is also a great way to learn more about the language, attacking the issue from a different angle. The book's site has some pretty intriguing samples to try out if you think you might be interested. This book might not teach you everything you want to know, but it will expose gaps in your understanding and push you to seek out more information.

Diametunim
Oct 26, 2010

MariusMcG posted:

Well, it depends on what you're looking to do with Java. I personally love Effective Java, but that's more of a general book. You might prefer to read about a specific Java technology in a recipe-style book, like Spring Recipes.

Thanks, I'm definitely looking for a more general Java book right now to refresh the skills that I already know. The most complicated thing I've ever put my head into was advanced swing menus. The most complicated thing I've ever written was a simple version of battleship and even that didn't work correctly so I'm definitely still looking for more basic (or what I consider to be basic?) books.

Internet Janitor posted:

Java Puzzlers is also a great way to learn more about the language, attacking the issue from a different angle. The book's site has some pretty intriguing samples to try out if you think you might be interested. This book might not teach you everything you want to know, but it will expose gaps in your understanding and push you to seek out more information.
Thanks, I'll definitely look into those. I've been meaning to post the pastebin links for the Zelda code I asked about I just haven't had time, but here they are.

Sound
Sound 2
Loop Sound

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Diametunim: I took a look at your "Loop Sound" class. First, I went through and tried to simplify it as much as possible. I've mainly removed some variables and methods you didn't use and collapsed together all the exception handling stuff. I think this makes it a bit easier to follow the logic of the code. You will also notice I restructured your main loop using "Loop and a Half" as discussed earlier.

http://pastebin.com/S6yWMR6H

Your approach to looping the audio seems kinda sketchy- you just keep instantiating new LoopSound objects over and over. After a little poking around, I found the Clip class, which may already do exactly what you want:

http://pastebin.com/fFQEKGdR

Obviously at this point you don't really need a "LoopSound" class in the first place. This approach probably buffers the entire audio file in memory, so if this poses a problem you may have to get more creative- does anyone else have much experience with audio in Java?

Paolomania
Apr 26, 2006

I just want to take a moment to express ARRRGH at the ecj compiler for breaking javac's nice clean NEW-DUP-init constructor idiom. Having poo poo like DUP_X1/SWAP/etc. messing with the stack between a bunch of oddly nested NEWS and inits for who knows what silly constructor call optimizing sure makes your bytecode a bitch to instrument, so thanks, ecj.

Wronkos
Jan 1, 2011

Swing has always been my weak point in Java. I need more practice, and as such I was wondering if there were any decent e-books or sites that could help.

Wronkos fucked around with this message at 23:26 on Mar 22, 2011

DholmbladRU
May 4, 2006
I am trying to come up with a way to merge two 'database' files which will be contained in ~ delimited .txt format. First I want to read in a header line which is the column headers, then compare and join on common columns. However I want to implement this without the use of arrays.

Does anyone have any ideas on how to approach this? I thought about implementing a hash table of some sort.

DholmbladRU fucked around with this message at 03:09 on Mar 23, 2011

epswing
Nov 4, 2003

Soiled Meat

DholmbladRU posted:

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

Uh what? Why??

DholmbladRU
May 4, 2006

epswing posted:

Uh what? Why??

Wouldn't that reduce scalability? If a file I am trying to read has 10million rows, the program might not be able to handle this. Could be wrong, Im not the greatest programer.

DholmbladRU fucked around with this message at 03:31 on Mar 23, 2011

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

DholmbladRU posted:

Wouldn't that reduce scalability? If a file I am trying to read has 10million rows, the program might not be able to handle this. Could be wrong, Im not the greatest programer.

I would load them as separate tables in a database like SQLite and then you can JOIN the two tables.

edit: after reading how you wrote the original question it almost sounds like you are asking about a homework assignment

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
Maybe you can be a bit more descriptive. What is your goal? Why aren't you bulk loading this into a relational database? How wide are your rows? How big is the file in bytes? Are you arbitrarily sorting or is it known at compile time?

Kamakaze9
Jun 11, 2005
Hey you guys!!
I'm currently working on a class for school that asks me to simulate and atm machine. While using an array of 10 accounts. The accounts only contain an id number and a balance. My issue is when I ask the user for their account number I am trying to compare it to the id (which happens to be the same number as the index of the array it's in e.g. 0-9) If it does not match any of the id numbers It should ask them to enter a valid account number. Instead it throws an ArrayOutOfIndex error, which leads me to believe that I am not actually looking at the id numbers but the array index numbers. Here is the relevant code. Let me know if you need the entire code and I'll edit it in.

code:
import java.util.Scanner;

public class TestAccount
{
	public static void main (String[] args)
	{
		Scanner input = new Scanner(System.in);

		int option;
		int accnum;
		double amt;
		int count = 0;

			//Create 10 accounts.
		Account accounts[] = new Account[10];

			//initailize the array and account information
		for (int i = 0; i < 10; i++)
			accounts[i] = new Account(i, 100);

			//Interface
		while (count != -1)
		{
				for (int j=0; j<30; j++)
					System.out.println(" ");
			System.out.println("Please enter your account number.");
			accnum = input.nextInt();

			if(accnum == accounts[accnum].getId())
			{
				System.out.println(" ");
				System.out.println("   Main Menu  ");
				System.out.println(" ");
				System.out.println("1. Check Balance");
				System.out.println("2. Withdraw");
				System.out.println("3. Deposit");
				System.out.println("4. Exit");
				System.out.printf("Please enter the number of your selection. ");
				option = input.nextInt();
				System.out.println(" ");

				while (option != 4)
				{
					switch (option)
					{
						case 1:
							System.out.println("        Your balance is: $" + accounts[accnum].getBalance());
							option = 0;
							break;
						case 2:
							System.out.println("How much would you like to withdraw? ");
							amt = input.nextDouble();
							accounts[accnum].withdraw(amt);
							System.out.println("        Your new account balance is: $" + accounts[accnum].getBalance());
							amt = 0;
							option = 0;
							break;
						case 3:
							System.out.println("How much would you like to deposit? ");
							amt = input.nextDouble();
							accounts[accnum].deposit(amt);
							System.out.println("        Your new account balance is: $" + accounts[accnum].getBalance());
							amt = 0;
							option = 0;
							break;
						case 4:
							System.exit(0);
					}
				System.out.println(" ");
				System.out.println("   Main Menu  ");
				System.out.println(" ");
				System.out.println("1. Check Balance");
				System.out.println("2. Withdraw");
				System.out.println("3. Deposit");
				System.out.println("4. Exit");
				System.out.printf("Please enter the number of your selection. ");
				option = input.nextInt();
				System.out.println(" ");
				}


			}
			else
			{
				System.out.println("Invalid account number: Please enter a valid account number");
			}
			count++;

		}
	}


}

Kamakaze9 fucked around with this message at 18:31 on Mar 23, 2011

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
:stare:

quote:

if(accnum == accounts[accnum].getId())
you're accessing an array here, so this is where I assume you're going outside the index

and if I understand you right: You're checking to see if the given number, i.e. 9, is equal to array[9].getId(), which should be 9?

Kamakaze9
Jun 11, 2005
Hey you guys!!

Aleksei Vasiliev posted:

:stare:

you're accessing an array here, so this is where I assume you're going outside the index

and if I understand you right: You're checking to see if the given number, i.e. 9, is equal to array[9].getId(), which should be 9?

That's correct. And I figured that's where the issue was, I wanted to make sure that I was in fact looking at the id number, not the array index number, and if I am. How to make it ignore the fact that I am out of the index and just go to my else statement. Instead of closing out program.

poemdexter posted:

I assume all of this is within a for loop?

Yes, It all seems to be working, except when I enter a number that is outside the array. Again, I can post all the code if you need me too.

chippy posted:

Well you could throw a try-catch around it and handle the ArrayOutOfIndex of exception that way, but you shouldn't be using exceptions for flow control.

Can't you just check (if accnum < accounts.length) and act accordingly?

I could but then it is only looking at the array index and my goal is to compare the id number. For more realistic issue of the account numbers being different than the array index. Maybe I'm being too anal, obviously a real world wouldn't be using arrays anyway.

Kamakaze9 fucked around with this message at 18:27 on Mar 23, 2011

poemdexter
Feb 18, 2005

Hooray Indie Games!

College Slice
I assume all of this is within a for loop?

chippy
Aug 16, 2006

OK I DON'T GET IT
Well you could throw a try-catch around it and handle the ArrayOutOfIndex of exception that way, but you shouldn't be using exceptions for flow control.

Can't you just check if (accnum < accounts.length) and act accordingly?

edit: I think we better see more of your code, I'm not sure what you're doing here.

quote:

if(accnum == accounts[accnum].getId())

Surely this is always going to be true, as long as you haven't gone out of range of the array?

chippy fucked around with this message at 18:26 on Mar 23, 2011

poemdexter
Feb 18, 2005

Hooray Indie Games!

College Slice

Aleksei Vasiliev posted:

:stare:

you're accessing an array here, so this is where I assume you're going outside the index

and if I understand you right: You're checking to see if the given number, i.e. 9, is equal to array[9].getId(), which should be 9?

Also, Aleksei has a point. If you only have 0-9 and someone puts in 20, your if statement will actually try to access the accounts[20] and throw an out of bounds error.

Maybe:

code:
int userInput = theirNumber;

if (userInput <= accounts.length) {
do your code
}

chippy
Aug 16, 2006

OK I DON'T GET IT
Also you're never going to hit case 4 there, because that switch statement won't be executed if case = 4.

Kamakaze9
Jun 11, 2005
Hey you guys!!
I see what you're saying about the case 4, I'll fix that. And now that you point it out I realize that I am always looking at a specific index. I would like to go through each one and compare just the id numbers, but I'm not sure how to go about that.

Also, I have edited my first post with the full code.

poemdexter
Feb 18, 2005

Hooray Indie Games!

College Slice
If someone puts in 20 as their account, you are still getting array out of bounds errors. Do:
code:
if (accnum <= accounts.length && accnum > -1) {
   logics...
}
This will in fact get rid of the need for:
code:
if (accnum == accounts[accnum].getId()) {
   logics...
}
As long as accnum is 0 or greater up to the length of the array, you will always get a legit account.

Kamakaze9
Jun 11, 2005
Hey you guys!!
Not quite what I wanted to do with it, but it will work and I'll use it unless I figure out how to accomplish my goal or at least how to better articulate it. Thanks for all the help.

Also, is there a more specialized irc channel you use or does everyone pretty much hang out in cobol.

Luminous
May 19, 2004

Girls
Games
Gains

Kamakaze9 posted:

Not quite what I wanted to do with it, but it will work and I'll use it unless I figure out how to accomplish my goal or at least how to better articulate it. Thanks for all the help.

Also, is there a more specialized irc channel you use or does everyone pretty much hang out in cobol.

People are getting caught up on the array issue, and the fact that, in this case, the array index corresponds to an id.

If, for instance, the Accounts were a Collection<Account> then 1) the idea that the array index happens to also be the account id disappears, and 2) people would probably be giving you ideas for searching that collection.

For instance, the most basic search, a simple linear search, would be to just loop through every Account in your collection, checking if the id you're looking for is the same as the id of the account currently being evaluated. If it is, return that account. If not, continue on until all of the accounts have been evaluated - in which case, you go to your error case of asking the user to enter a valid account id.

Adbot
ADBOT LOVES YOU

poemdexter
Feb 18, 2005

Hooray Indie Games!

College Slice
Yah, accounts with unique ids are just begging for a hashtable to be used.

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