0

我试图在文本字段中输入的数字之间放置空格。我正在使用以下代码:

    for(int i = 0; i <= 2; i++)
    {
        char cijfer = tf1.getText().charAt(i);
        char getal1 = tf1.getText().charAt(0);
        char getal2 = tf1.getText().charAt(1);
        char getal3 = tf1.getText().charAt(2);
    }

    String uitvoerGetal = getal1 + " " + getal2 + " " + getal3;

我想我还不明白这个charAt()功能,有没有人有一个链接以某种方式解释它,所以我也可以做这个工作?提前致谢!

4

5 回答 5

1

例子:

public class Test {

   public static void main(String args[]) {
      String s = "Strings are immutable";
      char result = s.charAt(8);
      System.out.println(result);
   }
}

这会产生以下结果:

a

更多细节来自 java 文档

public char charAt(int index)

Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

If the char value specified by the index is a surrogate, the surrogate value is returned.

Specified by:
    charAt in interface CharSequence
Parameters:
    index - the index of the char value.
Returns:
    the char value at the specified index of this string. The first char value is at index 0.
Throws:
    IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.
于 2013-10-29T09:05:25.620 回答
1

In straight words You can't. You can't add space in int datatype because int is meant to store the integer value only. Change int to String to store the space in between.

于 2013-10-29T09:03:16.773 回答
1

Okay, let's see what's wrong with your code...

  1. Your for-loop is 1-based instead of the standard 0-based. That's not good at all.
  2. You're attempting to assign a char to a String (3 times), the first call to charAt is correct, but for some reason you then switch to using a String?
  3. Finally you're attempting to assign a String to an int, which is just completely nonsensical.
于 2013-10-29T09:03:22.893 回答
0

You have a number of problems, but well done on an honest attempt.

  • First up, the indexes in a string are zero-based, so charAt(0) gives you the first character, charAt(1) gives you the second character, and so on.
  • Secondly, repeating all your calls to charAt three times is probably unnecessary.
  • Thirdly, you must be careful with your types. The return value from charAt is a char, not a String, so you can't assign it to a String variable. Likewise, on the last line, don't assign a String to an int variable.
  • Lastly, I don't think you've thought about what happens if the text field doesn't contain enough characters.

Bearing these points in mind, please try again, and ask for further help if you need it.

于 2013-10-29T09:04:26.777 回答
0

试试下面的代码

String text = tf1.getText(); // get string from jtextfield
StringBuilder finalString = new StringBuilder();
for(int index = 0; index <text.length(); index++){
    finalString.append(text.charAt(index) + " ");  // add spaces      
}
tf1.setText(finalString.toString().trim()) // set string to jtextfield
于 2013-10-29T09:12:15.193 回答