AyaAbdelmoghith's blog

By AyaAbdelmoghith, history, 5 years ago, In English

Here is the problem question ~~~~~ import java.util.Scanner;

public class Word {

public static void main(String[] args) {

    Scanner input = new Scanner (System.in);
    String str=input.next();
    int lower=0;
    int upper=0;

    //System.out.println(str);
    for (int i=0 ;i<str.length();i++) {
       int num=str.charAt(i) ;
       System.out.println(num);
       if (num>= 97 || num<= 122 )  {
         lower++;
         System.out.println(lower+"L");}
       else //if (num>= 65 || num<= 90 )
         {
         upper++;
         System.out.println(upper+"U");
       }}
    System.out.println(lower +" "+ upper);
    if (lower>= upper)
       str=str.toLowerCase();

    else //if (lower<upper)
       str=str.toUpperCase();
    System.out.println(str);
    input.close();
}

}

~~~~~ I have no idea why my output comes out as lowercase only! beside please, if you see that I need a bit of advice to improve myself at PS don't hesitate to tell me so because while I am reading the question I know 100% that the answer is so easy and I am struggling to find a solution to the problem. I am solving currently at level A.

Thanks All.

  • Vote: I like it
  • +3
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it +3 Vote: I do not like it

It's perfectly okay to get stuck at this level :) Every journey began with a step!

Your mistake is here: if (num>= 97 || num<= 122 )

You should be testing if num >= 97 AND num <= 122 (try to understand why by yourself), that will give true if it's a lowercase letter.

Otherwise, it will always return true(using ||) since all letters are <= 122.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

if (num>= 97 || num<= 122 )

here use and operation ...

if (num>= 97 && num<= 122 )

»
5 years ago, # |
  Vote: I like it +6 Vote: I do not like it

Hi I am beginner also but I would like to share another method to Solve Question: Using logic and Java Operations (.toUpperCase(), toLowerCase()), we can simply apply the operation to the letter, then compare with the original string. If they are equal, then you know if it is uppercase or lowercase. Example: String a="A"; if(a.toUpperCase().equals(a)) { // DO } else { // Lowercase} So that's a fast way to test if a substring is uppercase or lowercase.

Hope this helps;)

»
5 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Unrelated to the bug here, I have another tip. In order to reduce the chance of a bug, you can use chars directly instead of using ints.

For example, 'A' <= x && x <= 'Z' is much less error-prone than looking up the ASCII numbers for each letter.

In fact, I usually go one further and make constants to further avoid risk from a typo, but that's probably a little excessive:

static final char A = 'A', Z = 'Z';
// ...
if (A <= x && x <= Z)