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

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

via Dev.to WebdevHanna

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 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" ); } By checking if num2 is not 0.0, we

Continue reading on Dev.to Webdev

Opens in a new tab

Read Full Article
2 views

Related Articles