简体   繁体   中英

Cannot convert int to string in Java

I get an error: cannot convert int to string in Java

Script simply extracts a 'hidden' word inside a string by taking the first 2 letters, mid letter and last 2 letters

Here's my code:

package hello;
import java.util.*;
public class hello {
    public static String extract(String input) {
        if (input.length()>5) {
            int len = input.length();
            return input.charAt(0) + input.charAt(1) + input.charAt(len/2)+ input.charAt(len-2)+ input.charAt(len-1);
        }
    }
    public static void main(String[] args) {
        System.out.println("Enter a word:");
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String output = extract(input);
        System.out.println(output);     
    }
}
return input.charAt(0) + input.charAt(1) + input.charAt(len/2)+ input.charAt(len-2)+ input.charAt(len-1)

char is an int type. If you add together a bunch of characters, you get an int, not a string.

On the other hand, if you add a string to a char, or to another string, then they are concatenated into another string:

return input.substring(0,2) + input.charAt(len/2) + input.substring(len-2);

Edit

Your method currently only returns if input.length()>5 . If the length is 5 or less, your method doesn't know what it should return, so the code will not compile.

If you know what your method should return in this case, add an extra return after your if block.

public static String extract(String input) {
    int len = input.length();
    if (len >= 5) {
        return input.substring(0,2) + input.charAt(len/2) + input.substring(len-2);
    }
    return input; // must return something if the length was less than 5.
}

Alternatively, you might want to throw an exception if your string is too short.

When you use + between two chars it considered like you sum two integer for example :

'a' + 'b'

Is equivalent to :

97 + 98

In your case you can use String.copyValueOf to create a String from your chars.

return String.copyValueOf(new 
        char[] { 
                input.charAt(0), input.charAt(1), input.charAt(len / 2),
                input.charAt(len - 2), input.charAt(len - 1) 
        }
);

This will create a String from this chars.

Your method still need to return a default value if your condition is not correct.

public static String extract(String input) {
    if (input.length() > 5) {
        int len = input.length();
        return String.copyValueOf(new 
                char[] { 
                        input.charAt(0), input.charAt(1), input.charAt(len / 2),
                        input.charAt(len - 2), input.charAt(len - 1) 
                }
        );
    }
    return "";//if the condition is wrong return empty string or null
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM