Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link

Eric Kaiser

Greenhorn
+ Follow
since Jan 07, 2020
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
3
Received in last 30 days
0
Total given
3
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Eric Kaiser

Hi there!

I was just wondering that in programming there are a lot of concepts. So do you guys memorize things? Like two days ago I learned some sorting algorithms such as Selection sort, Insertion sort, Mergesort, and Quicksort. Thoroughly understood how they work and then implemented in Java. Now there's a report or saying like on average one forgets 80% of what they have learned the next day. Now today I tried to implement those algorithms again in Java. I did well but got confused a bit for quicksort so could not do it completely. I always believe memorizing codes or programs are bad. But sometimes I guess we do need to memorize the important parts of a specific program so we can implement them during exams or job interviews etc. Or am I just dumb?
2 years ago

Junilu Lacar wrote:

Eric Kaiser wrote:


And buy yourself some whitespace, too. There's no advantage in smushing everythingtogetherlikethis.Itjustmakesthecodethatmuchmoredifficulttoread.


Yeah I write neat code with proper indentations and spaces. You can check it here
I came here to post a question for a coding exercise I was doing. But then decided to try it more on my own. Then thought when I am already here let me check if anyone needs help with beginner level Java and came across this post. I typed that code in a hurry to get back to my work. So it is a bit messy.
2 years ago

Paul Clapham wrote:But just one thing: in Java there's a boolean type which can be true or false, so flag variables which have the values 0 and 1 aren't good form. Your flag variables should be boolean, not int.


Yeah I agree that's a good practice. Gonna do that from next time.
2 years ago


I think you have probably solved it from what Paul said. I just want to reiterate what I wanted to say in my previous reply. This achieves your task I think.
2 years ago
As far I understood from the explanation of your problem, you can simply use flag variables to solve this. Take three flag variables and set them as zero. Put if conditions before each block of code. When the compiler or control enters the particular function you should then set the flag variable as one. Next time when the iteration starts the if condition will check if the flag of that block of code is zero or one. If it is zero then it will enter the block else it won't.

Hope this solves your problem.
2 years ago

Campbell Ritchie wrote:Yes, I can see the problem now; thank you for explaining it. You are lucky that you are unlikely to repeat that mistake.



You are welcome . Yeah for sure. I thank HeadFirst Java for making these basic concepts so clear.
2 years ago

Campbell Ritchie wrote:Well done

But what was the problem?



You see in the main function I have taken a do-while loop. In that do-while loop I am creating an object of type LinkedList every time the loop gets executed. So the head node in LinkedList class is getting created for each object of LinkedList type, though the reference variable ll remains the same.

Now I can either take this line out of the loop or can make the head node static. That did the trick
2 years ago
Solved the issue. Pretty silly one.
2 years ago
Hi everyone!

I am learning Singly Linked List implementation in Java. So in the below code whenever I am inserting a node and trying to display the nodes, none of the nodes are getting printed. I checked that my head node is always remaining null. I can't understand why. Maybe I am making a silly mistake somewhere but really can't figure it out. Any help will be appreciated.

Thank you.

2 years ago
Thank you everyone! Noted down all the points in my head. Thanks again guys.
2 years ago
Thanks to Campbell Ritchie and Junilu Lacar! I understood why and what is happening in all the codes I posted.

That would be the wrong conclusion. Go through the tutorials and understand what each kind of exception (checked vs. unchecked) is for. It's quite appropriate to allow unchecked exceptions to blow up and interrupt program execution while you're still developing and testing your program. In this case, you want that to happen because it allows you to find developer mistakes and fix them. As for checked exceptions, it may be appropriate to allow them to bubble up to a level where the decision on how to handle them makes sense. It all depends on the context.



I don't know how to tag an user but this question is for Junilu Lacar or to anyone who can answer this. I get that unchecked exceptions are useful for the developer and also those exceptions usually happen when there is a mistake done by the developer in programming logic. But for checked exceptions why will you like to allow it? I mean we have to handle them in a catch block regardless or have to declare it. Unless I am debugging an application during development I don't think I will like to have checked exceptions arise as ultimately they won't allow to get the code compiled.

2 years ago

Campbell Ritchie wrote:That is an unchecked exception. The compiler will allow it to compile without needing a catch, and the finally will be executed. I suggest you go through this part of the Java™ Tutorials.



Ok so if I get this right, if an exception occurs I need to handle it with the catch() except if it is a unchecked exception that gets a free pass from the compiler. Ultimately I conclude that whatever the exception maybe it is best to handle the exception with the catch() block.
2 years ago

Junilu Lacar wrote:

Eric Kaiser wrote:"If you define a try block, you can pair it with a matching catch or finally block, or both."

So by this I understand you don't have to handle the exception if it arises or not, finally() block will always run.


It's not a question of whether or not the finally block will run. What you got there was a compile-time error because of the rule that checked exceptions must be either declared with throws or caught in a catch() block. Since you did neither, then the code will not compile. The presence or absence of a finally block is irrelevant.



I see. While reading on this try/final block topic I stumbled upon this piece of code online:



This code got executed without any issues but I did not expect this to. Why?

The output is given as:

Executing finally block
Exception in thread "main" java.lang.NullPointerException
at Main.print(Main.java:12)
at Main.main(Main.java:5)


Now this code differs from mine which did not execute in the case that here is no method which mentions an exception is being thrown of any kind for example:



But when I executed code with this change still the output remained same. Why?
2 years ago

Dave Tolls wrote:MyEx extends Exception, which means that it needs to be handled (as the compiler error says).
It can be handled either by catching it in a catch block, or by changing the method signature to show that it throws the exception.

Your second code works because you have told Java that the exception is being passed out of the method rather than being handled inside the method, by changing the signature to note the exception thrown.



But the statement said: "If you define a try block, you can pair it with a matching catch or finally block, or both."

So by this I understand you don't have to handle the exception if it arises or not, finally() block will always run.
2 years ago
Hi everyone! Newbie here.

I am learning Java from Head First Java. I have completed upto exceptions. There is a "Code Magnets" exercise on page 349. After solving it I decided to have some fun with the code.

There is a statement in the chapter: "If you define a try block, you can pair it with a matching catch or finally block, or both." So this statement says a try/final block without the catch block will also work.

So I took the code from "Code Magnets" and pasted it in an online Java editor to test the above statement. The code normally goes like this:



Here the output will be if input is yes: thaws and if input is suppose no then output is throws

Now my code where I decided to test the above statement.



But to my surprise the code did not execute. I got an error:

Main.java:7: error: unreported exception MyEx; must be caught or declared to be thrown
               doRisky(test);
                      ^


Now I have read that by declaring an exception I can duck it. So I modified the code as:



Now the output is given as:

thw
Exception in thread "main" MyEx
at Main.doRisky(Main.java:19)
at Main.main(Main.java:7)


So finally it works as usual.

Can any kind soul explain to me why the previous code of mine did not work but the second one worked? According to me both codes should work as the statement said.
2 years ago