【问题标题】:Programming Challenges 110106 [closed]编程挑战 110106 [关闭]
【发布时间】:2014-01-20 17:52:36
【问题描述】:

我目前正在编程挑战网站上做问题 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() {
    }

}

【问题讨论】:

  • 请正确缩进您的代码。没有人会照原样阅读它。
  • 请说明您的目标是什么。该算法可能不是答案的正确解决方案。
  • 他们教你如何在编程挑战中compare strings

标签: java exception


【解决方案1】:

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

while (input.hasNext()) {

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

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

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

【讨论】:

  • 谢谢,您的解决方案解决了我的问题。从现在开始也将成为 .equals() 的女巫:)
【解决方案2】:
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("")

【讨论】: