使用For语句分配矩阵值 – Java

huangapple go评论81阅读模式
英文:

Assigning Matrix Values using For Statement - Java

问题

public class Solution {

public static void main(String[] args) {
    
    Scanner prompt = new Scanner(System.in);
    int[][] array = new int [prompt.nextInt()+1][prompt.nextInt()];
    
    // Create matrix
    for (int a = 0; a < array.length; a++){
        for (int b = 0; b < array[a].length; b++){
           array[a][b] = prompt.nextInt();          /*This is the line 14*/
        }
    }
    
    for (int h=0; h < array.length; h++){ 
        for(int c=0; c < array[h].length; c++){ 
            System.out.printf("%5d",array[h][c]);
        }
        System.out.println("");
    } } }
英文:

I am working on HackerRank that is a platform that allows students to upload their Homeworks code, then the platform runs some random tests on our code. On this opportunity, my teacher has asked me to create a matrix on base from the input from HackerRank and then fill it also with more inputs from this platform.

I have created this code to create a matrix with the dimensions on which HackerRank wants and also refill it with the values generated of the platform but seems to have an error on my code and I don't see it.

It gives me this error:

Exception in thread &quot;main&quot; 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:14)

My code:

public class Solution {

public static void main(String[] args) {
    
    Scanner prompt = new Scanner(System.in);
    int[][] array = new int [prompt.nextInt()+1][prompt.nextInt()];
    
    // Create matrix
    for (int a = 0; a &lt; array.length; a++){
        for (int b = 0; b &lt; array[a].length; b++){
           array[a][b] = prompt.nextInt();          /*This is the line 14*/
        }
    }
    
    for (int h=0; h &lt; array.length; h++){ 
        for(int c=0; c &lt; array[h].length; c++){ 
            System.out.printf(&quot;%5d&quot;,array[h][c]);
        }
        System.out.println(&quot;&quot;);
    } } }

答案1

得分: 1

Scanner抛出NoSuchElementException时,这意味着你正试图从扫描器中读取内容,而此时已经到达输入的末尾。

在for循环中的nextInt之前尝试使用hasNextInt

英文:

When a Scanner Throws a NoSuchElementException it means that you're trying to read from the scanner when you've already reached the end of the input.

Try using hasNextInt just before the nextInt in the for loop.

答案2

得分: 0

NoSuchElementException在这种情况下表示没有更多的标记可供读取。您在输入上提供了足够的值吗?

参见:https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextInt--

英文:

NoSuchElementException in this case indicates that there are no more tokens to be read. Did you provide enough values on the input?

See: https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextInt--

huangapple
  • 本文由 发表于 2020年9月21日 04:18:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63983285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定