指定拆分字符串解析INT [英] Assign split string to parsed int

查看:139
本文介绍了指定拆分字符串解析INT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误,我不能完全弄清楚如何解决它。


  

错误:
     结果不兼容的类型
  结果发现:INT
  结果要求:INT []


  
  

数组[X] =的Integer.parseInt(元素[0]);


这里是code代表我的方法。正在使用的文件是1000个号码,2%与500线线,以逗号分隔的文本文件。

例如:结果
结果1,2
结果16,92
结果109,7

该块的目的是为了阅读文本文件的所有行,并指定所有数字2D整数数组。

 公共静态INT [] [] writeTypes(){
    字符串的位置;
    的String [] =元素新的String [2];
    INT X;
    INT Y = 1;
    int数组[] [] =新INT [500] [2];
    文件TypesFile =新的文件(Types.txt);    尝试{
        扫描仪twoput =新的扫描仪(pkTypesFile);
        为(X = 0; twoput.hasNext(); X ++){
            位置= twoput.nextLine();
            元素= position.split(,,2);            数组[X] =的Integer.parseInt(元素[0]);
            数组[X] [Y] =的Integer.parseInt(元素[1]);            的System.out.println(数组[X] ++阵列[X] [Y]);
        }
    }赶上(例外五){
        System.err.format(类型的文件不存在\\ n);
    }
    返回数组;
}


解决方案

您似乎混淆了索引到多维数组。 阵列是一个2维数组,这意味着它是一个数组的数组。 新INT [500] [2] 创建一个长度为500的数组,其中每个元素是一个 INT [] 长度为2,其中每个元素是一个int。这位前pression 数组[X] 选择长度2的500 INT [] 阵列之一编译错误是说你不能一个int分配给 INT [] 。您需要提供另一个索引来选择数组中的整数由表示数组的一个[X]

具体来说,应该更改

 数组[X] =的Integer.parseInt(元素[0]);
数组[X] [Y] =的Integer.parseInt(元素[1]);
的System.out.println(数组[X] ++阵列[X] [Y]);

 数组[X] [0] =的Integer.parseInt(元素[0]);
数组[X] [1] =的Integer.parseInt(元素[1]);
的System.out.println(数组[X] [0] ++阵列[X] [1]);

第二个索引选择在整数之一 INT [] ,然后你就可以分配到。

i'm getting this error and i can't quite figure out how to fix it.

error:
incompatible types
found : int
required: int[]

array[x] = Integer.parseInt(elements[0]);

here is the code for my method. The file being used is a text file of 1000 numbers, 2 per line with 500 lines, separated by commas.

Example:

1,2
16,92
109,7

the purpose of this block is to read all lines of the text file, and assign all numbers to the 2d integer array.

public static int[][] writeTypes(){
    String position;
    String[] elements = new String[2];
    int x;
    int y=1;
    int array[][] = new int[500][2];
    File TypesFile = new File("Types.txt");

    try {
        Scanner twoput = new Scanner(pkTypesFile);
        for(x = 0; twoput.hasNext(); x++){
            position = twoput.nextLine();
            elements = position.split(",", 2);

            array[x] = Integer.parseInt(elements[0]); 
            array[x][y] = Integer.parseInt(elements[1]);

            System.out.println(array[x] + " " + array[x][y]);
        }
    } catch (Exception e) {
        System.err.format("Types File does not exist.\n");
    }
    return array;
}

解决方案

You seem to be confused about indexing into multidimensional arrays. array is a 2-dimensional array, meaning it's an array of arrays. new int[500][2] creates an array of length 500, each element of which is an int[] of length 2, each element of which is an int. The expression array[x] selects one of the 500 int[] arrays of length 2. The compile error is saying you can't assign an int to an int[]. You need to provide another index to select one of the ints in the array denoted by array[x].

Specifically, you should change

array[x] = Integer.parseInt(elements[0]); 
array[x][y] = Integer.parseInt(elements[1]);
System.out.println(array[x] + " " + array[x][y]);

to

array[x][0] = Integer.parseInt(elements[0]); 
array[x][1] = Integer.parseInt(elements[1]);
System.out.println(array[x][0] + " " + array[x][1]);

The second index selects one of the ints in the int[], which you can then assign to.

这篇关于指定拆分字符串解析INT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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