如果......不是int { [英] If ... is not an int {

查看:156
本文介绍了如果......不是int {的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让程序识别是否未输入int。

I am trying to get the program to recognize if an int is not entered .

我见过以下所有内容:

   if  (v % 1) 

    parseInt();

但它们不适合我。

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();
        }


推荐答案

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

Checking that a value is a primitive like that won't work. Java will not be able to compare a value to a type in such a way.

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

One method is to take advantage of the static function Integer.parseInt(String s) to see if an appropriate int value has been entered. Notice that it throws a NumberFormatException. If you can take advantage of this fact, you can obtain whether or not an integer was provided from a function.

try {
   //Attempt the parse here
} catch (...) {
   //Not a proper integer
}

第二种技术(因为你已经利用 Scanner 类)是使用扫描仪 方法 hasNextInt() nextInt()确定是否:

A second technique (since you're already taking advantage of the Scanner class) is to use the Scanner methods hasNextInt() and nextInt() to determine if:


  1. 扫描器流中有一个新整数

  2. 获取实际整数流的价值

  1. The Scanner has a new integer in the stream
  2. Get the actual integer value off of the stream

一个示例用法是:

if (myScanner.hasNextInt()) {
   int totalAmount = myScanner.nextInt();
   // do stuff here
} else {
   //Int not provided
}

正如您在更新中所提到的,当扫描仪的输入分隔时,这一切都很好。空间。默认情况下,Scanner按空格分隔流中的值。当你有一个不同的分隔符(例如:,或//等)在逻辑上在流上分隔两个唯一值时会发生什么?

As you mentioned in your update, this is all fine and good when input from the Scanner is delimited by spaces. By default, Scanner delimits values within the stream by spaces. What happens when you've got a different delimiter (ex: "," or "//" etc) that separates two unique values logically on the stream?

解决方案那就是修改 Scanner 正在使用的分隔符。有一种名为 useDelimiter(字符串模式) ,它允许您指定如何在流中逻辑分隔值。它对于你正在处理的事情(或任何空格不限定值的情况)非常有用。

The solution to that is to modify the delimiter that the Scanner is using. There is a method called useDelimiter(String pattern) which allows you to specify how values will be separated logically within the stream. It's very useful for cases like what you're dealing within (or any cases where spaces do not delimit the values).

用法看起来像这样:

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

在<$ c $中有一个很好的例子c>扫描仪 API(请参阅该示例中包含的扫描仪中的第一个链接),其中描述了如何使用它。我建议检查一下,它会很好地为你(我相信)聚集在一起。

There is a good example of this within the Scanner API (see the first link on Scanner I included for that example) that describes how this is used. I suggest checking that out, and it'll come together very nicely for you (I believe).

这篇关于如果......不是int {的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