Java 找到中间的字母并使其大写。。我被卡住了,最后(

Java 找到中间的字母并使其大写。。我被卡住了,最后(,java,Java,我在这里找到数组中每个元素的中间字母,使其大写并合并,这样它会给出一个类似于e:g-{bOy,inDia,apPle}的结果 以下是我目前正在尝试的代码 public class FindMiddle { public static void main(String[] args) { ArrayList<String> al = new ArrayList<String>(); al.add("boy india apple

我在这里找到数组中每个元素的中间字母,使其大写并合并,这样它会给出一个类似于e:g-{bOy,inDia,apPle}的结果

以下是我目前正在尝试的代码

public class FindMiddle {

    public static void main(String[] args) {

        ArrayList<String> al = new ArrayList<String>();

        al.add("boy india apple");

        String str[] = al.toArray(new String[al.size()]);
        String newar[];
        String delimiter = " ";

        newar = str[0].split(delimiter);

        for (int i = 0; i < newar.length; i++) {
            char[] c = newar[i].toCharArray();
            for (int j = 0; j < c.length; j++) {
                if (j == c.length / 2) {
                    c[j] = Character.toUpperCase(c[c.length / 2]);
                    System.out.println(c[j]);
                }
                newar[i] += c[j];
            }
        }
    }
}
它只给了我一个O-D-p


我希望它们与原始元素(如bOy inDia apPle)合并。

这是因为您只打印中间字符,println位于if语句中

 if (j == c.length / 2) {
        c[j] = Character.toUpperCase(c[c.length / 2]);
        System.out.println(c[j]);
 }
我建议使用

试试这个:

我的解释是代码中的注释

注意:当您在for循环中时,您正在检查是否输入了中间字符。如果遇到,您将使用大写,否则不会更改任何内容,并且每个字符都会在控制台上打印出来。

您可以使用String对象的charAt函数和replace函数 在访问字符串的中间索引时,使用full String.length是不准确的,因为索引的长度为-1,因为它以0开头,所以需要从长度中减去1,然后将其除以一半

for(int i=0;i<newar.length;i++){
            int index = (newar[i].length()-1)/2;
            newar[i] = newar[i].replace(newar[i].charAt(index), Character.toUpperCase(newar[i].charAt(index)));
            System.out.println(newar[i]);
        }
1-我去掉了这个,因为它只打印大写字母。你已经将所有3个单词的中间字符设置为大写,但是,你需要打印整个单词,而不仅仅是一个字母

2-但是,你确实想打印出整个单词,这是这一行正在做的。它不做println,只打印。原因是我们利用了遍历特定单词中每个字符的for循环,在检查完是否是中间字母后,能够打印出每个字母


3-我们在这里有这一行是因为我们希望能够在单词之间进行分隔。我不确定您希望这些单词如何分隔,所以在您觉得有必要时进行更改,我只是将它们分隔开来,以便您可以看到。

您的代码有很多错误,因此这里有一个简化的重写:

String temp = ""; //define temp string as "" (don't assign null)
for (int j = 0; j < c.length; j++) {
    ...
    //newar[i] += c[j];  //don't append to existing String in array
    temp += c[j];
    newar[i] = temp;
}

//after replacement is done
//now can print replaced string in array by looping
for (String string : newar) {
        System.out.println(string);
}
public static void main(String[] args) {
    String s = "boy india apple";

    String[] split = s.split( " " );
    String[] toReturn = new String[split.length];
    for (int i = 0; i < split.length;i++)
    {
        String word = split[i];
        char[] chars = word.toCharArray();
        chars[chars.length/2] = Character.toUpperCase( chars[chars.length/2] );
        toReturn[i] = String.valueOf( chars );
    }
    System.out.println(Arrays.toString( toReturn ));
}
为了更正代码,您可以从删除无用的ArrayList开始,并将System.out移出for循环之外。还有一些其他问题,例如您正在将新结果附加到原始结果,因此运行后的newar将类似于{boybOy,IndianDia,appleapPle}

编辑:

出于教学目的,这里对代码进行了修改,使其能够正常工作,但效率低下

public static void main(String[] args) {

    ArrayList<String> al = new ArrayList<String>();

    al.add("boy india apple");

    String str[] = al.toArray(new String[al.size()]);
    String newar[];
    String delimiter = " ";

    newar = str[0].split(delimiter);

    System.out.print("{");
    for (int i = 0; i < newar.length; i++) {
        char[] c = newar[i].toCharArray();
        for (int j = 0; j < c.length; j++) {
            if (j == c.length / 2) {
                c[j] = Character.toUpperCase(c[c.length / 2]);
            }
            System.out.print(c[j]);
        }
        newar[i] = String.valueOf(c);
        if (i < newar.length - 1)
            System.out.print(",");
    }
    System.out.println("}");
}

你想要ODP还是boyidaappleit应该像{bOy,india,apple}你有一个地方可以打印东西。打印出来的东西是你刚刚用大写的字符。你想让它打印每个字母,在它被大写或保持不变后。是的..JB..但我不知道如何让大写字母与原始单词合并。谢谢..我得到了…:好,现在我不确定tput是您想要的格式,您也需要帮助吗?或者输出格式好吗?不,不,我会处理..我的问题是我打印了一段错误的代码..试图将其附加到原始代码中,结果让我大吃一惊..在打印所需结果之前附加null..如nullinDia
bOy
inDia
aPPle
import java.util.ArrayList;


public class Test
{
   public static void main(String[] args) {

      ArrayList<String> al = new ArrayList<String>();

      al.add("boy india apple");

      String str[] = al.toArray(new String[al.size()]);
      String newar[];
      String delimiter = " ";

      newar = str[0].split(delimiter);

      for (int i = 0; i < newar.length; i++) {

         char[] c = newar[i].toCharArray();
         for (int j = 0; j < c.length; j++) {

            if (j == c.length / 2) {
               c[j] = Character.toUpperCase(c[c.length / 2]);
               // System.out.println(c[j]);
               // (1)    
            }

            System.out.print(c[j]);
            // (2)               

            newar[i] += c[j];

         }
         System.out.println();
         // (3)
      }
   }
}
String temp = ""; //define temp string as "" (don't assign null)
for (int j = 0; j < c.length; j++) {
    ...
    //newar[i] += c[j];  //don't append to existing String in array
    temp += c[j];
    newar[i] = temp;
}

//after replacement is done
//now can print replaced string in array by looping
for (String string : newar) {
        System.out.println(string);
}
public static void main(String[] args) {
    String s = "boy india apple";

    String[] split = s.split( " " );
    String[] toReturn = new String[split.length];
    for (int i = 0; i < split.length;i++)
    {
        String word = split[i];
        char[] chars = word.toCharArray();
        chars[chars.length/2] = Character.toUpperCase( chars[chars.length/2] );
        toReturn[i] = String.valueOf( chars );
    }
    System.out.println(Arrays.toString( toReturn ));
}
public static void main(String[] args) {

    ArrayList<String> al = new ArrayList<String>();

    al.add("boy india apple");

    String str[] = al.toArray(new String[al.size()]);
    String newar[];
    String delimiter = " ";

    newar = str[0].split(delimiter);

    System.out.print("{");
    for (int i = 0; i < newar.length; i++) {
        char[] c = newar[i].toCharArray();
        for (int j = 0; j < c.length; j++) {
            if (j == c.length / 2) {
                c[j] = Character.toUpperCase(c[c.length / 2]);
            }
            System.out.print(c[j]);
        }
        newar[i] = String.valueOf(c);
        if (i < newar.length - 1)
            System.out.print(",");
    }
    System.out.println("}");
}