Java string concatenation with .charAt(), "Incompatible Types"

I have ~this~ code (not exactly, it’s edited for clarity*):

String method (String input){
Random rand = new Random();
String upper = new String();
String output = new String();
int index = 0;

input = input.toLowerCase();
upper = input.toUpperCase();
index = rand.nextInt(input.length());
output = input.charAt(0-(index-1)) + upper.charAt(index) + input.charAt((index + 1)-(input.length() - 1));
    return (output);

}

It is supposed to capitalize a random letter in the string. When I compile it, I get this**:

class.java:17: error: incompatible types
output = input.charAt(0-(index-1)) + upper.charAt(index) + input.charAt((index + 1)-(input.length() - 1));
^
required: String
found: int
1 error

Anyone know what it’s referring to? I think it might have to do with the use of methods on methods***, as in I might have made an error somewhere with parenthases or order, but I can’t see an issue. Also, only the second “+” is an issue, not the first and the third, and the issue is with the “+”, not with the actual methods or variables according to the caret (“^”)… -.-

I’d appreciate any answers!
(Oh, and are there “code” tags here? And can/should I use spoiler tags?)

*As in, the method name, class name, and variable names. I don’t actually hava a method called method in a class called class, with variables called input and output, they’re named things that make sense in context of the program in the real program
**I use notepad + CMD, and this is the CMD output.
*** like "input.charAt(input.length() - 1).toUpperCase();

It looks to me like you are intending that charAt(…) will give a substring from the first letter up to the one before the random index, and then the substring from the letter after the random index to the end. But charAt only returns a single character, and you are ending up with code that evaluates to charAt(-7) or similar. You should be getting an IndexOutOfBoundsException, but for some reason Java is producing a different error message. Try the substring methods instead of charAt.

That’s funny, I was thinking it was weird to use “-” to set the range, .I’ll use substring tommorrow and see if I get it to work. speaking of whihc, if the random number is the first or last character in the string, would the current range settings work? Because it goes from 0 to the location of the uppercase letter minus one, so that would lead to “0 to -1”, and then the letter, and the same for the end. Also if the uppercase letter would be second or second last, you would get a range like “0-0”. I had separate if conditions for these, but it’s very messy with four different ones (if index == 0, if index == 1, if index == input.lenth() -1, if index == input.length() - 2). I think it would be a problem, but is there a better solution?

String concatenation only works with other String input - that’s why it’s unhappy.
You won’t get an IndexOutOfBoundsException from the compiler, as that’s a runtime error. The compiler won’t catch those.

I’d do it this way (didn’t try to run it, so forgive any typos, etc)

// assuming input is a String (as per your example)
// convert the string to a character array (and lowercase in the process)
char[] myCharArray = input.toLowerCase();

// generate your random number and get an index position
Random myRand = new Random();
int myRandPos = myRand.nextInt(input.length()); // will return 0 to (input.length()-1)

// use the Character class to convert a single character (static method)
myCharArray[myRandPos] = Character.toUpperCase(myCharArray[myRandPos]);

// turn the array back into a string
String myOutput = new String(myCharArray);

Of course. That’s a lot better than my messy code XD
I’m pretty sure I decided it would be “simpler” to keep it as a string instead of converting a char, but evidently it’s much better your way :stuck_out_tongue:
Thanks a lot to both of you ^W^