如何遍历字符串数组并在新的 int 数组中列出所有数字并将单词留在字符串数组中

How to go through a String array and list all numbers in a new int Array and leave the words in the string array

所以使用 java,我的问题是我有一个 .txt 文件,其中数字和单词用空格分隔,我想创建 2 个数组。只用单词填充第一个数组,我希望新的第二个数组将所有数字作为整数插入。

    public static void main(String[] args) throws FileNotFoundException {

    File inputFile = new File("numbersandwords.txt");
    Scanner scan = new Scanner(inputFile).useDelimiter(" ");
    String[] words = new String[20];
    int[] numbers = new int[20];
    int i = 0;

    while (scan.hasNext()) {
        words[i] = scan.next();
        i++;
    }

    //Insert Code here

    System.out.println(Arrays.toString(words));
    System.out.println(Arrays.toString(numbers));

   }
   }

到目前为止,我已经用 words[] 填充了 txt 文件的所有内容。但我不确定如何将 words[] 中的数字移动到 numbers[].

这也不是作业。我这样做只是为了自己学习 java。我正在阅读一本有编程问题但没有答案的教科书。

如有任何帮助,我们将不胜感激。

你可以使用

while (scan.hasNext()) {
        words[i] = scan.next();
        number[i] = words[i].replaceAll("\D+","");  //delete non-digits.

        i++;
    }

不要将单词从单词列表移动到数字列表。首先检查你读到的单词是数字还是单词,然后把它放在正确的列表中。

要检查字符串是否为数字,您可以使用:

String word = scan.next();
...
int i = Integer.valueOf(word);

如果您的字符串不是数字,它将抛出异常,您必须捕获它。如果您的数字有浮点数,请使用 Double 而不是 Integer

并且不要使用固定大小的数组,您不知道将拥有多少个元素。使用 ArrayList,您可以动态添加新元素。

ArrayList<String> words = new ArrayList<String>();
...
words.add("word");

你可以试试这个:

File inputFile = new File("d:\data.txt");
Scanner scan = new Scanner(inputFile).useDelimiter(" ");
String[] words = new String[20];
String[] temp = new String[20];
int[] numbers = new int[20];
String regex = "[0-9]+";
int i = 0;
int j=0;

while (scan.hasNext()) {
    words[i] = scan.next();
    if(words[i].matches(regex)){
    numbers[j]=Integer.parseInt(words[i]);// extracting numbers only
    i--;   // skipping null value
    j++;

    }else{
        temp[i]=words[i];   // extracting words
    }
    i++;
}
words=temp;  // copy temp to words array

//Insert Code here

System.out.println(Arrays.toString(words));
System.out.println(Arrays.toString(numbers));

文件包含:

345 hello this is 10 file handling 2 4 56 78 345

输出:

[hello, this, is, file, handling, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
[345, 10, 2, 4, 56, 78, 345, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

注意: 确保您的数组大小足以容纳文件中的数据

编辑:从数组中删除 null 和 0。

System.out.println(Arrays.toString(Arrays.copyOfRange(temp,0, j-1)));
System.out.println(Arrays.toString(Arrays.copyOfRange(numbers,0, i-1)));

输出:

[hello, this, is, file, handling, 35chainatouwn]
[345, 10, 2, 4, 56]

这是解决方案..

public class 测试类 {

public static void main(String[] args) throws FileNotFoundException {

    File inputFile = new File("numbersandwords.txt");
    Scanner scan = new Scanner(inputFile).useDelimiter(" ");
    String[] words = new String[20];
    int[] numbers = new int[20];
    int i = 0;
    int j=0;

    while (scan.hasNext()) {
        String s = scan.next();
        if (isInteger(s)) {
            numbers[i] = Integer.parseInt(s);
            i++;
        }else{
            words[j] = s;
            j++;
        }

    }

    System.out.println(Arrays.toString(words));
    System.out.println(Arrays.toString(numbers));
}

public static boolean isInteger(String s) {
    try {
        int myInt = Integer.parseInt(s.trim());
        return true;
    } catch (Exception e) {
        return false;
    }
}

}