2016-10-08 46 views
1

我的任务是编写一个程序来检查输入字符串是否是有效数字。一个有效的数字是任何具有或不具有+或 - 符号和一个小数的数字,如:(+1 -1 1.0 1.0000000000 -1.0 -1.000000000)但是当我放入如下东西时:(1.2.3)它有效。我在做什么错如何让我的允许输入更严格

import java.util.Scanner; 

public class parseNum { 

    public static void main(String[] args) { 
    System.out.println("Please enter a number:"); 
    Scanner input = new Scanner(System.in); 
    String inStr = input.nextLine(); 
    int i; 
    String state = "start"; 

    for (i=0; i<inStr.length(); i++) { 
     if (state.equals("start")) { 
     if (inStr.charAt(i) == '+' || inStr.charAt(i) == '-') { 
      state = "afterSign"; 
      continue; 
     } 
     } 
     // i assume this is where i messed up 
     if(inStr.charAt(i) == ('.')){ 
     if(inStr.charAt(i+1) >= '0' && inStr.charAt(i+1) <= '9'){ 
     state = "accept"; 
     continue; 
     } 
     else{ 
     state = "reject"; 
     break; 
     } 
     } 

     if (inStr.charAt(i) >= '0' && inStr.charAt(i) <= '9') { 
     state = "accept"; 
     continue; 
     } 
     else { 
     state = "reject"; 
     break; 
     } 
    } 

    if (state.equals("accept")) 
     System.out.println("Thank you"); 
    else 
     System.out.println("Invalid input"); 

    input.close(); 
    } 

} 

回答

0

你上面的for循环声明一个int计数点的数量,

int dotCount = 0; 

未来,统计每一个点被发现

if(inStr.charAt(i) == ('.')){ 
      dotCount++; //THIS LINE 
      if(inStr.charAt(i+1) >= '0' && inStr.charAt(i+1) <= '9'){ 
       state = "accept"; 
       continue; 
      } 
      else{ 
       state = "reject"; 
       break; 
      } 
     } 

最后时间,添加if语句,如果它发现多于1则拒绝。

if(dotCount > 1) { 
      state = "reject"; 
      break; 
     } 
+0

YES !!!那工作非常感谢 –

+1

见http://stackoverflow.com/help/someone-answers – c0der

0

看评论:

import java.util.Scanner; 

public class ParseNum {//use java naming conventions 

    public static void main(String[] args) { 

     System.out.println("Please enter a number:"); 
     Scanner input = new Scanner(System.in); 
     String inStr = input.nextLine(); 
     int i =0; 
     String state = "start"; 
     int dotCount = 0; 

     //check for +/- outside the loop. It should only be 
     //at the beginning. This will cause number with multiple 
     //+/- signs to fail 
     if (inStr.charAt(0) == '+' || inStr.charAt(0) == '-') { 
      state = "afterSign"; 
      i=1; 
     } 

     for (i=0; i<inStr.length(); i++) { 

      if(inStr.charAt(i) == ('.')){ 
       if(dotCount == 0){ 
        state = "accept"; 
        dotCount++; 
        continue; 
       } 
       else{//multiple dots fond 
        state = "reject"; 
        break; 
       } 
      } 

      if (inStr.charAt(i) >= '0' && inStr.charAt(i) <= '9') { 
       state = "accept"; 
       continue; 
      } 
      else { 
       state = "reject"; 
       break; 
      } 
     } 

     if (state.equals("accept")) 
      System.out.println("Thank you"); 
     else 
      System.out.println("Invalid input"); 

     input.close(); 
    } 
} 

注意,多显示为0的数字,如000009.8被视为有效。
一个更简单和更好的实现将是:

public static void main(String[] args) { 

     System.out.println("Please enter a number:"); 
     Scanner input = new Scanner(System.in); 
     String inStr = input.nextLine(); 
     String state = "start"; 

     try { 

      Double d = Double.valueOf(inStr); 
      state = "accept"; 
     } catch (NumberFormatException ex) { 

      state = "reject"; 
     } 

     if (state.equals("accept")) { 
      System.out.println("Thank you"); 
     } else { 
      System.out.println("Invalid input"); 
     } 

     input.close(); 
    } 
相关问题