-1

Given a string personName, I'm trying to create a boolean condition2 equal to the condition

the first or last letter in personName is 'A' (case-insensitive). e.g., 'aha' or 'A'

Here's what I've tried so far:

boolean condition2;
if (personName.charAt(0) = "a" || personName.charAt(personName.length()-1) = "a") {
    condition2 = true;
} else {
    condition2 = false;
}
royhowie
  • 11,075
  • 14
  • 50
  • 67
Ismail Salah
  • 5
  • 1
  • 4

6 Answers6

0

use single quote for data type char. and convert it to lower case, so that it allow whether it's upper case or lower case.

Also use == when comparing if it's equal. this = sign, is for assigning value

if(Character.toLowerCase(personName.charAt(0)) == 'a' || 
   Character.toLowerCase(personName.charAt(personName.length()-1)) == 'a')

it will look like this

boolean condition2;
if(Character.toLowerCase(personName.charAt(0)) == 'a' || Character.toLowerCase(personName.charAt(personName.length()-1)) == 'a')
{
    condition2 = true;
}
else{
    condition2 = false;
}
Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
0

You can do it this way except comparison operator is == and you compare characters, not String. So right way would be:

Character.toLowerCase(personName.charAt(0)) == 'a'

See, double equals and single quote.

Alex
  • 4,457
  • 2
  • 20
  • 59
0

char type in Java is a character so wouldn't you be looking for something like this?

    boolean condition2;
    if((personName.charAt(0) == 'a' || personName.charAt(0) == 'A') && 
(personName.charAt(personName.length()-1) == 'a' || personName.charAt(personName.length()-1) == 'A'))
    {
        condition2 = true;
    }
    else{
        condition2 = false;
    }
0

For your first question where first and last character of a String should be 'a'

boolean condition1 = false; 
if(personName.charAt(0) == 'a' && personName.charAt(personName.length()-1) == 'a') {
    condition1 = true;
}

For your second condition where variable should be true only if age is in the range [18,24]

boolean condition2 = false;
if(personAge >=18 && personAge <=24) {
    condition2 = true;
}
Prudhvi
  • 2,276
  • 7
  • 34
  • 54
0

It is circuitous to write

boolean b;
if (some boolean expression)
   b = true;
else
   b = false;

Much simpler to write

boolean b = some boolean expression;

For reasons I don't understand, there is a widespread reluctance to write a boolean expression (as distinct from the simple literal values true/false) outside an 'if' statement.

And don't get me started on if (b == true)

royhowie
  • 11,075
  • 14
  • 50
  • 67
dave
  • 1
0

Your problem can be solve in this manner;

public static void main(String[] args) {   
    String s = "bsdadasd";
    boolean condition2;

        System.out.println(check(s.toLowerCase().charAt(0),s.toLowerCase().charAt(s.length()-1)));
    }
    public static boolean check(char a,char b){
        return (a == 'a' || b == 'a');
    }

You can pass the two characters as parameters for a method where it return true or falsedepending on the condition.

Since both characters are irrelevant of its case first made them to lowercase. toLowerCase() then passed the char at 0 and char at last.

The return statement will return the true or false to you.

And also use == to check if similar = means assigning.

Anjula Ranasinghe
  • 584
  • 1
  • 9
  • 24