Home > Mobile >  Produce lowercase character instead of uppercase in Java
Produce lowercase character instead of uppercase in Java

Time:10-19

The output is fine but the output is in Uppercase letter, I want the output to be lowercase character, what should I do?

import java.util.Scanner;

public class Q2 {
      
        public static void main(String[] args) {
            int a[]=new int[26];
            Scanner sc =  new Scanner (System.in);
            String str=sc.nextLine();
            for(int i = 0; i<str.length();i  ) {
                if(str.charAt(i)>=65 && str.charAt(i)<=90) {
                    a[str.charAt(i)-65]  ;
                }
                else if(str.charAt(i)>=97 && str.charAt(i)<=122) {
                    a[str.charAt(i)-97]  ;
                }
            }
            for(int i=0;i<26;i  ) {
                if(a[i]>0) {
                    System.out.print(" " (char )(i 65)  "("   a[i] ")");
                }
                
            }
                    
        }       

}

CodePudding user response:

'A' is 65. 'a' is 97. Use 97 instead of 65 in your final loop.

Alternatively use 'a' directly instead of the number. This makes it more obvious what you're doing in that code.

CodePudding user response:

Check out this post with a accepted answer How do I convert strings between uppercase and lowercase in Java?

You could probably do something like this

import java.util.Scanner;

public class Q2 {
      
        public static void main(String[] args) {
            int a[]=new int[26];
            Scanner sc =  new Scanner (System.in);
            String str=sc.nextLine();
            String lowerCaseInput = str.toLowerCase();
            System.out.print(lowerCaseInput);
        }
}

  • Related