BMI if statements using return - android-studio-2.3

This is my code on if statement but I am warned that it cant return because I use void type,please help
if (BMI < 18.5)
return " You are underweight";
if ((BMI >= 18.5) && (BMI < 24.5))
return " You are normal weight.";
else if (BMI > 25)
return " You are overweight.";
else (BMI>30)
return "You are Obese!";

If this is a part of a method, you should define it with return type String and not with void.
public String myMethod(...some args...){
your code...
and not
public void myMethod(...some args...){

Related

Android studio with java

I have trouble getting data here from database there isn't data,
Data is not displayed outside of the method.
Could you please help me?
List<Person> Refresh() {
Person p = new Person();
ParseQuery <ParseObject> query = ParseQuery.getQuery(NAME_DATABASE);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList , ParseException e) {
if (e == null) {
for (int i = 0; i < scoreList.size(); i++){
p.setId(scoreList.get(i).getInt(Key.ID));
p.setName(scoreList.get(i).getString(Key.NAME));
p.setAge(scoreList.get(i).getString(Key.AGE)); p.setDate_start(scoreList.get(i).getString(Key.DATE_START));
p.setMonth_number(scoreList.get(i).getString(Key.MONTH_NUMBER));
p.setPropriety(scoreList.get(i).getString(Key.PROPRIETY));
p.setPrice(scoreList.get(i).getString(Key.PRICE));
p.setGender(scoreList.get(i).getString(Key.GENDER));
persons.add(p); //there is find data
}
}
else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
return persons; //here there isn't data (size=0)
}
The answer is simple really. You are returning persons outside the findInBackground() method.
else{
Log.d("score", "Error: " + e.getMessage());
}
//Return here after the else statement
return persons;
}
Note : you might want to make persons a global variable or else android studio will tell you to declare it as a final variable.

Dart private variable returns null in other functions

I have declared a private variable in dart _finalScore which is later assigned a value or result _finalScore = prevRes + currResult. It works well where i set the value to it but after that in another function when i try to you the private variable in another function if(_finalScore > 25) return "Good Performace" in same class, it(_finalScore) return null.
CalculatorScore({this.prevScore, this.currScore});
final int prevScore;
final int currScore;
double _finalScore;
String calculateScore() {
double _finalScore= prevScore + currScore;
return _finalScore.toStringAsFixed(1);
}
String getScoreRemark() {
if (_finalScore >= 60) {
return "Good work";
} else if (_finalScore > 50) {
return "Work hard";
} else {
return "Poor performace";
}
}
}
calculateScore() works well with _finalScore receiving a value but _finalScore returns null in getScoreRemark()
Any help thanks.
That happens because you are not setting the value of the class variable _finalScore but instead creating a new local variable inside the calculateScore method:
String calculateScore() {
double _finalScore = ...
return ...
}
you should remove the type and assign the value to the variable
_finalScore = ...

Checking if the first letter of string is in uppercase in Dart

