简体   繁体   English

Java扫描仪例外java.util.NoSuchElementException

[英]Java Scanner excenprion java.util.NoSuchElementException

Hello I have a problem reading integer values from scanner. 您好,我从扫描仪读取整数值时遇到问题。 Program sorts two arrays and saves the sorted values into new array and then it writes it on the console output. 程序对两个数组进行排序,然后将排序后的值保存到新数组中,然后将其写入控制台输出中。 Here is exception: 这是例外:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at U42Slevani.main(U42Slevani.java:40)

Here is my code. 这是我的代码。

package u42slevani;
import java.util.Scanner;



/**
 *
 * @author matej.rehak
 */
public class U42Slevani {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner sc = new Scanner(System.in);
        int[] pole1;
        int[] pole2;
        int pocet=1;


        while(pocet>0)
        {

            int pom;
            int j;



        pocet = sc.nextInt();
        if(pocet<0 || pocet>1000)
        {
            break;
        }
        pole1 = new int[pocet];
        for(int i = 0;i<pole1.length;i++)
        {
            pole1[i] = sc.nextInt();
        }

        pocet = sc.nextInt();   //problem here

        if(pocet<0 || pocet>1000)
        {
            break;
        }
        pole2  = new int[pocet];
        for(int i = 0;i<pole2.length;i++)
        {
            pole2[i] = sc.nextInt();
        }

            for (int i = 1; i<pole1.length; i++)
            {
                pom = pole1[i];
                j = i - 1;
                while ((j >= 0 ) && (pole1[j] > pom))
                {
                    pole1[j+1] = pole1[j];
                    j--;
                }
                pole1[j+1] = pom;
            }

            for (int i = 1; i<pole2.length; i++)
            {
                pom = pole2[i];
                j = i - 1;
                while ((j >= 0 ) && (pole2[j] > pom))
                {
                    pole2[j+1] = pole2[j];
                    j--;
                }
                pole2[j+1] = pom;
            }

            int[] pole3 = new int[pole1.length+pole2.length];
            merge(pole3, pole1, pole2);
            StringBuilder vystup = new StringBuilder("");
            for (int i = 0; i < pole3.length - 1; i++) 
            {
                vystup.append(pole3[i] + " ");
            }
            vystup.append(pole3[pole3.length -1]);
            System.out.println(vystup);


        }
    }

  public static void merge(int[] list, int[] left, int[] right) {
  int i = 0;
  int j = 0;
  // dokud nevyjedeme z jednoho z poli
  while ((i < left.length) && (j < right.length)) {
    // dosazeni toho mensiho prvku z obou poli a posunuti indexu
    if (left[i] < right[j]) {
      list[i + j] = left[i];
      i++;
    }
    else {
      list[i + j] = right[j];
      j++;
    }
  }
  // doliti zbytku z nevyprazdneneho pole
  if (i < left.length) {
    while (i < left.length) {
      list[i + j] = left[i];
      i++;
    }
  }
  else {
    while (j < right.length) {
      list[i + j] = right[j];
      j++;
    }
  }
}

}

I know problem is on line 41 but I don't know why. 我知道问题出在第41行,但我不知道为什么。 Thank you. 谢谢。

You are trying to read the next integer from the underlying stream of the Scanner by calling nextInt() , even though you are not sure there is something to be read. 您正在尝试通过调用nextInt()Scanner的基础流中读取下一个整数,即使您不确定要读取的内容也是如此。 If you try to read the next integer when there is none to be read, a NoSuchElementException will be thrown. 如果在没有要读取的整数时尝试读取下一个整数,则将引发NoSuchElementException

Solve this by first checking if there is an integer to be read, and in that case read it. 首先检查是否有整数要读取,然后读取该整数来解决。

if(sc.hasNextInt()) 
{
   pocet = sc.nextInt();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM