简体   繁体   English

如何检查 integer 字符串

[英]How to check for integer string

I'm supposed to write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9.我应该编写一个程序,将代表 integer 的字符串作为输入,如果每个字符都是数字 0-9,则输出 yes。

I have gone back through my chapters reading and gone through Google, but I'm still having trouble.我已经回顾了我的章节阅读并浏览了谷歌,但我仍然遇到了麻烦。 I know my code is a mess but I am lost.我知道我的代码一团糟,但我迷路了。 I may have bits and pieces correct or be wrong all together but this is what I have.我可能有正确或错误的点点滴滴,但这就是我所拥有的。

import java.util.Scanner; 

public class LabProgram {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      String userString;
  // Add more variables as needed

      userString = scnr.next();
      boolean check1 = Character.isDigit(userString);
  
      while (check1 = True) {
         System.out.println ("Yes");
    }
      System.out.println ("No");

Where did I go wrong?我在哪里 go 错了?

As pointed out by people in comments, there are some mistakes in your code.正如人们在评论中指出的那样,您的代码中有一些错误。 Character.isDigit won't work with String. Character.isDigit不适用于字符串。 What you need to use is Integer.parseInt(...) .您需要使用的是Integer.parseInt(...) You can put it in a try catch block;你可以把它放在一个 try catch 块中; Print yes if parsing is successful and print No if exception is thrown:如果解析成功则打印 yes,如果抛出异常则打印 No:

userString = scnr.next();
try {
  int parsedValue = Integer.parseInt(userString);
  // Do something with parsedValue
  System.out.println("Yes");
} catch (NumberFormatException numberFormatException) {
  System.out.println("No");
}

You could use regular expressions for that:您可以为此使用正则表达式:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

// your input, can also be from a scanner
String input = "1234567890";

// regex pattern to match a string that consists only of numbers and is also not empty
String regex = "^[0-9]+$";

// to match negative numbers as well: "^[\\-]?[0-9]+$"

// create a pattern object
Pattern pattern = Pattern.compile(regex);

// create a matcher object
Matcher matcher = pattern.matcher(input);

// check if the input matches
if (matcher.find()) {
  System.out.println("It's a number, yay!");
};

How does the regex work:正则表达式如何工作:

  • ^ matches the beginning of the input to make sure there is nothing before the number characters ^匹配输入的开头以确保在数字字符之前没有任何内容
  • [0-9]+ tells it to match the range 0-9 one or more times ( + ), which also makes sure that an empty string does not match [0-9]+告诉它匹配范围0-9一次或多次( + ),这也确保空字符串不匹配
  • $ matches the end of the string to make sure there is no non-numbers after the match $匹配字符串的结尾以确保匹配后没有非数字

If you want to match negative numbers as well, there is an additional part of the regex: [\\-]?如果您还想匹配负数,正则表达式还有一个附加部分: [\\-]? this matches one minus sign if there is on or none if there is no sign ( ? )如果有 on 则匹配一个减号,如果没有符号则匹配一个负号 ( ? )

https://www.tutorialspoint.com/java/java_regular_expressions.htm https://www.tutorialspoint.com/java/java_regular_expressions.htm

It's also possible the way you tried it:您尝试的方式也可能:

// open a scanner
Scanner scanner = new Scanner(System.in);

// read user input from the scanner
String input = scanner.next();

// assume it is a number until we find a non-digit character below
String isNumber = true;

// loop over each character of the user input
for (char character : input) {
  // check if the character is not a digit
  if (!Character.isDigit(character)) {
    System.out.println("NOT a number: ", character);

    // set the is number variable to false
    isNumber = false;

    // break the loop, because it cannot be a number because we already found a non-digit
    break;
  };
};

System.out.println("Is it a number? ", isNumber);

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

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