获取java.util.NoSuchElementException

时间:2016-03-20 07:20:01

标签: java java.util.scanner

我正在尝试为problem实施解决方案。代码就像eclipse中的魅力一样,但在Hacker Rank IDE中编译时会产生以下异常:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Solution.main(Solution.java:17)

这是代码

import java.util.Scanner;
class Ideone {
    public static void main(String[] args) throws Exception {
        Scanner input=new Scanner(System.in);

        int test_case = input.nextInt();
        //System.out.println("test case"+test_case);

        String ans[] = new String[test_case];

        for (int i = 0; i < test_case; i++) {
            int row = input.nextInt();
            int col =input.nextInt();
            //System.out.println("row:"+ row+" col: "+col);
            Result result = new Result(row, col);
            result.putRandom();
            //result.display();
            result.getPattern();
            ans[i] = result.match();

        }

        for (int i = 0; i < ans.length; i++) {
            System.out.println(ans[i]);
        }

    }

}

class Result {
    String[] matrix;
    int rows;
    int col;

    int pattern_row;
    int pattern_col;
    String[] pattern_matrix;
    Scanner inp=new Scanner(System.in);

    public Result(int r, int c) {
        rows = r;
        col = c;
        matrix = new String[rows];
    }

    public void putRandom() throws Exception {
        // System.out.println("Row:"+rows+" Colems:"+col);
    //Scanner inp = new Scanner(System.in);
        for (int i = 0; i < rows; i++) {
            matrix[i] =inp.nextLine();
            //System.out.println("matrix[i] : "+matrix[i] );
        }
    }

    public void display() {
        for (int i = 0; i < rows; i++) {
            System.out.print(matrix[i] + " ");
            System.out.println();
        }
    }

    public void getPattern() throws Exception{
        int r= inp.nextInt();
        int c =inp.nextInt();

        pattern_row = r;
        pattern_col = c;
        //System.out.println("pAttern row"+pattern_row);
        pattern_matrix = new String[pattern_row];
        putRandomInPattern();
    }

    public void putRandomInPattern() throws Exception{
        //Scanner inp = new Scanner(System.in);
        inp.nextLine();
        for (int i = 0; i < pattern_row; i++) {
            pattern_matrix[i] = inp.nextLine();
        }
        //display();
    }

    public String match() {
        String result = "NO";
        for (int r = 0; r < pattern_row; r++) {
            int index = find(pattern_matrix[r]);
            if (index != -1) {
                result = isPatternPresent(index, r);
                return result;
            }
        }
        return result;
    }

    public String isPatternPresent(int index_matrix, int index_pattern) {
        int matched = 0;
        int j = index_pattern;
        for (int i = index_matrix; i < (index_matrix+(pattern_row)); i++) {
            if (matrix[i].contains(pattern_matrix[j])) {
                matched++;
                j++;
            }
        }
        //System.out.println("matched:" + matched);
        if (matched == pattern_row)
            return "YES";
        return "NO";
    }

    public int find(String elem) {
        for (int i = 0; i < matrix.length; i++) {
            if (matrix[i].contains(elem)) {
                //System.out.println("found" + matrix[i]);
                return i;
            }
        }
        return -1;
    }

}

1 个答案:

答案 0 :(得分:0)

本部分中发生了一些事情

@mixin myStyle($color) {
  color: $color;
  border-left: 5px solid $color;
}
.item{
 @include myStyle(red); // i want to pass color value from my html class.
}

我认为你应该在使用它时将扫描程序传递给putRandom()方法,而不是在该类中创建一个新的扫描程序,这可能会搞乱hackerrank

    for (int i = 0; i < rows; i++) {
        matrix[i] =inp.nextLine();
        //System.out.println("matrix[i] : "+matrix[i] );
    }