Convert an integer to an array of digits

The immediate problem is due to you using <= temp.length() instead of < temp.length(). However, you can achieve this a lot more simply. Even if you use the string approach, you can use: You need to make the same change to use < newGuess.length() when printing out the content too – otherwise for an array of length 4 (which has … Read more

Java: Increment by 2 the two inputted integer

Some Notes: 1- in.nextInt(); reads an integer from the user, blocks until the user enters an integer into the console and presses ENTER. The result integer has to be saved in order to use it later on, and to do so save it into a variable, something like this: In your code, you need to assign the 3 … Read more

Finding the size of a char array in Java

You can use b.length to find out how many characters there are. This is only the number of characters, not indexing, so if you iterate over it with a for loop, remember to write it like this: Note the < (not a <=). It’s also important to note that since the array isn’t a class, .length isn’t a function, so you shouldn’t have … Read more

“Field can be converted to a local variable” message appearing when setting Android ActionBar colour

What the warning is telling you is that actionBarColor shouldn’t be a global variable (i.e. a field), because it’s only used in one method (onCreate). This is good advice: you should always minimize the scope of your variables, because it improves readability and reduces possibilities for programming errors. To get rid of the warning, fix the problem … Read more

break statement in “if else” – java

Because your else isn’t attached to anything. The if without braces only encompasses the single statement that immediately follows it. Not using braces is generally viewed as a bad practice because it can lead to the exact problems you encountered. In addition, using a switch here would make more sense. Note that instead of an infinite for loop I used a while(boolean), making it … Read more

How to pass an object from one activity to another on Android

One option could be letting your custom class implement the Serializable interface and then you can pass object instances in the intent extra using the putExtra(Serializable..) variant of the Intent#putExtra() method. Pseudocode: Note: Make sure each nested class of your main custom class has implemented Serializable interface to avoid any serialization exceptions. For example:

How do I test a class that has private methods, fields or inner classes?

Update: Some 10 years later perhaps the best way to test a private method, or any inaccessible member, is via @Jailbreak from the Manifold framework. This way your code remains type-safe and readable. No design compromises, no overexposing methods and fields for the sake of tests. If you have somewhat of a legacy Java application, and you’re not allowed to change … Read more

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