简体   繁体   English

线程“main”中的异常,这是什么意思?

[英]Exception in thread “main”, What does this mean?

I'm working on a homework problem for class. 我正在为课堂上的家庭作业问题做准备。 Where you have to calculate the distance between two points. 你必须计算两点之间的距离。 The code is basically done, but I have one question. 代码基本完成,但我有一个问题。 When I enter q to end the loop. 当我输入q结束循环。 I get a message back. 我收到一条消息。

Exception in thread "main" java.lang.NumberFormatException: For input string: "q" 线程“main”中的异常java.lang.NumberFormatException:对于输入字符串:“q”

at.sun.misc.FloatingDecimal.readJavaFormatString(Unkown Source) at.sun.misc.FloatingDecimal.readJavaFormatString(Unkown Source)

at java.lang.Double.parseDouble(Unkown Source) at java.lang.Double.parseDouble(Unkown Source)

atDistance.main(Distance.java:11) atDistance.main(Distance.java:11)

import java.util.Scanner;
public class Distance {
public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    while (true){
        System.out.print("Enter coordinate for x1: ");
        String x1String = input.next();
        if (x1String == "q")
            break;
            double x1 = Double.parseDouble(x1String);

        System.out.print("Enter coordinate for y1: ");
        String y1String = input.next();
        if (y1String == "q")
            break;
            double y1 = Double.parseDouble(y1String);

        System.out.print("Enter coordinate for x2: ");
        String x2String = input.next();
        if (x2String == "q")
            break;
            double x2 = Double.parseDouble(x2String);

        System.out.print("Enter coordinate for y2: ");
        String y2String = input.next();
        if (y2String == "q")
            break;
            double y2 = Double.parseDouble(y2String);

        double distance = (Math.pow(x2 - x1,2)) + (Math.pow(y2 - y1,2));
        distance = Math.sqrt(distance);
        System.out.printf("The distance is %5.2f",distance);
        System.out.println("");
    }
}//main
}//Distance

That is the code I have written. 那是我写的代码。 Any help is appreciated. 任何帮助表示赞赏。

It means 'q' is not a number. 这意味着'q'不是数字。 To compare strings you have to use equals , == just compares references. 要比较字符串,你必须使用equals==只是比较引用。

You don't do a string compare with ==. 你没有与==进行字符串比较。 The "==" comparison checks to see if they are the exact same objects, not if the strings contain the same characters. “==”比较检查它们是否是完全相同的对象,而不是字符串包含相同的字符。 Try x1String.equals("q") instead. 请尝试x1String.equals("q")

What's happening now is that the "==" will say "these aren't the same object" and then it will attempt to parse the "q" as a double in the next line, which is throwing the exception. 现在发生的是“==”会说“这些不是同一个对象”,然后它将尝试将“q”解析为下一行中的double,这就是抛出异常。

This old chestnut... 这老栗子......

You can not compare Strings (safely) using == . 无法使用==比较Strings(安全)。 Use .equals() instead: 使用.equals()代替:

if (x1String.equals("q")) // change every == to .equals()



Java != Javascript Java!= Javascript

x1String == "q" simply compares references. x1String == "q"只是比较引用。 In order to compare the input string with the string "q" , you need to use compareTo or equals : 为了将输入字符串与字符串"q"进行比较,您需要使用compareToequals

if (x1String.compareTo("q") == 0)
// or
if (x1String.equals("q"))

As others said, you should be using a function such as equals to compare the contents of the objects ( x1String and "q" ) rather than comparing the actual objects/references, but you should also be checking if the string is numeric and/or catch ing any exceptions that are thrown by parseDouble . 正如其他人所说,你应该使用诸如equals的函数来比较对象的内容( x1String"q" ),而不是比较实际的对象/引用,但你还应该检查字符串是否是数字和/或catch parseDouble抛出的任何异常。

What if someone inputs "asdfg" ? 如果有人输入"asdfg"怎么办?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在主线程上运行在android中意味着什么 - What does running on main thread mean in android 此错误代码是什么意思? 线程“ main”中的异常java.util.InputMismatchException - What does this error code mean? Exception in thread “main” java.util.InputMismatchException “线程“ main”中的异常“ java.lang.NoClassDefFoundError:javafx / embed / swing / JFXPanel”是什么意思,我该如何解决? - What does “Exception in thread ”main“ java.lang.NoClassDefFoundError: javafx/embed/swing/JFXPanel” mean and how do I fix it? 线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:索引 1 超出长度 0 的范围,不知道这是什么意思或如何解决 - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 0, no idea what does that mean or how to fix it 当main方法抛出异常时,它意味着什么? - What does it mean when the main method throws an exception? 这个例外是什么意思? - What does this exception mean? Thread Affinity是什么意思? - What does Thread Affinity mean? 成为守护程序线程意味着什么? - What does it mean to be a daemon thread? 什么“躲避异常”是什么意思? - What does “duck an exception” mean? Xtext:此异常是什么意思? - Xtext: What does this exception mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM