Java - Convert lower to upper case without using t

2020-03-24 02:46发布

I'm trying to create a short program that would convert all letters that are uppercase to lowercase (from the command line input).

The following compiles but does not give me the result I am expecting. What would be the reason for this??

Eg) java toLowerCase BANaNa -> to give an output of banana

 public class toLowerCase{
        public static void main(String[] args){

            toLowerCase(args[0]);
        }

        public static void toLowerCase(String a){

            for (int i = 0; i< a.length(); i++){

                char aChar = a.charAt(i);
                if (65 <= aChar && aChar<=90){
                    aChar = (char)( (aChar + 32) ); 
                }

                System.out.print(a);
            }
         }   
    }

10条回答
男人必须洒脱
2楼-- · 2020-03-24 03:06
import java.util.Scanner;
public class LowerToUpperC {

    public static void main(String[] args) {

         char ch;
            int temp;
            Scanner scan = new Scanner(System.in);

            System.out.print("Enter a Character in Lowercase : ");
            ch = scan.next().charAt(0);

            temp = (int) ch;
            temp = temp - 32;
            ch = (char) temp;

            System.out.print("Equivalent Character in Uppercase = " +ch);

    }

}
查看更多
一纸荒年 Trace。
3楼-- · 2020-03-24 03:07
public class Changecase
{
    static int i;

    static void changecase(String s)
    {
        for(i=0;i<s.length();i++)
        {
            int ch=s.charAt(i);
            if(ch>64&&ch<91)
            {
                ch=ch+32;
                System.out.print( (char) ch);
            }
            else if(ch>96&&ch<123)
            {
                ch=ch-32;
                System.out.print( (char) ch);
            }
            if(ch==32)
            System.out.print(" ");
        }
    }

    public static void main (String args[])
    {

        System.out.println("Original String is : ");
        System.out.println("Alive is awesome ");
        Changecase.changecase("Alive is awesome ");

    }
}
查看更多
Summer. ? 凉城
4楼-- · 2020-03-24 03:09
/**
     * Method will convert the Lowercase to uppercase
     * if input is null, null will be returned
     * @param input
     * @return
     */
    public static String toUpperCase(String input){
            if(input == null){
                return input;
            }
            StringBuilder builder = new StringBuilder();
            for(int i=0;i<input.length();i++){
                char stringChar = input.charAt(i);

                if(92 <= stringChar && stringChar <=122){
                    stringChar = (char)( (stringChar - 32) ); 
                    builder.append(stringChar);
                }
                else if (65 <= stringChar && stringChar<=90)
                {
                    builder.append(stringChar);
                }
            }
            if(builder.length() ==0){
                builder.append(input);
            }
            return builder.toString();
        }
查看更多
beautiful°
5楼-- · 2020-03-24 03:12

You are printing the String a, without modifying it. You can print char directly in the loop as follows:

public class toLowerCase
{
    public static void main(String[] args)
    {
        toLowerCase(args[0]);
    }

    public static void toLowerCase(String a)
    {
        for (int i = 0; i< a.length(); i++)
        {
            char aChar = a.charAt(i);
            if (65 <= aChar && aChar<=90)
            {
                aChar = (char)( (aChar + 32) ); 
            }
            System.out.print(aChar);
         }
     }
}    
查看更多
地球回转人心会变
6楼-- · 2020-03-24 03:13

A cleaner way of writing this code is

public static void printLowerCase(String a){
    for(char ch: a.toCharArray()) {
       if(ch >= 'A' && ch <= 'Z')
          ch += 'a' - 'A';
       System.out.print(ch);
    }
}

Note: this will not work for upper case characters in any other range. (There are 1,000s of them)

查看更多
等我变得足够好
7楼-- · 2020-03-24 03:13

Looks like you're close. :)

For starters...

char aChar = a.charAt(i);

"a" is an array of Strings, so I believe you would want to iterate over each element

char aChar = a[i].charAt(0);

and it also seems like you want to return the value of the modified variable, not of "a" which was the originally passed in variable.

System.out.print(aChar);

not

System.out.print(a);

Hope that helps you.

查看更多
登录 后发表回答