简体   繁体   English

字符串仅包含2个整数-Java

[英]String only contains 2 integers - Java

Using a scanner, how would I be able to tell if the String I am scanning only contains 2 integers in Java? 使用扫描仪,我怎么能知道我正在扫描的字符串在Java中是否仅包含2个整数?

I have already using the hasNext() method, but I'm unsure what to use. 我已经使用过hasNext()方法,但是不确定使用什么。 I know I can use hasNextInt() , but my program requires that nothing else was in the String. 我知道我可以使用hasNextInt() ,但是我的程序要求字符串中没有其他内容。

I assume that "only contains two integers" means "contains two tokens, both of which are integers". 我假设“仅包含两个整数”的意思是“包含两个令牌,两个令牌都是整数”。 I would probably do this with a regular expression, but if you want to do it by scanning, something like this should do the work: 我可能会使用正则表达式来执行此操作,但是如果您想通过扫描来执行此操作,则应执行以下操作:

public boolean hasTwoIntegers(String s){
  Scanner sc = new Scanner(s);
  if (!scanner.hasNextInt()) return false;
  scanner.nextInt();
  if (!scanner.hasNextInt()) return false;
  if (scanner.hasNext()) return false;
  return true; 


}

Generalized to handle N ints: 可以处理N个整数:

public boolean hasNIntegers(String s, int n){
  Scanner sc = new Scanner(s);
  for (int i = 0; i <n; i++){
    if (!scanner.hasNextInt()) return false;
    scanner.nextInt();
  }
  if (scanner.hasNext()) return false;
  return true; 

}

You can just do it with a pattern: 您可以使用以下模式进行操作:

import java.util.regex.Pattern;

public class Patternizer {
    public static void main(String[] args){
        Pattern p=Pattern.compile("\\d+ \\d+");
        System.out.print("1000 10000".matches(p.pattern()));
        System.out.print("mmd 10000".matches(p.pattern()));
        System.out.print("1000.0 10000".matches(p.pattern()));
    }
}

hasNext has variation hasNext(regex) which can be applied to check if next token matches regex, but it is limited to only one token. hasNext具有hasNext(regex)变体,可用于检查下一个令牌是否与regex相匹配,但仅限于一个令牌。 You would have to use delimiter which could allow you to have tokens build from two worlds. 您将必须使用定界符,这可能允许您从两个世界构建令牌。 Such delimiter can look like this 这样的分隔符看起来像这样

sc.useDelimiter("(?<!\\G\\S+)\\s+");//delimiter representing "every second space"

(you can find explanation of this regex in Extracting pairs of words using String.split() ). (您可以在使用String.split()提取单词对中找到此正则表达式的说明)。

This will allow you to use hasNext("\\\\d+\\\\s+\\\\d+") which will check if next token is number, space, number. 这将允许您使用hasNext("\\\\d+\\\\s+\\\\d+")来检查下一个标记是否为数字,空格,数字。

After your test is complete you can set delimiter to standard one if you are interested in reading single word tokens later. 测试完成后,如果您希望以后读取单个单词标记,则可以将定界符设置为标准定界符。

Code example 代码示例

String data = "foo 12 32 bar";

Scanner sc = new Scanner(data);
Pattern delimiter = sc.delimiter();//default delimiter

System.out.println(sc.next());//foo
sc.useDelimiter("(?<!\\G\\S+)\\s+");//delimiter representing "every second space"

System.out.println(sc.hasNext("\\d+\\s+\\d+"));//TRUE

sc.useDelimiter(delimiter);//reset delimiter

System.out.println(sc.nextInt());//12
System.out.println(sc.nextInt());//32
System.out.println(sc.next());//bar

but my program requires that nothing else was in the String. 但是我的程序要求字符串中没有其他内容。

If you are interested in checking if entire string (not just some part of it) is two numbers than you can simply use 如果您有兴趣检查整个字符串(而不只是部分字符串)是否为两个数字,则可以简单地使用

yourString.matches("\\d+\\s+\\d+")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM