此消息不断出现“java.util.NoSuchElementException”

时间:2015-09-17 01:15:24

标签: java

import java.util.Scanner;

公共类DisplayBox2 {

public static void box (int length, int width){ 

    Scanner input = new Scanner (System.in) ;
    String Answer; 

    System.out.println("Do you want to use a special character to use to display the box ?"); 
      Answer = input.nextLine();

    if (Answer == "Yes"){
        System.out.println("Please enter the character that you would like to display the box");
        int Char = input.nextInt(); 
    for (int i = 0; i < length; i++) {
           for (int j = 0; j < width; j++) {
                System.out.print(Char +" ");
            }
            System.out.println("");
        }
    }

    if (Answer == "No"){

        for (int i = 0; i < length; i++) {
            for (int j = 0; j < width; j++) {
                System.out.print(" *");
            }
            System.out.println("");
        }
    }


    input.close(); 
}

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

    Scanner input = new Scanner (System.in);  

    int length, width ; 

    System.out.println ("Please enter the length of the box"); 
    length = input.nextInt();
    System.out.println (" Please enter the width of the box");
    width = input.nextInt(); 

    input.close(); 


    box (length, width); 


}

}

我不明白我的代码中的错误是什么。任何人都可以请帮助

请输入方框的长度

5  请输入框的宽度

5 您想使用特殊字符来显示该框吗?

线程“main”中的异常java.util.NoSuchElementException:找不到行

at java.util.Scanner.nextLine(Unknown Source)

at methods.DisplayBox2.box(DisplayBox2.java:14)

at methods.DisplayBox2.main(DisplayBox2.java:56)

2 个答案:

答案 0 :(得分:1)

我已更正了您的工作代码:

public static void box(int length, int width, Scanner input) {

        String answer;

        System.out.println("Do you want to use a special character to use to display the box ?");
        answer = input.next();
        if (answer.equals("Yes")) {
            System.out.println("Please enter the character that you would like to display the box");
            int Char = input.nextInt();
            for (int i = 0; i < length; i++) {
                for (int j = 0; j < width; j++) {
                    System.out.print(Char + " ");
                }
                System.out.println("");
            }
        }else if (answer.equals("No")) {
            for (int i = 0; i < length; i++) {
                for (int j = 0; j < width; j++) {
                    System.out.print(" *");
                }
                System.out.println("");
            }
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        try {
            int length, width;
            System.out.println("Please enter the length of the box");
            length = input.nextInt();
            System.out.println(" Please enter the width of the box");
            width = input.nextInt();

            box(length, width, input);
        }catch (Exception ex){
            ex.printStackTrace();
        }finally{
            input.close();
        }
    }

注意事项:

  • 当您使用input.close()时,您正在关闭System.in而非关闭扫描程序,因此您将获得NoSuchElementException
  • 仅将字符串与equals()进行比较 - 比较除'=='以外的比较引用的值。对于学习者来说,这是一个基本但常见的错误
  • 使用try/catch/finally,尤其是finally来关闭/清理资源。如果你使用JDK 1.7或更高版本,尝试使用资源可以帮助更好

答案 1 :(得分:-1)

包方法;

import java.util.Scanner;

公共类DisplayBox2 {

public static void box (int length, int width, String character){ 

    Scanner input = new Scanner (System.in) ;

    for (int i = 0; i < length; i++) {
           for (int j = 0; j < width; j++) {
                System.out.print(character +" ");
            }
            System.out.println("");
        }
    }





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

    Scanner input = new Scanner (System.in);  

    int length, width ; 
    String Answer; 
    String character = ""; 

    System.out.println("Do you want to use a special character to use to display the box ?"); 
      Answer = input.next();

  if (Answer.equals("Yes")){
    System.out.println("Please enter the character that you would like to display the box");
     character =  input.next();
  }

  if (Answer == "No"){
      character = "*"; 
  }
    System.out.println ("Please enter the length of the box"); 
    length = input.nextInt();
    System.out.println (" Please enter the width of the box");
    width = input.nextInt(); 

    input.close(); 


    box (length, width, character); 


}
}

我的回答。我没有使用try catch,但最终却很有效。