本文介绍了从键盘获取字符串中的java.util.InputMismatchException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package info.code;

import java.util.Scanner;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class GetDataFromKeyBoardAndStoreInDB {

public static void main(String[] args) {

  Scanner s = new Scanner(System.in);

  Product p = new Product();
  p.setPid(s.nextInt());
  p.setName(s.next());
  p.setPrice(s.nextDouble());

  Configuration cfg = new Configuration(); 
  cfg.configure();
  SessionFactory sf = cfg.buildSessionFactory();
  Session hsession = sf.openSession();
  hsession.beginTransaction();

  hsession.save(p);

  hsession.getTransaction().commit();
  hsession.close();
  sf.close();
}

}



INPUT



INPUT

115
Hello World
Exception in thread "main" java.util.InputMismatchException
        at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextDouble(Scanner.java:2387)
    at info.code.GetDataFromKeyBoardAndStoreInDB.main(GetDataFromKeyBoardAndStoreInDB.java:18)

- 上面的代码当提供HelloWorld作为输入时工作正常,但是当提供Hello World
时没有。此方法是否不接受带空格的String。

-- The above code is working fine when supplied HelloWorld as input but not when supplied Hello World. Is this method doesn't accept String with space.

推荐答案

代码



Code

  Scanner s = new Scanner(System.in);

  Product p = new Product();
  p.setPid(s.nextInt());
  p.setName(s.next().concat(s.nextLine()));
  p.setPrice(s.nextDouble());



输入



333

Input

333

Hello World Am Fine //这里我们可以有任意数量的String标记(但不应超过DB中声明的限制)

Hello World Am Fine // We can have any number of tokens of String here ( But should not cross the limit declared in DB )

6000

log4j:WARN找不到logger(org.hibernate.cfg.Environment)的appender。
log4j:WARN请正确初始化log4j系统。

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).log4j:WARN Please initialize the log4j system properly.

Hibernate:插入HIB.PRODUCT(NAME,PRICE,PID)值(?,?,? )

Hibernate: insert into HIB.PRODUCT (NAME, PRICE, PID) values (?, ?, ?)

PID名称价格

333 Hello World Am Fine 6000

333 Hello World Am Fine 6000

如果我们的代码是

  p.setPid(s.nextInt());
  p.setName(s.nextLine());
  p.setPrice(s.nextDouble());

然后,如果我们提供'Hello World'作为参数输入到setName(); ,它只接受'World'作为输入,因为扫描程序超过当前行并返回跳过的输入。此方法(readLine())返回当前行的其余部分,不包括末尾的任何行分隔符。

then , if we supply ' Hello World ' as input for parameter to setName(); , it only take 'World' as input becasue scanner past the current line and returns the input that was skipped. This method (readLine()) returns the rest of the current line, excluding any line separator at the end.

因此对于相同的输入(如上所示)输出将

Hence for the same input ( as given above ) the output will be

PID名称价格

333 World 6000

333 World 6000

如果我们的代码是

          p.setPid(s.nextInt());
          p.setName(s.next().concat(s.nextLine());
          p.setPrice(s.nextDouble());

然后我们得到预期的输出。首先我们使用s.next();方法得到String,然后我们连接跳过的字符串(跳过s。 next());通过使用nextLine()读取跳过的String,对我们的旧String使用concat()方法;方法

then we get the expected output. As first we get the String using s.next(); method, then we concat the skipped String ( skipped by s.next() ); using concat() method to our old String by reading that skipped String using nextLine(); method

对于readLine();方法

For readLine(); method

扫描程序经过当前行并返回跳过的输入。此方法返回当前行的其余部分,排除最后的任何行分隔符。

Scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end.

这篇关于从键盘获取字符串中的java.util.InputMismatchException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 13:04