如何在字符串中添加未定义数量的数字字符,然后将字符串转换为int? [英] How can I add an undefined number of digit characters to a string, so then I can convert the string to an int?

查看:123
本文介绍了如何在字符串中添加未定义数量的数字字符,然后将字符串转换为int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个简单的程序,要求您使用键盘输入数字(每个数字用空格或逗号分隔),然后将它们从低到高排序。

I wanted to write a simple program that asks you to enter numbers by keyboard (being each number separated by a space or by a comma) and then sorts them from lower to higher.

我想到的是要求用户通过键盘输入数字,将输入设置为String对象,然后将String通过for循环传递,该循环确定字符是数字还是普通字符。
如果角色是数字,则将其附加到String数组的字段中,并寻找下一个数字,然后执行相同的操作,直到一个字符不是数字为止;在这种情况下,它将寻找另一个数字并重复相同的过程,但要使用下一个String数组的字段。
程序通过字符串的长度后,字符串数组的每个字段都会转换为一个int,因此可以对其进行排序。

What I had in mind was asking the user to enter the numbers by keyboard, setting the input to a String object, and then passing the String through a for loop that determines if a character is a digit or a normal character. If the caracter is a digit, it would be appended to a String array's field and look for the next digit and do the same thing until one character isn't a digit; in which case, it would look for another digit and repeat the same process but with the next String array's field. Once the program has passed through the hole String's length, each field of the String Array would be converted into an int, so it can be sorted.

问题是我不知道如何向 String 中添加未定义数量的字符。
我的工作是:

The problem is that I don't know how to add an undefined number of chars to a String. What I do is the following:

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

    int[] numbers;
    String input;
    boolean fieldUsed = false;
    int len = 0;
    Scanner scanner = new Scanner(System.in);
    input = scanner.next();
    numbers = new int[input.length()];
    String[] aux = new String[input.length()];
    int numOfAuxField = 0;
    for (int i = 0; i < input.length(); i++) {
        do{
        if(Character.isDigit(input.charAt(i))){
            aux[numOfAuxField].append((input.charAt(i)).toString());
            i++;
            fieldUsed = true;
        }else{
            if(fieldUsed == true){
                numOfAuxField++;
            }
        } while(Character.isDigit(input.charAt(i)));
    }

    for (int a = 0; a < numbers.length; a++){
        if(aux[a] != null){
        numbers[a] = Integer.parseInt(aux[a]);
        }
    }
}
}

但是它说


不能在原始类型char上调用toString()

Cannot invoke toString() on the primitive type char

在第16行。
我已经在寻找答案了,但它们都是为了预定义的字符数。

in the 16th line. I already looked for an answer but they are all for a predefined number of characters.

我该怎么办?

推荐答案

您应使用以下方式之一:

You should use one of the following ways:


  1. Character.toString(input.charAt(i))

  2. + input.charAt(i)

  3. String.valueOf(input.charAt(i))

  1. Character.toString(input.charAt(i))
  2. "" + input.charAt(i)
  3. String.valueOf(input.charAt(i))

而不是(input.charAt(i))。toString()

原始类型无法使用它们。

Primitive types have no methods to work with them.

这篇关于如何在字符串中添加未定义数量的数字字符,然后将字符串转换为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