如何在Java中删除特定字符(”,”)?

huangapple go评论63阅读模式
英文:

How to remove a specific character (",") in java?

问题

// 创建一个类和方法
class Main {
    public static void main(String[] args) {

        // 清屏
        System.out.print("\033[H\033[2J");
        System.out.flush();

        // 初始化一个具有12个限制的整数数组
        int[] numbers = new int[12];

        // 创建一个语句
        System.out.println("随机生成的数组:");

        System.out.print("{");

        // 开始一个循环以打印随机数字
        for (int x = 0; x < numbers.length; x++) {
            numbers[x] = (int) (Math.random() * 101); // 使其打印0到100(包含)范围内的随机数

            // 打印出12个随机数
            System.out.print(numbers[x]);
            if (x < numbers.length - 1) {
                System.out.print(", ");
            }
        }
        System.out.print("}");

        // 创建空行
        System.out.println();
        System.out.println();

        // 创建一个语句
        System.out.println("每个阶段的排序:");

        // 开始循环数字直到12
        for (int i = 0; i < numbers.length; i++) {

            // 开始循环数字并将其与“i”值进行比较
            for (int j = i; j < numbers.length; j++) {
                if (numbers[j] < numbers[i]) {

                    // 使用变量“temp”交换数字的位置
                    int temp = numbers[j];
                    numbers[j] = numbers[i];
                    numbers[i] = temp;
                }

                // 创建空行
                System.out.println();

                // 使用循环打印排序每个阶段的数字
                System.out.print("{");
                for (int k = 0; k < numbers.length; k++) {
                    System.out.print(numbers[k]);
                    if (k < numbers.length - 1) {
                        System.out.print(", ");
                    }
                }
                System.out.print("}");
                System.out.println();
            }
        }

        // 创建空行
        System.out.println();
        System.out.println("\n冒泡排序后的数组:");
        System.out.print("{");

        // 使用循环按升序打印最终排序的数字
        for (int a = 0; a < numbers.length; a++) {
            System.out.print(numbers[a]);
            if (a < numbers.length - 1) {
                System.out.print(", ");
            }
        }

        System.out.print("}");

        // 创建空行
        System.out.println();
        System.out.println();
    }
}
英文:

I basically want to remove the last comma "," from my array of 12 integers. My output goes like {23, 45, 54, 56, 67, 87, 89, 90, 76, 89, 87, 23, } whereas I want to remove the last comma and space it creates after the comma.

Sample output:

{23, 45, 54, 56, 67, 87, 89, 90, 76, 89, 87, 23}

My task is to basically sort the integers using bubble sorting method in ascending order. And my output basically shows the sorting at each stage and a final sorted array. I want to remove the last comma and the space it creates from the sorting at each stage, final sorted array, and random generated array. I basically provided comments in my code which will help you find each thing.

Please do not make this overly complicated

Code:

