Java 如果。。。不是整数{

Java 如果。。。不是整数{,java,int,Java,Int,我试图让程序识别是否输入了int 我从以下方面看到了一切: if (v % 1) 到 但他们不是为我工作 import java.util.Scanner; public class LinearSlopeFinder { public static void main(String[]args){ double x1, y1, x2, y2, n1, equation, constant = 0 ; double slope, slope1

我试图让程序识别是否输入了int

我从以下方面看到了一切:

   if  (v % 1) 

但他们不是为我工作

import java.util.Scanner;

public class LinearSlopeFinder {
    public static void main(String[]args){
        double x1, y1, x2, y2, n1, equation, constant = 0 ;
        double slope, slope1, slopeAns;
        Scanner myScanner = new Scanner(System.in);

        System.out.print("    What is the first set of cordinants? example: x,y ... ");
        String coordinate1 = myScanner.nextLine();

        //below is what i am referring to 


        if (coordinate1 != int ){    // if it is a double or String 
            System.out.println("Sorry, you must use whole numbers.What is the first set of cordinants? example: x,y ... ");
            System.out.print("    What is the first set of cordinants? example: x,y ... ");
            String coordinate1 = myScanner.nextLine();
        }

您可以使用
Integer.parseInt(yourInt)
,但是如果您没有收到整数,则必须捕获抛出的错误,如下所示:

try {
  Integer.parseInt(yourInt);
}
catch (NumberFormatException e) {
  // do something that indicates to the user that an int was not given.
}
Scanner myScanner = ...; # Get this how you normally would
String delimiter = ...;  # Decide what the separator will be
myScanner.useDelimiter(delimiter); # Tell the Scanner to use this separator

您可以使用
Integer.parseInt(yourInt)
,但是如果您没有收到整数,则必须捕获抛出的错误,如下所示:

try {
  Integer.parseInt(yourInt);
}
catch (NumberFormatException e) {
  // do something that indicates to the user that an int was not given.
}
Scanner myScanner = ...; # Get this how you normally would
String delimiter = ...;  # Decide what the separator will be
myScanner.useDelimiter(delimiter); # Tell the Scanner to use this separator
不会工作,因为您正在将值与类型进行比较。此代码甚至不应编译

使用:

不会工作,因为您正在将值与类型进行比较。此代码甚至不应编译

使用:

使用此模型:

Integer result = null;
try {
    result = Integer.valueOf(input);
} catch (NumberFormatException e) {}
if (result != null) {
    // An integer was entered
}
Integer.valueOf将对带小数的数字引发异常,因此您不应该有任何问题。我使用Integer类vs int,以便将其设置为null。使用-1表示无法输入-1。

使用此模型:

Integer result = null;
try {
    result = Integer.valueOf(input);
} catch (NumberFormatException e) {}
if (result != null) {
    // An integer was entered
}

Integer.valueOf将对带小数的数字引发异常,因此您不应该有任何问题。我使用Integer类vs int,以便将其设置为null。使用-1意味着无法输入-1。

检查值是否是这样的基元将不起作用。Java将无法以这种方式将值与类型进行比较

一种方法是利用静态函数查看是否输入了适当的int值。请注意,它会抛出一个
NumberFormatException
。如果可以利用这一事实,则可以获得函数是否提供了整数

try {
   //Attempt the parse here
} catch (...) {
   //Not a proper integer
}
第二种技术(因为您已经利用了
扫描仪
类)是使用
扫描仪
hasnetint()
nextInt()
来确定:

  • 扫描仪
    在流中有一个新的整数
  • 从流中获取实际整数值
  • 一个示例用法是:

    if (myScanner.hasNextInt()) {
       int totalAmount = myScanner.nextInt();
       // do stuff here
    } else {
       //Int not provided
    }
    
    正如您在更新中提到的,当来自
    扫描仪的输入用空格分隔时,这一切都很好。默认情况下,扫描仪用空格分隔流中的值。当您使用不同的分隔符(例如:“,”或“/”等)在逻辑上分隔流中的两个唯一值时,会发生什么情况

    解决方法是修改
    扫描程序使用的分隔符。有一个方法称为。该方法允许您指定值在流中的逻辑分隔方式。这对于您正在处理的内容(或空格不分隔值的任何情况)非常有用

    用法如下所示:

    try {
      Integer.parseInt(yourInt);
    }
    catch (NumberFormatException e) {
      // do something that indicates to the user that an int was not given.
    }
    
    Scanner myScanner = ...; # Get this how you normally would
    String delimiter = ...;  # Decide what the separator will be
    myScanner.useDelimiter(delimiter); # Tell the Scanner to use this separator
    

    Scanner
    API中有一个很好的例子(请参阅我为该示例提供的
    Scanner
    API上的第一个链接),它描述了如何使用它。我建议您检查一下,它将非常适合您(我相信).

    检查一个值是否是这样的基元是行不通的。Java将无法以这种方式将一个值与一个类型进行比较

    一种方法是利用静态函数查看是否输入了适当的int值。请注意,它会抛出一个
    NumberFormatException
    。如果可以利用这一事实,则可以获得函数是否提供了整数

    try {
       //Attempt the parse here
    } catch (...) {
       //Not a proper integer
    }
    
    第二种技术(因为您已经利用了
    扫描仪
    类)是使用
    扫描仪
    hasnetint()
    nextInt()
    来确定:

  • 扫描仪
    在流中有一个新的整数
  • 从流中获取实际整数值
  • 一个示例用法是:

    if (myScanner.hasNextInt()) {
       int totalAmount = myScanner.nextInt();
       // do stuff here
    } else {
       //Int not provided
    }
    
    正如您在更新中提到的,当来自
    扫描仪的输入用空格分隔时,这一切都很好。默认情况下,扫描仪用空格分隔流中的值。当您使用不同的分隔符(例如:“,”或“/”等)在逻辑上分隔流中的两个唯一值时,会发生什么情况

    解决方法是修改
    扫描程序使用的分隔符。有一个方法称为。该方法允许您指定值在流中的逻辑分隔方式。这对于您正在处理的内容(或空格不分隔值的任何情况)非常有用

    用法如下所示:

    try {
      Integer.parseInt(yourInt);
    }
    catch (NumberFormatException e) {
      // do something that indicates to the user that an int was not given.
    }
    
    Scanner myScanner = ...; # Get this how you normally would
    String delimiter = ...;  # Decide what the separator will be
    myScanner.useDelimiter(delimiter); # Tell the Scanner to use this separator
    

    Scanner
    API中有一个很好的例子(请参阅我为该示例提供的
    Scanner
    API上的第一个链接),它描述了如何使用它。我建议您检查一下,它将非常适合您(我相信).

    还有另一个解决方案,因此您可以选择:在中,您可以读取整数可以是什么。将定义放在regexp形式中,您有:

    // input s
    if (s.matches("(?i)[+-]?(0|[1-9][0-9]*|0[x][0-9a-f]+|0[0-7]+)L?")) { /* do something */}
    
    缺点是,您可以在代码中添加一个详细的数字定义,Java可以帮您做到这一点。不过,好处是
    2,3
    的情况很容易解决:

    String intLiteral = "[+-]?(0|[1-9][0-9]*|0[x][0-9a-f]+|0[0-7]+)L?";
    // input s
    if (s.matches("(?i)" + intLiteral + "," + intLiteral)) { /* do something */}
    
    (事情不会那么简单,因为您可能也希望像
    2、3
    这样的字符串有效,但我将把它留给您。)

    回到常用的解决方案和您对
    2,3
    的评论,您必须养成分离问题的习惯:您已经知道几种检查字符串是否包含整数的方法。现在您可以做的是不处理此问题,并解决逗号问题:

        String s = "2,3";
        String[] parts = s.split(","); // here we deal with commas
    
        try {
            // now when we have parts, deal with parts separately
            System.out.println("First:  " + Integer.parseInt(parts[0]));
            System.out.println("Second: " + Integer.parseInt(parts[1]));
        } catch (NumberFormatException e) {
            System.out.println("There was a problem, you know: " + e.getMessage());
        }
    

    还有另一个解决方案,因此您可以选择:在中,您可以读取整数可以是什么

    // input s
    if (s.matches("(?i)[+-]?(0|[1-9][0-9]*|0[x][0-9a-f]+|0[0-7]+)L?")) { /* do something */}
    
    缺点是,您可以在代码中添加一个详细的数字定义,Java可以帮您做到这一点。不过,好处是
    2,3
    的情况很容易解决:

    String intLiteral = "[+-]?(0|[1-9][0-9]*|0[x][0-9a-f]+|0[0-7]+)L?";
    // input s
    if (s.matches("(?i)" + intLiteral + "," + intLiteral)) { /* do something */}
    
    (事情不会那么简单,因为您可能也希望像
    2、3
    这样的字符串有效,但我将把它留给您。)

    返回常用的解决方案和您的同事