DEV Community

Hanna
Hanna

Posted on

📝 07-1. Java Practice: Nested If-Statements & Data Conversion (Login Logic!)

● 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());
Enter fullscreen mode Exit fullscreen mode
  • Why it's cool: It immediately feeds the scanner.nextLine() output into Double.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");
}
Enter fullscreen mode Exit fullscreen mode
  • By checking if num2 is not 0.0, we can safely perform the division. If it is 0.0, it simply prints "Infinity." Using if for exception handling is so useful! (Though nested if statements 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);
Enter fullscreen mode Exit fullscreen mode
  • It combines var2, a dot ., and var3 to 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");
}
Enter fullscreen mode Exit fullscreen mode
  • Lessons Learned: * Always use .equals() instead of == when comparing strings!
    • I struggled at first because I tried to use = inside the if parentheses, but I realized the structure itself handles the logic without extra assignment symbols.
    • Nested structures (an if inside an if) are still a bit dizzying, but I'm getting used to the flow.

🏁 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)