编程挑战110106

时间:2014-01-20 17:52:37

标签: java exception

我目前正在编程挑战网站上做问题110106。我在下面的文件中的第二个000上得到NoSuchElementException

下面是我的代码,任何帮助都会很棒。

1

299
492
495
399
492
495
399
283
279
689
078
100
000
000
000

以下是示例:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Problem110106 {

    int mainCounter = 0;
    int array[] = new int[10];

    public static void main(String[] args) throws FileNotFoundException, NoSuchElementException {
        // TODO Auto-generated method stub

        Problem110106 problem = new Problem110106();
        problem.start();
    }

    public void start() throws FileNotFoundException, NoSuchElementException {
        Scanner input = new Scanner(new BufferedReader(new FileReader("text.txt")));
        int numTestCases = Integer.parseInt(input.nextLine().trim());
        System.out.println(numTestCases);
        String blank = input.nextLine();
        System.out.println(blank);

        while (input.hasNext()) {

            int num = Integer.parseInt(input.next());
            if (input.next() == "") {

            } else {

                int first = num / 100;
                int second = (num / 10) % 10;
                int third = (num % 10);

                System.out.println(first);
                System.out.println(second);
                System.out.println(third);

                switch (first) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    set(first, second, third);
                    break;
                case 3:
                    add(first, second, third);
                    break;
                case 4:
                    multiply(first, second, third);
                    break;
                case 5:
                    set(first, second, third);
                    break;
                case 6:
                    add(first, second, third);
                    break;
                case 7:
                    multiply(first, second, third);
                    break;
                case 8:
                    set2(first, second, third);
                    break;
                }
            }
        }
    }

    public void set(int first, int second, int third) {
        array[second] = third;
        mainCounter++;
        // System.out.println("got here");
    }

    public void add(int first, int second, int third) {
        array[second] = array[second] + third;
        mainCounter++;
    }

    public void multiply(int first, int second, int third) {
        array[second] = array[second] * third;
        mainCounter++;
    }

    public void set2(int first, int second, int third) {
        // array[second] =
    }

    public void goTo() {
    }

}

2 个答案:

答案 0 :(得分:0)

while (input.hasNextInt()) {
    int num = Integer.parseInt(input.next());   
    if (!input.hasNext() || input.next().isEmpty()){
        break; // Jump out of the loop.
    } else {

字符串通常比较为:

    input.next().equals("")

答案 1 :(得分:0)

我认为你的问题可能就在这里

while (input.hasNext()) {

      int num = Integer.parseInt(input.next());
      if (input.next() == "") {

第二行读取输入,但下一行也是如此。因此,当循环位于最后一个元素时,第一行将读取它,然后下一行将尝试读取不存在的下一个元素,因此NoSuchElementException

不要将字符串与==进行比较。使用equals()equalsIgnoreCase()