// Create a class and method
class Main {
public static void main(String[] args) {
// Clear screen
System.out.print(&quot;\033[H\033[2J&quot;);
System.out.flush();
// Initialize a int array with the limit of 12
int[] numbers = new int[12];
// Create a statement 
System.out.println(&quot;Random Generated Array:&quot;);
System.out.print(&quot;{&quot;);
// Start a for loop to print random numbers
for (int x = 0; x &lt; numbers.length; x++) {
numbers[x] = (int) (Math.random() * 101); // So that it prints random numbers from 0 to 100 (inclusive)
// Print out the 12 random numbers
System.out.print(numbers[x] + &quot;, &quot;);
}
System.out.print(&quot;}&quot;);
// Create space
System.out.println();
System.out.println();
// Create a statement 
System.out.println(&quot;Sorting At Each Stage:&quot;);
// Start looping numbers until 12
for (int i = 0; i &lt; numbers.length; i++) {
// Start looping numbers and comparing it to the &quot;i&quot; values
for (int j = i; j &lt; numbers.length; j++) {
if (numbers[j] &lt; numbers[i]) {
// Swap the positions of the numbers with the variable &quot;temp&quot;
int temp = numbers[j];
numbers[j] = numbers[i];
numbers[i] = temp;
}
// Create space
System.out.println();
// Use a for loop to print numbers at each stage of sorting
System.out.print(&quot;{&quot;);
for (int k = 0; k &lt; numbers.length; k++) {
System.out.print(numbers[k] + &quot;, &quot;);
}
System.out.print(&quot;}&quot;);
System.out.println();
}
}
// Create space
System.out.println();
System.out.println(&quot;\nBubble Sorted Array:&quot;);
System.out.print(&quot;{&quot;);
// Use a for loop to print the final sorted numbers in ascending order
for (int a = 0; a &lt; numbers.length; a++) {
System.out.print(numbers[a] + &quot;, &quot;);
}
System.out.print(&quot;}&quot;);
// Create space
System.out.println();
System.out.println();
}
}

答案1

得分: 2

你可以在你的for循环中使用一个if条件,以便在最后一个数字不添加逗号:

// 开始一个for循环来打印随机数
for (int x = 0; x < numbers.length; x++) {
  numbers[x] = (int) (Math.random() * 101); // 以便打印从0到100(含)的随机数
  if (x + 1 == numbers.length)
      // 由于是最后一个数字,所以不添加逗号,直接输出随机数
      System.out.print(numbers[x]);
  else
      // 打印这12个随机数
      System.out.print(numbers[x] + ", ");
}
英文:

You could just use an if condition in your for loop to not place a comma on the last number:

// Start a for loop to print random numbers
for (int x = 0; x &lt; numbers.length; x++) {
numbers[x] = (int) (Math.random() * 101); // So that it prints random numbers from 0 to 100 (inclusive)
if(x + 1 == numbers.length)
//Print out random number with no comma as it&#39;s the last
System.out.print(numbers[x]);
else
// Print out the 12 random numbers
System.out.print(numbers[x] + &quot;, &quot;);
}

答案2

得分: 2

如果您想查看一种将数组连接成字符串的替代方法您可以考虑使用Java 8流中提供的功能

这将对数组进行流处理使用String::valueOf将每个元素映射为字符串然后使用分隔符", "将它们收集在一起我只是在两端添加了大括号以使其与您的操作相匹配

```java
import java.util.Arrays;
import java.util.stream.Collectors;

public class so64395137 {
    public static void main(String[] args) {
        int[] numbers = { 23, 45, 54, 56, 67, 87, 89, 90, 76, 89, 87, 23 };
        String strNumbers = "{" + Arrays.stream(numbers)
                .mapToObj(String::valueOf).collect(Collectors.joining(", "))
                + "}";
        System.out.println(strNumbers);
    }
}

输出:

{23, 45, 54, 56, 67, 87, 89, 90, 76, 89, 87, 23}
英文:

If you'd like to see an alternate way to join an array into a string you could consider the functionality that came with Java 8 streams.

This streams the array, mapping each element to a string using String::valueOf and then collecting them together with a delimiter of ", ". I simply add a brace to either end to make it match what you were doing.

import java.util.Arrays;
import java.util.stream.Collectors;
public class so64395137 {
public static void main(String[] args) {
int[] numbers = { 23, 45, 54, 56, 67, 87, 89, 90, 76, 89, 87, 23 };
String strNumbers = &quot;{&quot; + Arrays.stream(numbers)
.mapToObj(String::valueOf).collect(Collectors.joining(&quot;, &quot;))
+ &quot;}&quot;;
System.out.println(strNumbers);
}
}

Prints:

{23, 45, 54, 56, 67, 87, 89, 90, 76, 89, 87, 23}

答案3

得分: 1

我在你的循环中添加了一个 if 语句,如果是最后一次迭代,就不要添加额外的位。

// 创建一个类和方法
class Main {
  public static void main(String[] args) {

    // 清屏
    System.out.print("\033[H\033[2J");
    System.out.flush();

    // 初始化一个具有12个限制的整数数组
    int[] numbers = new int[12];

    // 创建一个语句
    System.out.println("随机生成的数组:");

    System.out.print("{");

    // 开始一个for循环以打印随机数字
    for (int x = 0; x < numbers.length; x++) {
      numbers[x] = (int)(Math.random() * 101); // 以便打印从0到100(包含)的随机数字
      if (x == numbers.length - 1) {
        // 打印出这12个随机数字
        System.out.print(numbers[x]);
      } else {
        System.out.print(numbers[x] + ", ");
      }
    }
    System.out.print("}");

    // 创建空间
    System.out.println();
    System.out.println();

    // 创建一个语句
    System.out.println("每个阶段的排序:");

    // 开始循环数字,直到12
    for (int i = 0; i < numbers.length; i++) {

      // 开始循环数字并将其与“i”值进行比较
      for (int j = i; j < numbers.length; j++) {
        if (numbers[j] < numbers[i]) {

          // 用变量“temp”交换数字的位置
          int temp = numbers[j];
          numbers[j] = numbers[i];
          numbers[i] = temp;
        }

        // 创建空间
        System.out.println();

        // 使用for循环打印排序每个阶段的数字
        System.out.print("{");
        for (int k = 0; k < numbers.length; k++) {
          System.out.print(numbers[k] + ", ");
        }
        System.out.print("}");
        System.out.println();
      }
    }

    // 创建空间
    System.out.println();
    System.out.println("\n冒泡排序后的数组:");
    System.out.print("{");

    // 使用for循环以升序打印最终排序的数字
    for (int a = 0; a < numbers.length; a++) {
      System.out.print(numbers[a] + ", ");
    }

    System.out.print("}");

    // 创建空间
    System.out.println();
    System.out.println();
  }

}
英文:

I've added an if in your loop whereby if it is the last iteration, don't add the extra bits.

    // Create a class and method
class Main {
public static void main(String[] args) {
// Clear screen
System.out.print(&quot;\033[H\033[2J&quot;);
System.out.flush();
// Initialize a int array with the limit of 12
int[] numbers = new int[12];
// Create a statement 
System.out.println(&quot;Random Generated Array:&quot;);
System.out.print(&quot;{&quot;);
// Start a for loop to print random numbers
for (int x = 0; x &lt; numbers.length; x++) {
numbers[x] = (int)(Math.random() * 101); // So that it prints random numbers from 0 to 100 (inclusive)
if (x == numbers.length) {
// Print out the 12 random numbers
System.out.print(numbers[x]);
} else {
System.out.print(numbers[x] + &quot;, &quot;)
}
System.out.print(&quot;}&quot;);
// Create space
System.out.println();
System.out.println();
// Create a statement 
System.out.println(&quot;Sorting At Each Stage:&quot;);
// Start looping numbers until 12
for (int i = 0; i &lt; numbers.length; i++) {
// Start looping numbers and comparing it to the &quot;i&quot; values
for (int j = i; j &lt; numbers.length; j++) {
if (numbers[j] &lt; numbers[i]) {
// Swap the positions of the numbers with the variable &quot;temp&quot;
int temp = numbers[j];
numbers[j] = numbers[i];
numbers[i] = temp;
}
// Create space
System.out.println();
// Use a for loop to print numbers at each stage of sorting
System.out.print(&quot;{&quot;);
for (int k = 0; k &lt; numbers.length; k++) {
System.out.print(numbers[k] + &quot;, &quot;);
}
System.out.print(&quot;}&quot;);
System.out.println();
}
}
// Create space
System.out.println();
System.out.println(&quot;\nBubble Sorted Array:&quot;);
System.out.print(&quot;{&quot;);
// Use a for loop to print the final sorted numbers in ascending order
for (int a = 0; a &lt; numbers.length; a++) {
System.out.print(numbers[a] + &quot;, &quot;);
}
System.out.print(&quot;}&quot;);
// Create space
System.out.println();
System.out.println();
}
}
System.out.println();
System.out.println();
}
}

答案4

得分: 0

如果您想要一种简单的方法来实现这个,只需创建一个数组来保存数字的字符串值。然后使用 String.join() 方法来为您完成操作。

int[] numbers = new int[12];
String[] strings = new String[numbers.length];
for (int x = 0; x < numbers.length; x++) {
    numbers[x] = (int) (Math.random() * 101); // 以便打印从 0 到 100(包括)的随机数字
    strings[x] = Integer.toString(numbers[x]);
}
System.out.printf("{%s}%n",
        String.join(", ", strings));

输出类似于

{3, 11, 24, 83, 36, 58, 22, 27, 43, 12, 30, 79}
英文:

If you want an easy way to do it, just create an array to hold a String value of the number. Then use the String.join() method to do the work for you.

int[] numbers = new int[12];
String[] strings = new String[numbers.length];
for (int x = 0; x &lt; numbers.length; x++) {
numbers[x] = (int) (Math.random() * 101); // So that it prints random numbers from 0 to 100 (inclusive)
strings[x] = Integer.toString(numbers[x]);
}
System.out.printf(&quot;{%s}%n&quot;,
String.join(&quot;, &quot;, strings));

Prints something like

{3, 11, 24, 83, 36, 58, 22, 27, 43, 12, 30, 79}
</details>

huangapple
  • 本文由 发表于 2020年10月17日 03:16:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64395137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定