Damien O Sullivan

Ranch Hand
+ Follow
since Oct 07, 2014
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Damien O Sullivan

Hi,

I'd like to know is there a way to restrict a user from inputting certain characters into a TextField?

Is there something like isNumeric() available?

In Visual Basic, I was able to prevent the user entering certain characters of my choice into each textbox, however I've looked and I haven't been able to find a solution.
I've tried using the following else if as part of an if statement:

but it doesn't seem to work.

I'd appreciate any feedback I receive.

Thanks!
10 years ago

Junilu Lacar wrote:Great! So the first thing I look for in code is duplication. Duplication is where the same, or very similar, commands are repeated throughout your program. Find something that has been duplicated in your code. You will then refactor that duplicated code by extracting it to its own method.

Refactoring is covered by these screencasts:

https://www.youtube.com/watch?v=OrsdvCndArY - introduction to refactoring
https://www.youtube.com/watch?v=JZn4hlMvP60 - refactoring - extracting methods

Watch these screencasts and tell me which duplicated instructions in your program you'd like to refactor first.



So is the fact that I have two Try Catch Blocks duplication?
10 years ago

Junilu Lacar wrote:Ok, create a new Java project. Here's a step-by-step guide that you can follow: https://www.youtube.com/watch?v=zezOt235nSc. Feel free to change the names where appropriate.

For example, when the screencast tells you to create a new project "Whats UP program" just give it whatever name you like. When it tells you to give a name for the new class you're creating, just give it the name of your class. Then copy your code and paste it over whatever Eclipse creates for you by default. Remember that spelling and capitalization matter, so be sure to get that right. Save the file and run your program. You'll need to stop the program manually since you didn't write any code to do that from the program. To do that, you need to find a red square icon on the menu bar. It will look like the STOP button on your video player, and it should be near a green circle with a white triangle that points to the right (like a PLAY button). Click the STOP button to stop your program.

Once you get this done, we should be ready to start refactoring.



I've done all that, so we're ready to go
10 years ago

Junilu Lacar wrote:

Damien O Sullivan wrote:Should I carry on using jGrasp or should I download Eclipse? If so, what do I need to download exactly?


I'm not familiar with jGrasp and looked around but couldn't tell if it had any features to help in refactoring. If you're up for trying Eclipse, you can download and install the Eclipse IDE for Java Developers from http://www.eclipse.org/downloads/



I've eventually downloaded Eclipse and just after opening it, so I think I'm ready!
10 years ago

Junilu Lacar wrote:

Damien O Sullivan wrote:We haven't gone through creating different classes as of yet in the tutorials so and I don't know what helper methods are either.

I would be up for you guiding me through it if it's not too much trouble for you


Not a problem, I'd be happy to.

A couple of things before we start:

1. What IDE or text editor are you using? I think you know enough to use Eclipse now.

2. Because we're not sitting side-by-side, this won't take 15 minutes, so just be ready for this to drag on a bit. Also, there may be some delays in my responses as I am preparing for a minor medical procedure scheduled for tomorrow. Nothing serious, just need to get scoped out "where the sun don't shine" if you get what I mean... The prep is a b:censored:, though. Sorry if this is TMI.



I'm using jGrasp ATM, I haven't used anything else, as I've only been doing Java for less than two months.

It took me a couple of goes to get jGrasp working on my laptop 'cause I didn't know what to download exactly.

Should I carry on using jGrasp or should I download Eclipse? If so, what do I need to download exactly?
10 years ago

Junilu Lacar wrote:As an exercise, I tried refactoring your code and I got a pretty decent solution in about 15 minutes. I even kept a try-catch block in there but it only spanned 5 lines. As a bonus, the program exits when the user hits the "Cancel" button in the input dialog box. And it does everything else that you wanted it to do. Solution has 74 lines of code including a main method with 2 lines of code, a proper Quiz class, three constants, and four helper methods. The main while loop is only about 30 lines, including whitespaces and there's no duplicate code.

I'll extend my previous offer to guide you through the refactoring of your code, if you're up for it. I really think it would be worth your while.



We haven't gone through creating different classes as of yet in the tutorials so and I don't know what helper methods are either.

I would be up for you guiding me through it if it's not too much trouble for you
10 years ago

fred rosenberger wrote:

Damien O Sullivan wrote:The program does what I want it to do at the moment, are there any other problems you can notice with the code?


Post the current code (with code tags, please). It's kind of hard to comment on code we can't see.



10 years ago

Knute Snortum wrote:The braces at lines 20 and 39 are superfluous. The while loop at 17 has only one statement, the try block, and therefore doesn't need braces. However, this is confusing and they should be put in.



I've removed those braces on 20 and 39, the program now compiles and also runs.

The program does what I want it to do at the moment, are there any other problems you can notice with the code?
10 years ago

Greg Charles wrote:Your indentation is confusing, but it looks like what you are doing there is putting the try block inside the (outer) loop and the associated catch outside that loop. That's violating nesting rules, and I assume it won't compile. Is that what you mean by not working as you want it to? It's better to be specific about problems like that when you are asking questions. "I expected A, but got B," for example. It helps us focus our efforts. In any case, if I've diagnosed it right, you either need both try and catch together inside the block defined by the while loop, or you can put the while loop inside the try block. Although it is legal to use extra braces to define blocks of code that aren't loops, try-catch, if-else, etc., there's usually no reason to do that, and you run the risk of making your code harder to read.



The thing is that this code does compile and run though.
I want the code to keep repeating, without ending, and it does so.

But when I place the try { outside the while(answerIsCorrect) and the answer is correct, the program ends. I don't want it to end, I want it to keep on repeating, which it does with the original code.
10 years ago
At this moment in time my program compiles and runs as I want it to, but when I look at my code I see that my While Loops braces are placed inside my Try Block. When I move these braces outside of the Try Block, next to the WhileLoop, the program doesn't run like I want it to.

Likewise, when I place the inside the Try Block, it doesn't work like I want it to either.

What should I do to fix this problem? Thanks.

Also, my problem is on Lines 17 and 18.



10 years ago

Knute Snortum wrote:You're in a do/while loop, so put something in the catch block that will continue the loop.

Other observations about your code:

* Don't start variable names with a capital letter, doThis instead of DontDoThis.



* You don't need the outer parentheses in the line above.

* You have an extra set of braces inside your do/while loop

* Your formatting, especially your indentation, is inconsistent




What can I put into the Catch Block that will jump back into the loop? I've tried setting a new variable but it still just ends the program.
10 years ago

Joanne Neal wrote:

Damien O Sullivan wrote:Also, I need to validate it so that the program doesnt crash if they leave the input box blank or empty.


Your program is crashing because you don't catch the exception thrown by the Integer.parseInt method.
You can't add validation handling to the JOptionPane dialog, so the only way to validate the input (both empty boxes and invalid numbers) is to catch that exception or write your own input dialog that you can add validation to. The first option is the easiest.



Should I place the Try on Line 33?

Also, in the Catch, could I get the program to repeat the same question?

For example, if the question was 'What is 5 times 2?' and the user entered 'Ten'...when the program catches that exception, is it possible to repeat the same question?

Or would the program generate a new set of random numbers?

Thanks.
10 years ago
If you need the code, here it is:

10 years ago
Hi,

I need to validate an input box so that the user can enter only round numbers.

The user can't enter any other characters except 0 to 9, they shouldn't be able to enter decimal points either.

They would be answering questions like, 'What is 7 times 8?'

Also, I need to validate it so that the program doesnt crash if they leave the input box blank or empty.

Thanks in advance for any help you can give me.
10 years ago
Hi,

I'd like to change the text on the two buttons which I have in my Dialog Box, how should I do this?

Also, when I click the X Button in the top right of the Dialog Box, nothing happens, and the program only stops when I end it in jGrasp. How do I make this button close the program when clicked on?

Thanks in advance for any help you can give me.

10 years ago