• Post Reply Bookmark Topic Watch Topic
  • New Topic

Curly Brackets Placed Incorrectly  RSS feed

 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's your code as auto-formatted by Eclipse. This will give you an idea of how unconventional your indentation style is. Following the convention goes a long way in helping others read your code.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fundamental problem with this code is that it's too convoluted/complicated. You could streamline the logic and eliminate a few levels of nesting and duplicated sections of functionality if you followed the advice I gave in another thread where you asked about this same program. But since you have decided to go with this complicated structure, it's kind of difficult to give you any advice on how to make it better holistically. I think the best people will have to offer are localized band-aids and there are only so many band-aids you can put on code before it starts looking like Frankenstein. Sorry if that doesn't come off as very nice but I have to tell you that in all honesty, I think you've put yourself in a tough spot.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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, though. Sorry if this is TMI.
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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, 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?
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

If you don't see the STOP button on the menu bar, look for the Console tab as shown in the screencast. It should also have the STOP button there and you can click it to stop your program.

Once you get this done, we should be ready to start refactoring.
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's not exactly the kind of duplication we're looking for. Try-catch blocks are control structures. We're looking for actual code. Statements. What commands have you repeated in your code? There's a couple that you have in there three times.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'll just point it out to you. These two lines are duplicated three times in your code:

Sure, there are variations but they are essentially the same. We'll want to refactor one of these with Extract Method first, then we'll deal with eliminating the other instances of it afterwards.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also duplication in these lines:

The literal 10 is what is known as a "Magic Number". You want to extract that to a constant (Refactor | Extract Constant...) and give it a meaningful name. The calls to rand.nextInt() are next in line. You want to extract that to a method.

Once you're done with some of these refactorings, post your code so we can see the progress and make sure we're on the same page.

I'm going to be out most of the day tomorrow so I may not be able to respond until later in the evening or on Friday (US Eastern time).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!