Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在输入文本字段之间放置空格_Java_Charat - Fatal编程技术网

Java 如何在输入文本字段之间放置空格

Java 如何在输入文本字段之间放置空格,java,charat,Java,Charat,我试图在文本字段中输入的数字之间放置空格。我正在使用以下代码: 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().

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

    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;

用于(int i=0;i直接用词不能。不能在
int
数据类型中添加空格,因为
int
仅用于存储整数值。将
int
更改为
String
以存储中间的空格。

直接用词不能。不能在
int
数据类型中添加空格,因为
int
仅用于存储整数值。将
int
更改为
String
以存储中间的空格。

用直截了当的话,您不能在
int
数据类型中添加空格,因为
int
仅用于存储整数值。将
int
更改为
String
以存储中间的空格

直截了当地说,您不能在
int
数据类型中添加空格,因为
int
仅用于存储整数值。请将
int
更改为
String
以存储中间的空格。

好的,让我们看看您的代码有什么问题

  • 您的for循环是基于1的,而不是基于标准0的。这一点都不好
  • 您尝试将char分配给字符串(3次),第一次调用charAt是正确的,但由于某种原因,您随后切换到使用字符串
  • 最后,您尝试将字符串指定给int,这完全是荒谬的

  • 好的,让我们看看你的代码有什么问题

  • 您的for循环是基于1的,而不是基于标准0的。这一点都不好
  • 您尝试将char分配给字符串(3次),第一次调用charAt是正确的,但由于某种原因,您随后切换到使用字符串
  • 最后,您尝试将字符串指定给int,这完全是荒谬的

  • 好的,让我们看看你的代码有什么问题

  • 您的for循环是基于1的,而不是基于标准0的。这一点都不好
  • 您尝试将char分配给字符串(3次),第一次调用charAt是正确的,但由于某种原因,您随后切换到使用字符串
  • 最后,您尝试将字符串指定给int,这完全是荒谬的

  • 好的,让我们看看你的代码有什么问题

  • 您的for循环是基于1的,而不是基于标准0的。这一点都不好
  • 您尝试将char分配给字符串(3次),第一次调用charAt是正确的,但由于某种原因,您随后切换到使用字符串
  • 最后,您尝试将字符串指定给int,这完全是荒谬的

  • 你有很多问题,但是诚实的尝试做得很好

    • 首先,字符串中的索引是从零开始的,因此
      charAt(0)
      为您提供第一个字符,
      charAt(1)
      为您提供第二个字符,依此类推
    • 其次,可能不需要重复所有对
      charAt
      的调用三次
    • 第三,您必须小心键入。来自
      charAt
      的返回值是
      char
      ,而不是
      String
      ,因此不能将其分配给
      String
      变量。同样,在最后一行,不要将
      String
      分配给
      int
      变量
    • 最后,我认为您没有考虑过如果文本字段没有包含足够的字符会发生什么

    记住这些要点,请再试一次,如果需要,请寻求进一步的帮助。

    您有很多问题,但诚实的尝试做得很好

    • 首先,字符串中的索引是从零开始的,因此
      charAt(0)
      为您提供第一个字符,
      charAt(1)
      为您提供第二个字符,依此类推
    • 其次,可能不需要重复所有对
      charAt
      的调用三次
    • 第三,您必须小心键入。来自
      charAt
      的返回值是
      char
      ,而不是
      String
      ,因此不能将其分配给
      String
      变量。同样,在最后一行,不要将
      String
      分配给
      int
      变量
    • 最后,我认为您没有考虑过如果文本字段没有包含足够的字符会发生什么

    记住这些要点,请再试一次,如果需要,请寻求进一步的帮助。

    您有很多问题,但诚实的尝试做得很好

    • 首先,字符串中的索引是从零开始的,因此
      charAt(0)
      为您提供第一个字符,
      charAt(1)
      为您提供第二个字符,依此类推
    • 其次,可能不需要重复所有对
      charAt
      的调用三次
    • 第三,您必须小心键入。来自
      charAt
      的返回值是
      char
      ,而不是
      String
      ,因此不能将其分配给
      String
      变量。同样,在最后一行,不要将
      String
      分配给
      int
      变量
    • 最后,我认为您没有考虑过如果文本字段没有包含足够的字符会发生什么

    记住这些要点,请再试一次,如果需要,请寻求进一步的帮助。

    您有很多问题,但诚实的尝试做得很好

    • 首先,字符串中的索引是从零开始的,因此
      charAt(0)
      为您提供第一个字符,
      charAt(1)
      为您提供第二个字符,依此类推
    • 其次,可能不需要重复所有对
      charAt
      的调用三次
    • 第三,您必须小心您的类型。
      charAt
      的返回值是
      char
      ,而不是
      String
      ,因此您不能将其分配给
      String
      变量。同样,在最后一行
      public class Test {
      
         public static void main(String args[]) {
            String s = "Strings are immutable";
            char result = s.charAt(8);
            System.out.println(result);
         }
      }
      
      a
      
      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.
      
      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