Java - convert lowercase to uppercase without using toUppercase ()

I am trying to create a short program that will convert all upper case letters to lower case (from command line input).

The following compiler, but does not give me the result that I expect. What is the reason for this?

For example) java toLowerCase BANaNa -> for banana output

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); } } 

}

+4
source share
10 answers

It seems like homework is just a hint to me. You print the string a , while you change the char type of aChar without changing the original string a . (Remember that strings are immutable).

+1
source

You print String a without changing it. You can print char directly in a loop like this:

 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); } } } 
+8
source

Looks like you're around. :)

To start...

 char aChar = a.charAt(i); 

"a" is an array of strings, so I believe that you will need to iterate over each element

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

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

 System.out.print(aChar); 

not

 System.out.print(a); 

Hope this helps.

+1
source

A cleaner way to write 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 uppercase characters in any other range. (There are 1000 of them)

+1
source
 /** * 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(); } 
+1
source
 public static void toLowerCase(String a){ String newStr = ""; for (int i = 0; i< a.length(); i++){ char aChar = a.charAt(i); if (65 <= aChar && aChar<=90){ aChar = (char)( (aChar + 32) ); } newStr = newStr + aChar; } System.out.println(newStr); } 

You should print newStr outside of the loop. You tried to print it inside a loop

0
source
 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 "); } } 
0
source
 public class MyClass { private String txt; private char lower; public MyClass(String txt) { this.txt = txt; } public void print() { for(int i=0;i<txt.length();i++) { if('A' <= txt.charAt(i) && txt.charAt(i) <= 'Z') { lower = (char)(txt.charAt(i) + 32); System.out.print(lower); } else { lower = txt.charAt(i); System.out.print(lower); } } } public static void main(String[] args) { MyClass mc = new MyClass("BaNaNa"); mc.print(); } } 

Sorry pretty late, but this should solve. The else condition, because when it is not equal to zero, it completely discards the alphabet.

0
source

If someone needs clean code without MagicNumbers and as few conversions as possible, this is my solution:

 final char[] charArray = new char[string.length()]; for (int i = 0; i < string.length(); i++) { char c = string.charAt(i); charArray[i] = Character.isLowerCase(c) ? Character.toUpperCase(c) : Character.toLowerCase(c); } String.valueOf(charArray); 
0
source
 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); } } 
-3
source

All Articles