Couple Java Questions

Zachary Boddy

Staff member
Aug 3, 2014
2,389
13
38
www.windowscentral.com
Hey guys I'm a relatively new Java programmer (if you can even call me that yet) and I have a few questions. I've been collecting all my useful methods and such and compiling them into a utility class and I was just wondering if there were better ways to do the things I'm doing. More specifically:

public static int enterInt()
{
Scanner input = new Scanner(System.in);
int t = input.nextInt();
return t;
}
I'm wondering if there's a better way to ask the user for an integer and then return that value to the user. And:

public static void delay(int n)
{
long startDelay = System.currentTimeMillis();
long endDelay = 0;
while (endDelay - startDelay < n)
endDelay = System.currentTimeMillis();
}
I'm wondering if there's a better way to do a delay. I want a reliable delay. Also:

public static void drawCircle(Graphics g, int x, int y, int r)
{
int d = r * 2;
g.drawOval(x - r, y - r, d, d);
}
This is how I currently draw a circle. That's all for now. Thanks in advance.
 

cmakthat

New member
Nov 14, 2014
23
0
0
Visit site
Hey Zachary,

For enterInt(): Looks good to me. Short and sweet functions are always nice. Looks like you could use a "BufferReader" too, but the way you've done it is a bit simpler.

For delay(): I might've gone with using "Thread.sleep(n)" instead. If you're just building a console input type application, then it should be fine. If you're goal is to do mobile development eventually, usually delaying the code like that isn't the best idea because it could freeze the application and the user won't know what's going on. What exactly are you using the delay for?

For drawCircle(): Again, short and sweet. As long as it works, I don't see a problem with this function.

Hope this was helpful for you.
 

Zachary Boddy

Staff member
Aug 3, 2014
2,389
13
38
www.windowscentral.com
Hey Zachary,

For enterInt(): Looks good to me. Short and sweet functions are always nice. Looks like you could use a "BufferReader" too, but the way you've done it is a bit simpler.

For delay(): I might've gone with using "Thread.sleep(n)" instead. If you're just building a console input type application, then it should be fine. If you're goal is to do mobile development eventually, usually delaying the code like that isn't the best idea because it could freeze the application and the user won't know what's going on. What exactly are you using the delay for?

For drawCircle(): Again, short and sweet. As long as it works, I don't see a problem with this function.

Hope this was helpful for you.

Hey thanks for your advice. I'll keep the enterInt and drawCircle methods the same. About the delay method, I was planning to use it as a general "go-to" delay method for, well, anything. Recently I've been playing with simple animations and I've been using that delay method to do it. If there's a better, more reliable way to do a delay method I'd love some more detail on the topic.
 

Members online

No members online now.

Forum statistics

Threads
323,251
Messages
2,243,520
Members
428,049
Latest member
velocityxs