1

So, I'm trying to make a program that reads args and transforms all letters in capital letters without the use of toUpperCase(). The only way I have to use to process the single letters is with " - 'a' + 'A' "

This is what I did so far

import java.util.Scanner;

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

for(int i = 0; i<args.length; i++){
    for(int y=0; y<args[i].length(); y++){
    if ('a' <= args[i].charAt(y) && args[i].charAt(y) <= 'z') {
        args[i].charAt(y) = (char)(args[i].charAt(y) - 'a' + 'A');}  
    }
}

}

}

I don't understand what I'm doing wrong, the error says:

ArgsTest.java:9: error: unexpected type
        args[i].charAt(y) = (char)(args[i].charAt(y) - 'a' + 'A');
                      ^
  required: variable
  found:    value
  1 error
axaro1
  • 29
  • 1
  • 1
  • 3

2 Answers2

2

args[i].charAt(y) returns a character not a variable, so you can not assign a value to it

What you should do is creating a new String and add the uppercase charater to this and print it out:

    for(int i = 0; i<args.length; i++){
        String result = "";
        for(int y=0; y<args[i].length(); y++){
        if ('a' <= args[i].charAt(y) && args[i].charAt(y) <= 'z') {
            result  += (char)(args[i].charAt(y) - 'a' + 'A');}  
        }
        System.out.println(result);
    }
Jens
  • 67,715
  • 15
  • 98
  • 113
0

The charAt() method returns character not a variable. So you can't assign a value to a character. In java We can only assign value to a variable not to a value.

for(int i = 0; i<args.length; i++){
    String s = "";
    for(int y=0; y<args[i].length(); y++){
        if ('a' <= args[i].charAt(y) && args[i].charAt(y) <= 'z') {
            s+=(char)(args[i].charAt(y) - 'a' + 'A');
        }  
    }
    args[i]=s;
}
Deepak Kumar
  • 1,246
  • 14
  • 38