Scanner is skipping nextLine() after using next() or nextFoo()?

That’s because the Scanner.nextInt method does not read the newline character in your input created by hitting “Enter,” and so the call to Scanner.nextLine returns after reading that newline. You will encounter the similar behaviour when you use Scanner.nextLine after Scanner.next() or any Scanner.nextFoo method (except nextLine itself). Workaround: Either put a Scanner.nextLine call after … Read more

How to really read text file from classpath in Java

With the directory on the classpath, from a class loaded by the same classloader, you should be able to use either of: If those aren’t working, that suggests something else is wrong. So for example, take this code: And this directory structure: And then (using the Unix path separator as I’m on a Linux box): … Read more

Why is Netbeans suggesting I “Flip operands of the binary operators” in my Java code

Netbeans frequently suggests that I “flip operands of the binary operator” when I’m doing mathematical calculations. For example, in the following line of code: Netbeans makes the suggestion for every mathematical symbol (so it does it three times in the ‘pennies’ line. I’m not sure I understand why it’s making the suggestion. If I were … Read more

Printing array elements with a for loop

This is a challenge question from my online textbook I can only get the numbers to prin forward… 🙁 Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, … Read more

When/why to call System.out.flush() in Java

System.out is based around a PrintStream which by default flushes whenever a newline is written. From the javadoc: autoFlush – A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of the println methods is invoked, or a newline character or byte (‘\n’) is written So the println … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)