I want to check if the first letter of string is in uppercase in Dart language. How can I implement it? Thanks in advance.
The simplest way I can think of is to compare the first letter of the string with the uppercase equivalent of it. Something like:
bool isUpperCase(String string) {
if (string == null) {
return false;
}
if (string.isEmpty) {
return false;
}
if (string.trimLeft().isEmpty) {
return false;
}
String firstLetter = string.trimLeft().substring(0, 1);
if (double.tryParse(firstLetter) != null) {
return false;
}
return firstLetter.toUpperCase() == string.substring(0, 1);
}
Updated the answer to take in consideration digits.
Also #Saed Nabil is right, this solution will return true if the string starts with any character that is not a letter (except for digits).
You can use the validators library if you are not already using it.
Then use this method
isUppercase(String str) → bool
check if the string str is uppercase
Don't forget to import the dependency, see documentation, to the pubspec.yaml and to your code import 'package:validators/validators.dart';.
Example code:
if(isUppercase(value[0])){
... do some magic
}
You should check that the value is not empty and not null first for safety. Like this:
if(value != null && value.isNotEmpty && isUppercase(value[0])){
... do amazing things
}
check this code it will return the actual uppercase letter else will return null
void main(){
var myString = "1s you said";
var firstCapital = firstCapitalLetter(myString);
if( firstCapital != null){
print("First Capital Letter is ${firstCapital}");
}else{
print("Not found");
}
}
String firstCapitalLetter(String myString){
final allCapitals = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //string.substring(0, 1).toUpperCase() == string.substring(0, 1) will not work with for ex. numbers;
if (myString == null) {
return null;
}
if (myString.isEmpty) {
return null;
}
if (myString.trimLeft().isEmpty) {
return null;
}
if( allCapitals.contains(myString[0])){
return myString[0];
}else{
return null;
}
}
this is a typical case for Optional type of Java language , please check this library if you prefer functional style code
optional package
bool isUppercase(String str){
return str == str.toUpperCase();
}
This seems to be elegant way for checking uppercase
String s='Hello';
bool isUpper = isUppercase(s[0]);
print(isUpper); //true
isUpper = isUppercase(s[1]);
print(isUpper); //false
Add this extension
extension Case on String{
// isuppercase
bool isUpperCase(){
int ascii = codeUnitAt(0);
return ascii >= 65 && ascii <= 90;
}
// islowercase
bool isLowerCase(){
int ascii = codeUnitAt(0);
return ascii >= 97 && ascii <= 122;
}
}
use it like this
String letter = 'A';
print(letter.isUpperCase()); // true

How do I bind Character to a TextField?

I've found an example of how to bind Integer to a TextField:
Binder<Person> b = new Binder<>();
b.forField(ageField)
.withNullRepresentation("")
.withConverter(new StringToIntegerConverter("Must be valid integer !"))
.withValidator(integer -> integer > 0, "Age must be positive")
.bind(p -> p.getAge(), (p, i) -> p.setAge(i));
The problem is - there is no StringToCharacterConverter and if have an error if I bind fields as is. The error is:
Property type 'java.lang.Character' doesn't match the field type 'java.lang.String'. Binding should be configured manually using converter.
You need to implement custom converter, here is very simplified version of what could be StringToCharacterConverter for getting the pattern what the they look like:
public class StringToCharacterConverter implements Converter<String,Character> {
#Override
public Result<Character> convertToModel(String value, ValueContext context) {
if (value == null) {
return Result.ok(null);
}
value = value.trim();
if (value.isEmpty()) {
return Result.ok(null);
} else if (value.length() == 1) {
Character character = value.charAt(0);
return Result.ok(character);
} else {
return Result.error("Error message here");
}
}
#Override
public String convertToPresentation(Character value, ValueContext context) {
String string = value.toString();
return string;
}
}

Method not found JSF 2

I want to call a method from managedbean in jsf but I am getting the same error.Before I didn't get this error.Here is my method and calling in xhtml.
public String veriSil(Personel personel){
msb.baglan();
String sonuc="";
String sql = "DELETE FROM jsfapp.personel WHERE ad='"+personel.getAd()+"' AND soyad='"+personel.getSoyad()+"'";
try {
PreparedStatement pstmt = (PreparedStatement) msb.getConnection().prepareStatement(sql);
resultSilme = pstmt.execute();
} catch (Exception e) {
e.printStackTrace();
}
if (!resultSilme) {
sonuc += personelad + " " + personelsoyad + " silindi.";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(sonuc));
return null;
} else {
sonuc += "Silme işlemi yapılamadı!";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(sonuc));
return null;
}
}
I called in jsf ;
<h:commandLink action="#{kmb.veriSil}" value="Sil"/>
I can not see an error.What is going wrong?
It seems you are calling a method that expects an argument without specifiying the argument. Therefore the compiler cannot match your call to a method because the argument is part of the method's signature.

Resources