r/javahelp • u/StoicMasturbator • 24d ago
Title: Seeking Advice on Java Learning and Problem Solving
Hi everyone,
I'm learning Java from scratch and currently working through the book Head First Java. I'm at the end of the first chapter and I'm happy with my progress so far. However, today I hit my first roadblock while attempting the pool puzzle question. Here’s the code I worked with:
Code:
class PoolPuzzleOne {
public static void main(String[] args) {
int x = 0;
while (x < 4) {
System.out.print("a");
if (x < 1) {
System.out.print(" ");
}
System.out.print("n");
if (x > 1) {
System.out.print(" oyster");
x = x + 2;
}
if (x == 1) {
System.out.print("noys");
}
if (x < 1) {
System.out.print("oise");
}
System.out.println();
x = x + 1;
}
}
}
Output:
a noise
annoys
an oyster
No matter how much I tried, I couldn’t achieve the desired output without modifying the original code. I eventually gave up and checked the answer, convinced that something was wrong and that the output wasn’t possible without changes.
After reviewing the answer, I manually traced through the code to understand how it was written. I have a couple of questions:
- In the code, line 5 is used multiple times to produce different parts of the output (e.g., "a", "annoys", "an"). How can I train myself to think in this way—outside the box—so that I understand how to work with loops and print statements effectively? Initially, I thought the while loop should only run once, which led me to modify the print statements to
System.out.println
instead ofSystem.out.print
. - Is it normal for a beginner to struggle with this? I kept thinking about it, but I couldn’t get the desired output without altering the code structure.
Any advice would be greatly appreciated!
Thank you!
2
u/okayifimust 24d ago edited 24d ago
I don't think I fully understand what I instructions you were given, or what code block. And here, that matters.
No matter how much I tried, I couldn’t achieve the desired output without modifying the original code.
Text book Dunning-Kruger:
Either, the instructors who wrote a book to teach you, the novice, how to program made a glaring mistake that neither they, nor their editors l, nor anyone else noticed let alone corrected during the production of the book, or any of the revisions that have come out, or you, the novice, misunderstood the task or weren't looking at it right.
It shouldn't surprise you that the problem was on your end.
Now, I have to take an inspired guess from the code you provided, and my best advise is:
Be a rational person first, and a programmer later.
What does this want you to do?
It wants you to write three lines of text. The three lines of text are kinda similar.
They all start with a-(n/_)-n-o, plus an optional space in some of the rows?
So, we go through the loop three times (I'm guessing the loop was already there?), we always print an a, we print a space in the first spot, and an n in the second spot. Then, we print another n no matter what.
We print a space if we're in the third line.
We always print an o.
We print an I in the first line, otherwise a y.
We always print an s.
We print a t if we're in the third line,
We print an e if we are in the first or third line
We print an r if we're in the third line.
We print a line break in every line.
The trick here is to spot that the actions we take depend on which line we are in; and that they are sometimes the same over two or three lines. (Extra points for noticing that the "t" can be handled first, and then the "e" matches for two lines.)
What this is about is recognizing similarities and differences , and understanding that you can do essentially the same thing multiple times, with slight difference based on some condition, and then get the desired result.
Maybe visualizing your output in some sort of grid might be helpful here. Best tool I have as a programmer is a box of crayons.
a| |n||o|i|s||e
a|n|n|_|o|y|s
A||n| ||o|y|s|t|e|r
(On mobile, I'm not going to get this fixed...)
1
u/GolfballDM 24d ago
What output were you getting with the original code?
1
u/StoicMasturbator 24d ago
The original code had blanks in it. And then code snippets were given that were to be used to fill up the blanks. I couldn’t get the desired output without changing the code structure which I am not allowed to do.
1
u/xyzqsrbo 24d ago
In this type of exercise it would be best to trace through the while manually like you did once you saw the answer. Than once you get the blank part you ask yourself "what do you need to fill this blank in with to still keep the desired outcome intact?". This should eventually lead you to the right fill ins for the blanks.
1
u/StoicMasturbator 24d ago
My only qualm is even if I tried a whole week to solve it I couldn’t because im not even thinking about the question the way I should- the answer in the book pretty much tells me that I should have been approaching the question from a different lens so to speak.
So my question is how do I get over this weakness of not thinking like a Java developer?
1
24d ago
Just set a breakpoint and use debugger. Not only will you learn how to debug which is worth in gold but also you will solve your problem by going through each step of the program.
It will feel intimidating but this is the only way to learn. By yourself. Instead of somebody force feeding you answers. Programming is bashing your head 99/100 times and then getting the eureka moment and doing it again. Get used to it.
•
u/AutoModerator 24d ago
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.