如何扫描并正确放置数组?

时间:2019-03-28 13:49:21

标签: java arrays java.util.scanner

我创建了三个类Main,ProgrammeInterFace和LoginInfo

LoginInfo:我应该从用户那里获取他的名字和ID

ProgrammeInterFace:我应该从用户那里获得关于Question的一些答案

我是在LoginInfo和ProgrammeInterFace Scanner Obj中创建的,然后当我在Main中调用它们并运行程序时

此错误出现

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 finalproject.ProgramInterFace.interFace(ProgramInterFace.java:45)
at finalproject.Main.main(Main.java:11)

这是类LoginInfo

package finalproject;

import java.util.Scanner;

public class LoginInformation {

public static void loginInfo() {
    String name, id , container[];

    container = new String[2];

    Scanner scan = new Scanner(System.in);

    System.out.println("Please, Enter Your Name: ");
    name = scan.nextLine();
    System.out.println("Please, Enter Your Test Registration ID: ");
    id = scan.nextLine();

    container[0] = name;
    container[1] = id;

    scan.close();

}
}

这是ProgrammeInterFace类

package finalproject;

import java.util.Scanner;

public class ProgramInterFace {

    public static void interFace() {
    Scanner scan = new Scanner(System.in);

    byte[] userAnswers;

    userAnswers = new byte[3];


    System.out.println("\n\n... Starting Test\n\n");
    System.out.println("- Choose the Correct Answer:\n");

    System.out.println(

            "a. Which Utility is used to compile Java applications?\n" 
    + "\t1. javaw\n" 
    + "\t2. Java\n" 
    + "\t3. javac\n"

            );

    userAnswers[0] = scan.nextByte();

    System.out.println(

            "b. Which is a restriction when using a switch statement\n" 
    + "\t1. Characters cannot be used\n" 
    + "\t2. Doubles cannot be used\n" 
    + "\t3. Integers cannot be used\n"

            );

    userAnswers[1] = scan.nextByte();

    System.out.println(
            "c. What is the range of byte data type in Java\n" 
    + "\t1. -128 to 127\n" 
    + "\t2. -32768 to 32767\n" 
    + "\t3. -2147483648 to 2147483647\n"

            );

    userAnswers[2] = scan.nextByte();

    scan.close();


}
}

这是主要的

package finalproject;


public class Main {

public static void main(String[] args) {
    // User Information 
    LoginInformation.loginInfo();

    // Questions that should be print in the screen  
    ProgramInterFace.interFace();


}

}

1 个答案:

答案 0 :(得分:0)

似乎您要在LoginInformation和ProgramInterFace中打开两个Scanner对象,并注册它们以从System.in中读取;据我所知,只有一台Scanner可以从System.in中读取。

要解决此问题,您可以在Main.java中初始化一个Scanner,然后将该Scanner对象传递到loginInfo和interFace(更新两个类中的方法签名并删除loginInfo中的close调用)。

package finalproject;

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        // Initialize a common Scanner
        final Scanner scan = new Scanner(System.in);

        // User Information
        LoginInformation.loginInfo(scan);

        // Questions that should be print in the screen
        ProgramInterFace.interFace(scan);
    }
}