GVKun编程网logo

Java扫描仪双精度值-InputMismatchException(java扫描仪的作用)

6

此处将为大家介绍关于Java扫描仪双精度值-InputMismatchException的详细内容,并且为您解答有关java扫描仪的作用的相关问题,此外,我们还将为您介绍关于"xceptioninth

此处将为大家介绍关于Java扫描仪双精度值-InputMismatchException的详细内容,并且为您解答有关java扫描仪的作用的相关问题,此外,我们还将为您介绍关于"xception in thread "main" java.lang.NumberFormatException: For input string: "20、com.fasterxml.jackson.databind.exc.MismatchedInputException、InputMismatchException、InputMismatchException在我从cmd运行jar时发生,如果从想法运行则不会发生的有用信息。

本文目录一览:

Java扫描仪双精度值-InputMismatchException(java扫描仪的作用)

Java扫描仪双精度值-InputMismatchException(java扫描仪的作用)

我尝试以最简单的方式使用扫描仪:

码:

double gas, efficiency, distance, cost;Scanner scanner = new Scanner(System.in);System.out.print("Enter the number of gallons of gas in the tank: ");gas = scanner.nextDouble();System.out.print("Enter the fuel efficiency: ");efficiency = scanner.nextDouble();

但是在第一次输入后5.1会抛出:

Exception in thread "main" java.util.InputMismatchException    at java.util.Scanner.throwFor(Scanner.java:909)    at java.util.Scanner.next(Scanner.java:1530)    at java.util.Scanner.nextDouble(Scanner.java:2456)    at udacity.MileagePrinter.main(MileagePrinter.java:59)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:601)    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

该JavaDoc中的状态:

由扫描程序抛出,以指示检索到的令牌
与预期类型的模式不匹配,或者令牌超出预期类型的​​范围。

但是在我看来,所有外观都正确,并且应该可以正常运行。

问题:

  • 为什么在这种情况下会发生这种情况?
  • 如何避免这种麻烦?

答案1

小编典典

你应该为扫描仪精确设置区域设置。

Scanner scanner = new Scanner(System.in).useLocale(Locale.US);

从文档:

此类的实例能够扫描标准格式以及扫描仪的语言环境格式的数字。扫描程序的初始语言环境是Locale.getDefault()方法返回的值。可以通过useLocale(java.util.Locale)方法更改它

。本地化的格式是根据以下参数定义的,对于特定的语言环境,将从该语言环境的DecimalFormat对象df以及其和DecimalFormatSymbols对象dfs中获取。

因此,你的默认语言环境肯定使用了DecimalFormat,它期望逗号作为小数点分隔符而不是点。

"xception in thread "main" java.lang.NumberFormatException: For input string: "20

spark.SparkContext: Registered listener com.cloudera.spark.lineage.ClouderaNavigatorListener
"xception in thread "main" java.lang.NumberFormatException: For input string: "20
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.valueOf(Integer.java:582)
    at com.xiaodao.spark.examples.ml.AdaoKMeanService.main(AdaoKMeanService.java:166)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:730)
    at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:181)
    at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:206)
    at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:121)
    at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
17/10/12 17:42:02 INFO spark.SparkContext: Invoking stop() from shutdown hook
17/10/12 17:42:02 INFO ui.SparkUI: Stopped Spark web UI at http://192.168.1.30:4041
17/10/12 17:42:02 INFO spark.MapOutputTrackerMasterEndpoint: MapOutputTrackerMasterEndpoint stopped!
17/10/12 17:42:02 INFO storage.MemoryStore: MemoryStore cleared
 

com.fasterxml.jackson.databind.exc.MismatchedInputException

com.fasterxml.jackson.databind.exc.MismatchedInputException

jackson json 转实体 com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException

jackson 2.2.2

由于 vo 中缺少 json 的某个字段属性引起

3 种解决方法

1、 vo 中添加注解 @JsonIgnoreProperties (ignoreUnknown = true),在实体类上加上注解

2、  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

3、 添加如下即可:mapper.configure (DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);,把空的数组 “[]” 转为空

 

com.fasterxml.jackson.databind.exc.MismatchedInputException

错误的格式

[{
    "firstName": "hgf",
    "lastName": "frew",
    "username": "erf",
    "email": "bgghjk",
    "password": "bgte",
    "role": "trrere"
  }
]

  

正确的格式,不要 []

{
    "firstName": "hgf",
    "lastName": "frew",
    "username": "erf",
    "email": "bgghjk", "password": "bgte", "role": "trrere" }

 

 

InputMismatchException

InputMismatchException

我想问为什么我得到一个InputMismathException

