简体   繁体   English

无法在Java中将int转换为字符串

[英]Cannot convert int to string in Java

I get an error: cannot convert int to string in Java 我收到一个错误: 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 脚本只需通过获取前2个字母,中间字母和后2个字母来提取字符串中的“隐藏”单词

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. char是一个int类型。 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: 另一方面,如果您将一个字符串添加到char或另一个字符串中,则它们会串联到另一个字符串中:

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

Edit 编辑

Your method currently only returns if input.length()>5 . 您的方法当前仅在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. 如果长度为5或更小,则您的方法不知道应返回什么,因此代码将无法编译。

If you know what your method should return in this case, add an extra return after your if block. 如果您知道在这种情况下您的方法应该返回什么,请在if块之后添加一个额外的return

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. 在您的情况下,您可以使用String.copyValueOf从您的字符创建一个字符串。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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