Java 使用Scanner对象读取一系列输入

Java 使用Scanner对象读取一系列输入,java,java.util.scanner,Java,Java.util.scanner,我必须检索三个输入:整数、字符串和双精度;下面是我的想法[注意:包含空格的字符串] 我试图替换中的input.nextLine() 使用input.next(),因为InputMisMatchException,但问题仍未解决。抛出错误是因为可能为新行值指定了一个双精度值[我认为这是不确定的]是否有方法检索包含空格的字符串,并能够同时完成必须输入的三个输入。谢谢 注意:我找不到与此相关的任何问题 让我添加发生错误的整个方法 public void addAccountRecords(){

我必须检索三个输入:整数、字符串和双精度;下面是我的想法[注意:包含空格的字符串]

我试图替换中的input.nextLine()

使用input.next(),因为InputMisMatchException,但问题仍未解决。抛出错误是因为可能为新行值指定了一个双精度值[我认为这是不确定的]是否有方法检索包含空格的字符串,并能够同时完成必须输入的三个输入。谢谢

注意:我找不到与此相关的任何问题 让我添加发生错误的整个方法

public void addAccountRecords(){
    AccountRecord record = new AccountRecord();
    input = new Scanner (System.in);

    try {
        output = new Formatter("oldmast.txt");
    } catch (FileNotFoundException e) {
        System.err.println("Error creating or opening the file");
        e.printStackTrace();
        System.exit(1);
    } catch (SecurityException e){
        System.err.println("No write access to this file");
        e.printStackTrace();
        System.exit(1);
    }


    System.out.println("Enter respectively an account number\n"+
            "a name of owner\n"+"the balance");

        while ( input.hasNext()){
            try{
                record.setAccountNumber(input.nextInt());
                record.setName(input.nextLine());
                record.setBalance(input.nextDouble());

                if (record.getBalance() >0){
                    output.format("%d\t%s\t%,.2f%n",record.getAccountNumber(),
                            record.getName(),record.getBalance());
                    record.setCount((record.getCount()+1));
                }else
                    System.err.println("The balance should be greater than zero");

            }catch (NoSuchElementException e){
                System.err.println("Invalid input, please try again");
                e.printStackTrace();
                input.nextLine();

            }catch (FormatterClosedException e){
                System.err.println("Error writing to File");
                e.printStackTrace();
                return;
            }
            System.out.println("Enter respectively an account number\n"+
                    "a name of owner\n"+"the balance\n or End of file marker <ctrl> z");
        }//end while
        output.close();
        input.close();

}//end AddAccountRecords
public void addAccountRecords(){
AccountRecord=新的AccountRecord();
输入=新扫描仪(System.in);
试一试{
输出=新格式化程序(“oldmast.txt”);
}catch(filenotfounde异常){
System.err.println(“创建或打开文件时出错”);
e、 printStackTrace();
系统出口(1);
}捕获(安全异常e){
System.err.println(“没有对此文件的写入权限”);
e、 printStackTrace();
系统出口(1);
}
System.out.println(“分别输入帐号\n”+
“所有者名称\n”+“余额”);
while(input.hasNext()){
试一试{
record.setAccountNumber(input.nextInt());
record.setName(input.nextLine());
record.setBalance(input.nextDouble());
if(record.getBalance()>0){
output.format(“%d\t%s\t%,.2f%n”,record.getAccountNumber(),
record.getName(),record.getBalance());
record.setCount((record.getCount()+1));
}否则
System.err.println(“余额应大于零”);
}捕获(无接触元素例外e){
System.err.println(“输入无效,请重试”);
e、 printStackTrace();
input.nextLine();
}捕获(FormatterClosedException e){
System.err.println(“写入文件时出错”);
e、 printStackTrace();
返回;
}
System.out.println(“分别输入帐号\n”+
“所有者名称”+“余额\n或文件结尾标记z”);
}//结束时
output.close();
input.close();
}//结束AddAccountRecords

nextLine
将读取字符串末尾的所有剩余数据,包括双精度字符。您需要改用
next
。我不确定为什么会出现
输入不匹配异常
——例如:

String s = "123 asd 123.2";
Scanner input = new Scanner(s);
System.out.println(input.nextInt());    //123
System.out.println(input.next());       //asd
System.out.println(input.nextDouble()); //123.2
因此,问题可能存在于您的输入或代码中的其他地方

注:

  • 如果我使用
    扫描仪输入=新扫描仪(System.in)
    并输入
    123 asd 123.2
    I得到相同的结果
  • 如果字符串(第二个条目)包含空格,则第二个单词将被解析为double并生成错误报告

是的,当然可以,但不要使用asd,而是尝试使用[as d]和空格,我不确定它是否可以使用。@RuruMorlano这是我的第二个注意事项。如果在中间有一个空格,则应该分别询问INT、String和String(即在单独的行上)。
public void addAccountRecords(){
    AccountRecord record = new AccountRecord();
    input = new Scanner (System.in);

    try {
        output = new Formatter("oldmast.txt");
    } catch (FileNotFoundException e) {
        System.err.println("Error creating or opening the file");
        e.printStackTrace();
        System.exit(1);
    } catch (SecurityException e){
        System.err.println("No write access to this file");
        e.printStackTrace();
        System.exit(1);
    }


    System.out.println("Enter respectively an account number\n"+
            "a name of owner\n"+"the balance");

        while ( input.hasNext()){
            try{
                record.setAccountNumber(input.nextInt());
                record.setName(input.nextLine());
                record.setBalance(input.nextDouble());

                if (record.getBalance() >0){
                    output.format("%d\t%s\t%,.2f%n",record.getAccountNumber(),
                            record.getName(),record.getBalance());
                    record.setCount((record.getCount()+1));
                }else
                    System.err.println("The balance should be greater than zero");

            }catch (NoSuchElementException e){
                System.err.println("Invalid input, please try again");
                e.printStackTrace();
                input.nextLine();

            }catch (FormatterClosedException e){
                System.err.println("Error writing to File");
                e.printStackTrace();
                return;
            }
            System.out.println("Enter respectively an account number\n"+
                    "a name of owner\n"+"the balance\n or End of file marker <ctrl> z");
        }//end while
        output.close();
        input.close();

}//end AddAccountRecords
String s = "123 asd 123.2";
Scanner input = new Scanner(s);
System.out.println(input.nextInt());    //123
System.out.println(input.next());       //asd
System.out.println(input.nextDouble()); //123.2