如何将用户输入到两个单独的数组中?

时间:2014-03-14 20:14:03

标签: java arrays

您好,我在使用Scanner时无法获得两个ArrayList的用户输入。当我运行此代码时,在输入两个数组后我得到IndexOutOfBounds异常。 该代码使用纹波加法器的逻辑将两个二进制数加在一起。预期用户输入的示例是

输入数组:1 0 1 0 输入B数组:0 0 0 1 产量:1 0 1 1

当数组是硬编码时,代码可以工作,如何让用户进入数组?

代码如下所示

import java.util.*;

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

        Scanner inputA = new Scanner(System.in);
        ArrayList<Integer> aList = new ArrayList<Integer>();
        ArrayList<Integer> bList = new ArrayList<Integer>();
        int c = 0;

        System.out.println("Enter A array");
        aList.add(inputA.nextInt());

        Scanner inputB = new Scanner(System.in);
        System.out.println("Enter B array");
        bList.add(inputB.nextInt());

        Adder bit1 = new Adder(parseInput(aList.get(3)), parseInput(bList.get(3)), parseInput(c));
        Adder bit2 = new Adder(parseInput(aList.get(2)), parseInput(bList.get(2)), bit1.getCout());
        Adder bit3 = new Adder(parseInput(aList.get(1)), parseInput(bList.get(1)), bit2.getCout());
        Adder bit4 = new Adder(parseInput(aList.get(0)), parseInput(bList.get(0)), bit3.getCout());

        if (bit4.getCout() == false) {
            System.out.println(bit4.toString() + " " + bit3.toString() + " " + bit2.toString() + " " + bit1.toString());
        } else {
            System.out.println("overflow!");
        }

    }

    public static boolean parseInput(int i) {

        if (i == 1) {
            return true;
        } else {
            return false;
        }

    }
}

Adder类代码:

    public class Adder {

    private boolean a, b, cin, cout, s;

    /**
     * Full Adder contructor
     */

    public Adder(boolean a, boolean b, boolean cin) {

        this.a = a;
        this.b = b;
        this.cin = cin;

        s = nand(nand(a, b), cin); //sum bit
        cout = or(and(nand(a, b), cin), and(a, b)); // - carry bit

    }

    /** Half adder constructor */

    //    public Adder (bloolean a, boolean b) {
    //
    //      this.a = a;
    //      this.b = b;
    //
    //      s = 
    //}

    /**
     * NAND gate
     */
    public boolean nand(boolean a, boolean b) {

        return a ^ b;

    }

    /**
     * AND gate
     */
    public boolean and(boolean a, boolean b) {

        return a && b;

    }

    /**
     * OR gate
     */
    public boolean or(boolean a, boolean b) {

        return a || b;

    }

    public boolean getCout() {

        return cout;

    }

    public String toString() {

        if (s == true) {
            return "1";
        } else {
            return "0";
        }

    }

    public String toStringCout() {

        if (cout == true) {
            return "1";
        } else {
            return "0";
        }

    }
}

4 个答案:

答案 0 :(得分:1)

Scanner.nextInt获取输入中的下一个整数,然后停止。每个列表只包含1个元素。

请改用以下内容:

String[] input = inputA.nextLine().split(" ");
for (String s : input)
{
    try { aList.add(Integer.parseInt(s)); }
    catch(NumberFormatException nfe) { /* handle exception as desired */ }
}

或者,你应该可以使用类似的东西:

while (inputA.hasNextInt())
{
    aList.add(inputA.nextInt());
}

答案 1 :(得分:1)

通过以稍微不同的方式接受输入,然后使用for循环添加每个位,可以简化和改进整个AdderApp类以接受任何位长度。 parseInput函数可以用简单的布尔比较替换:

import java.util.*;

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

        Scanner input = new Scanner(System.in);

        System.out.println("Enter A array");
        char[] aIn = input.nextLine().replace(" ", "").toCharArray();
        System.out.println("Enter B array");
        char[] bIn = input.nextLine().replace(" ", "").toCharArray();

        StringBuilder result = new StringBuilder();        
        Adder bit = new Adder(false, false, false);

        for (int i = aIn.length - 1; i >= 0; --i) {
            bit = new Adder((aIn[i] == '1'), (bIn[i] == '1'), bit.getCout());
            result.append(bit + " ");
        }
        System.out.println(bit.getCout() ? "overflow!" : result.reverse());
    }
}

答案 2 :(得分:0)

用户应输入4个号码,您的号码只允许用户输入1个号码:

int count = 0;
Scanner inputA = new Scanner(System.in);
System.out.println("Enter A array");
while(count < 4){
  count++;
  aList.add(inputA.nextInt());
}
count = 0;

Scanner inputB = new Scanner(System.in);
System.out.println("Enter B array");
while(count < 4){
  count++;
  bList.add(inputB.nextInt());
}

如果您想使用hasNextInt()

while(inputA.hasNextInt()){
  count ++;
  aList.add(inputA.nextInt());
  if(count == 4){
    count = 0;
    break;
  }
}

答案 3 :(得分:0)

你应该有一个for循环来输入你的ArrayList。

    System.out.println("Enter A array");
    for (int i = 0; i < 4; i++) {
        aList.add(inputA.nextInt());
    }


    Scanner inputB = new Scanner(System.in);

    System.out.println("Enter B array");           
    for (int i = 0; i < 4; i++) {
        bList.add(inputB.nextInt());
    }