Java Questions! About List and Objects

Something like this. double increase = 10000; for (Employee employee : theList) { double salaryNow = employee.getSalary(); employee.setSalary(salaryNow + increase); } Caution – you should never use a double to store an amount of money, as you will be subject to floating point rounding errors. Please use a BigDecimal instead. I’ve only done it in … Read more

get Index of last Element in list without using lastIndexOf() or any other Java/String- Methods [closed]

The below code works in getting the last index of a certain character: public int lastIndex(char a, char[] data) { int max=-1; for(int i=0; i<data.length; i++) { if(data[i]==a) { max=i; } } return max; } It works by going through the array and finding the latest occurrence of a certain character. The variable ‘max’ is … Read more

How to create a loop thats includes a if statement

If I understand your question correctly, every time the loop runs, you want to decrease “powerlevel” by a random value, then display a message depending on whether powerlevel can be reduced. Here are two approaches. 1) use an if statement in your for loop for (conditions){ if(powerlevel cannot be decremented){ //display a messsage break; } … Read more