在java中,如何从数组中读取一行中包含多个元素的多行?

在java中,如何从数组中读取一行中包含多个元素的多行?,java,arraylist,split,Java,Arraylist,Split,这里是我到目前为止所做的:但是你应该看看打印屏幕,看看我得到了什么输出。下面是我试图读取的.txt文件中的元素: 1000 1500 2 750 600 500 3 100 250 750 600 5 1500 400 500 4 1000 1500 750 -1 500 1500 只是想让你知道这是什么,这是一个糟糕的游戏,上面的数字是我的spin()随机选择的值 public class Board { ArrayList<Cell> cellList = new A

这里是我到目前为止所做的:但是你应该看看打印屏幕,看看我得到了什么输出。下面是我试图读取的.txt文件中的元素:

1000
1500 2
750
600
500 3
100
250
750
600 5
1500
400
500 4
1000
1500
750
-1
500
1500
只是想让你知道这是什么,这是一个糟糕的游戏,上面的数字是我的spin()随机选择的值

public class Board {

    ArrayList<Cell> cellList = new ArrayList<>(); // creating an arraylist that holds the array elemetns in it.

    public Board() throws FileNotFoundException {
        java.io.File file = new java.io.File("CellValues.txt");
        System.out.println("isFile: " + file.exists());

        try (java.util.Scanner scan = new java.util.Scanner(file)) {
            while(scan.hasNext()){

                String line = scan.nextLine();
                String[] lineArr = line.split(" ");
                System.out.println(lineArr.length); // test number number of index

                if(lineArr.length == 1){
                    int cellLine = Integer.parseInt(line);              // to be passd to Cell constructor
                    Cell cellObject = new Cell(cellLine);
                    cellList.add(cellObject);

                if(lineArr.length == 2){
                    int SpecialCell = Integer.parseInt(line);      // to be passed to Special Cell constructor
                    SpecialCell specialCellObject = new SpecialCell(cellLine, SpecialCell);
                    cellList.add(specialCellObject); 
                    //cellLine *= SpecialCell;
                    //int x = cellObject.getValue() * (int) specialCellObject.getMultiplier();
                }   
            }
        }
            for(Cell i : cellList){
            System.out.println(i); 
            }

            System.out.println("\ncurrent elements in the arraylist are: " + cellList.size());

            scan.close();
        }
    }

现在,我在线程“main”java.lang.NumberFormatException中得到-->异常:对于输入字符串:“1500 2”

,看起来只有在数组包含1个元素时才会执行从第33行开始的块,这正是您不希望它执行的。将该块移到第一个
if
块之外,以便它可以执行。更妙的是,你能做到吗

if (lineArray.length == 1) {
    // code for one element here...
}
if (lineArray.length == 2) {
    // code for two elements here...
}
具体而言:

if(lineArr.length == 1){
    int cellLine = Integer.parseInt(line);              // to be passd to Cell constructor
    Cell cellObject = new Cell(cellLine);
    cellList.add(cellObject);
} 
if(lineArr.length == 2){  
    int SpecialCell = Integer.parseInt(line);      // to be passed to Special Cell constructor
    SpecialCell specialCellObject = new SpecialCell(cellLine, SpecialCell);
    cellList.add(specialCellObject); 
    //cellLine *= SpecialCell;
    //int x = cellObject.getValue() * (int) specialCellObject.getMultiplier();
}   

看起来只有当数组包含1个元素时,才会执行从第33行开始的块,这正是您不希望它执行的。将该块移到第一个
if
块之外,以便它可以执行。更妙的是,你能做到吗

if (lineArray.length == 1) {
    // code for one element here...
}
if (lineArray.length == 2) {
    // code for two elements here...
}
具体而言:

if(lineArr.length == 1){
    int cellLine = Integer.parseInt(line);              // to be passd to Cell constructor
    Cell cellObject = new Cell(cellLine);
    cellList.add(cellObject);
} 
if(lineArr.length == 2){  
    int SpecialCell = Integer.parseInt(line);      // to be passed to Special Cell constructor
    SpecialCell specialCellObject = new SpecialCell(cellLine, SpecialCell);
    cellList.add(specialCellObject); 
    //cellLine *= SpecialCell;
    //int x = cellObject.getValue() * (int) specialCellObject.getMultiplier();
}   

我想你想要原始行的两个值,对吗?您需要使用使用
lines.split()拆分的单个字符串

或许可以尝试这样的逻辑:

int firstValue = 0;      // initialize a first value (always used)
int secondValue = 0;     // initialize a second value (may or may not be used)

if(lineArr.length == 1){
        // parse just the first element. That's all we need.
        firstValue = Integer.parseInt(lineArr[0]);  
        // do stuff here...
}
if(lineArr.length == 2){
        // parse both elements.  We need both, right?
        firstValue = Integer.parseInt(lineArr[0]);
        lineArr[1] = lineArr[1].trim();  // remove any leading whitespaces!
        secondValue = Integer.parseInt(lineArr[1]);
        // do stuff here...
}
编辑

if(lineArr.length == 1){
        // parse just the first element. That's all we need.
        firstValue = Integer.parseInt(lineArr[0]);  
        // do stuff here...
}
if(lineArr.length > 1){
        firstValue = Integer.parseInt(lineArr[0]);
        for (int i = 1; i < lineArr.length; ++i) {
            if (!lineArr[i].equals("")) {
                secondValue = Integer.parseInt(lineArr[i]);
            }
        }
        // do stuff here with firstValue and secondValue
}
if(lineArr.length==1){
//只解析第一个元素,这就是我们所需要的。
firstValue=Integer.parseInt(lineArr[0]);
//在这里做事。。。
}
如果(lineArr.length>1){
firstValue=Integer.parseInt(lineArr[0]);
对于(int i=1;i
我想您需要原始行中的两个值,对吗?您需要使用使用
lines.split()拆分的单个字符串

或许可以尝试这样的逻辑:

int firstValue = 0;      // initialize a first value (always used)
int secondValue = 0;     // initialize a second value (may or may not be used)

if(lineArr.length == 1){
        // parse just the first element. That's all we need.
        firstValue = Integer.parseInt(lineArr[0]);  
        // do stuff here...
}
if(lineArr.length == 2){
        // parse both elements.  We need both, right?
        firstValue = Integer.parseInt(lineArr[0]);
        lineArr[1] = lineArr[1].trim();  // remove any leading whitespaces!
        secondValue = Integer.parseInt(lineArr[1]);
        // do stuff here...
}
编辑

if(lineArr.length == 1){
        // parse just the first element. That's all we need.
        firstValue = Integer.parseInt(lineArr[0]);  
        // do stuff here...
}
if(lineArr.length > 1){
        firstValue = Integer.parseInt(lineArr[0]);
        for (int i = 1; i < lineArr.length; ++i) {
            if (!lineArr[i].equals("")) {
                secondValue = Integer.parseInt(lineArr[i]);
            }
        }
        // do stuff here with firstValue and secondValue
}
if(lineArr.length==1){
//只解析第一个元素,这就是我们所需要的。
firstValue=Integer.parseInt(lineArr[0]);
//在这里做事。。。
}
如果(lineArr.length>1){
firstValue=Integer.parseInt(lineArr[0]);
对于(int i=1;i
为什么不能在这里添加代码?只是因为我在打印屏幕上注释了解释,但我会立即发布代码…使用IDE自动缩进代码,您应该会立即看到其中一个问题。第二个问题是,您试图将包含两个值的行解析为整数。这没什么道理。@jbnize我不是向上帝发誓,我同意你的观点,但我不知道教授为什么在作业指导书中这样说!!!你认为有别的办法吗?嗯,我们不知道说明书。但我想你必须在这行分析这两个数字。因为你只是把那条线分割成一个数组,这很容易,不是吗?请注意,
if(lineArr.length==1&&lineArr.length==2)
没有任何意义。长度不能同时等于1和2。为什么不能在这里添加代码?只是因为我在打印屏幕上注释了解释,但我会立即发布代码…使用IDE自动缩进代码,您应该会立即看到其中一个问题。第二个问题是,您试图将包含两个值的行解析为整数。这没什么道理。@jbnize我不是向上帝发誓,我同意你的观点,但我不知道教授为什么在作业指导书中这样说!!!你认为有别的办法吗?嗯,我们不知道说明书。但我想你必须在这行分析这两个数字。因为你只是把那条线分割成一个数组,这很容易,不是吗?请注意,
if(lineArr.length==1&&lineArr.length==2)
没有任何意义。长度不能同时等于1和2。当我这样做时,第二个if内的单元格线无法访问。请注意,我正在将其传递给specialCellObject构造函数。
的else if
似乎没有必要,因为数组的长度只能是1或2,但不能同时是2。@TimBiegeleisen Right。谢谢@AhmedHadi看起来您需要赋值
int cellLine=Integer.parseInt(line)lineArray
的长度是1还是2,代码>都会生成。所以在两个块中都写,或者在两个块之前都写。@AlessandroScarlatti我已经试过了,但是我得到了NumberFormatException:对于输入字符串:“1500 2”,当我这样做时,第二个if中的单元格线无法访问。请注意,我正在将其传递给specialCellObject构造函数。
的else if
似乎没有必要,因为数组的长度只能是1或2,但不能同时是2。@TimBiegeleisen Right。谢谢@AhmedHadi看起来您需要赋值
int cellLine=Integer.parseInt(line)lineArray
的长度是1还是2,代码>都会生成。所以在两个块中都写,或者在两个块之前都写。@AlessandroScarlatti我试过了,但我得到了NumberFormatException:对于输入字符串:“15002”天哪,我非常感谢,它成功了!现在它用两个值打印元素的第一个值,而不是用空格分隔的另一个值,可以找出原因吗?
lines.split(“”)
仅在准确找到您发送的分隔符时才会找到第二个值:
。因此,如果有多个空格,它将无法工作:例如,
123456
。通过添加
lineArr[1]=lineArr[1].trim()修复此问题