我已经声明了一个类型的变量,double当我给它分配一个点值,例如(4.6)时,它抛出了我:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextDouble(Scanner.java:2456)
    at Exercises.ComputingMeanAndStandartDeviation_5_21.main(ComputingMeanAndStandartDeviation_5_21.java:18)

这是代码:

package Exercises;

import java.util.*;

public class ComputingMeanAndStandartDeviation_5_21 
{

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

        double sum = 0;
        double number = 1;
        double counter = 1;
        System.out.println("Enter ten numbers: ");
        while(counter<10)
        {
            number = input.nextDouble();
            sum +=number;
            counter ++;
        }

        System.out.println(sum + "   " + number + " " + counter);
        double mean = sum / counter;
        System.out.println("The mean is: " + mean);
    }

}

InputMismatchException在我从cmd运行jar时发生,如果从想法运行则不会发生

InputMismatchException在我从cmd运行jar时发生,如果从想法运行则不会发生

在我的主要方法,我有以下代码片段:

try { select = scanner.nextInt(); } catch (InputMismatchException e) { scanner.next(); //we should read erroneous System.out.println("Error. Please input number."); continue; }

我让mvn clezan安装 – >转到目标目录。 并开始与应用程序其实我input数字,但在控制台我看到以下消息:

D:freelanceНовая папкаmyrepositorytarget>java -jar palindrome-artifactId-1.0 -SNAPSHOT.jar Please type your name: u1 Please select menu item 1 - suggest word,2 - change user,3 - my score,4 - my word list,5 - records,6 - exit 1 Error. Please input number. Please select menu item 1 - suggest word,6 - exit

当我从理念(select主要方法)调用应用程序时,我没有看到问题。

请帮忙解决我的问题。

如何使用Windows API WriteFile将ASCIIstring写入文件?

如何find经常做io的过程?

如何在Windows中使用DeviceIoControl()读取各个扇区/群集?

Ruby文件写入窗口返回错误的文件大小?

如何在Windows中使用文件带宽保留(预定文件IO)

PS

全主要方法:

public static void main(String[] args) { System.out.println("Please type your name:"); try (Scanner scanner = new Scanner(system.in)) { scanner.useDelimiter("n"); String userName = scanner.next(); Game game = new Game(userName); AtomicInteger atomicInteger = new AtomicInteger(); int select = 0; do { System.out.println("Please select menu item"); System.out.println("1 - suggest word,6 - exit"); try { select = scanner.nextInt(); } catch (InputMismatchException e) { scanner.next(); //we should read erroneous System.out.println("Error. Please input number."); continue; } switch (select) { case 1: System.out.print("Word:"); String word = scanner.next(); if (game.suggestWord(word)) { System.out.println("Accepted: your score - " + game.getCurrentUserscore()); } else { System.out.println("Rejected: Word already exists in your list or it is not palindrome"); System.out.println("Your score - " + game.getCurrentUserscore()); } break; case 2: System.out.print("Name:"); String name = scanner.next(); game.changeUser(name); System.out.println("User changed successfully. Your score - " + game.getCurrentUserscore()); break; case 3: System.out.println("Your score - " + game.getCurrentUserscore()); break; case 4: System.out.println("Accepted words:"); game.getCurrentUserAcceptedWords().forEach(System.out::println); break; case 5: atomicInteger.set(1); game.getscores().forEach((k,v) -> System.out.println("#" + atomicInteger.getAndIncrement() + ". name: " + k + ",score: " + v)); break; case 6: System.out.println("Goodbye! thanks for the game"); break; default: System.out.println("You selected nonexistent menu item. Please try one more time."); } } while (select != 6); } }

衡量方法调用的I / O需求

删除一个目录,重新创build它,并添加文件

为什么read()会永远阻塞

运行在Windows上的Tomcat7

将WriteFile()是primefaces,如果进程被终止,但系统继续运行?

你的问题可能是你的分隔符。 它正在寻找一个n ,但在Windows上,每一行都以rn结尾。 所以你得到的用户名可能是"u1r"尽管你没有看到r (检查它的长度),而你输入的数字被读作"1r" ,这个数字不能被分析。

我猜你的IDE中的模拟控制台将你的按回车解释为n ,这就是为什么它在那里工作。

因此,将您的分隔符而不是n更改为r?n 。 分隔符是一个正则表达式,这将意味着“可选回车,后面是一个换行”。

今天关于Java扫描仪双精度值-InputMismatchExceptionjava扫描仪的作用的分享就到这里,希望大家有所收获,若想了解更多关于"xception in thread "main" java.lang.NumberFormatException: For input string: "20、com.fasterxml.jackson.databind.exc.MismatchedInputException、InputMismatchException、InputMismatchException在我从cmd运行jar时发生,如果从想法运行则不会发生等相关知识,可以在本站进行查询。

本文标签: