● Study period: 260313
● Study scope: Honja 3-01,02 part operator part
Today, I worked through some practice exercises. It was a bit overwhelming at first, but seeing the code come together so cleanly was super satisfying! Here are the key takeaways from my session.
1. Direct Data Conversion in One Line
Instead of creating a separate String variable to store and then convert input, I learned that you can pass the input directly into the conversion method!
double num1 = Double.parseDouble(scanner.nextLine());
-
Why it's cool: It immediately feeds the
scanner.nextLine()output intoDouble.parseDouble(). This makes the code much more concise and efficient.
2. Handling 0.0 and Infinity (Division Safety)
I learned that 0 and 0.0 are essentially treated as the same value in this context. It's crucial to use an if statement to prevent dividing by zero, which is a big "no-no" in programming.
if(num2 != 0.0) {
System.out.println("Result: " + (num1/num2));
} else {
System.out.print("Result: Infinity");
}
- By checking if
num2is not 0.0, we can safely perform the division. If it is 0.0, it simply prints "Infinity." Usingiffor exception handling is so useful! (Though nestedifstatements still give me a headache... lol)
3. Combining Strings to Create Numeric Values
I found this specific piece of logic really clever:
double var4 = var1 * var2 * Double.parseDouble(var2 + "." + var3);
- It combines
var2, a dot., andvar3to create a string like"3.14", then converts that entire string into a double for the calculation. I wasn't sure if you could mix strings into a numeric calculation like that, but this conversion makes it possible!
4. Creating Login Logic with Nested If-Statements
This exercise required both the ID and Password to be correct to "log in." Seeing the solution finally made it click for me.
if(name.equals("java")) { // Step 1: Check ID
if(password == 12345) { // Step 2: Check Password
System.out.println("Login Successful");
} else {
System.out.println("Login Failed: Wrong Password");
}
} else {
System.out.println("Login Failed: ID does not exist");
}
-
Lessons Learned: * Always use
.equals()instead of==when comparing strings!- I struggled at first because I tried to use
=inside theifparentheses, but I realized the structure itself handles the logic without extra assignment symbols. - Nested structures (an
ifinside anif) are still a bit dizzying, but I'm getting used to the flow.
- I struggled at first because I tried to use
🏁 Final Thoughts
Through these exercises, I realized there's a huge difference between understanding the concept and actually writing the code. Making mistakes now is the best way to ensure I don't make them later in the field! Another productive day in the books.
Top comments (0)