2012-04-25 149 views
1

我的任务:java输入验证:如何在空间拆分用户输入?

通过确保用户输入的脚正整数,对于英寸的非负十进制数,重量为一个正十进制数执行输入验证。详细信息请参阅示例会话。尤其要注意回波打印的高度和重量值以及生成的体重指数值的格式。

我的程序有效,但我无法弄清楚如何使它看起来像下面的示例程序。我必须让用户输入以英尺为单位的高度,然后再以英寸为单位输入高度。我不知道如何在这个空间分割用户输入。

样品会话:

Enter height using feet space inches (e.g., 5 6.25): hi there 
Invalid feet value. Must be an integer. 
Invalid inches value. Must be a decimal number. 
Re-enter height using feet space inches (e.g., 5 6.25): 0 9 
Invalid feet value. Must be positive. 
Re-enter height using feet space inches (e.g., 5 6.25): 5.25 0 
Invalid feet value. Must be an integer. 
Re-enter height using feet space inches (e.g., 5 6.25): 5 9.25 
Enter weight in pounds: 0 
Invalid pounds value. Must be positive. 
Re-enter weight in pounds: 150.5 
height = 5'-9.25" 
weight = 150.5 pounds 
body mass index = 22.1 

到目前为止我的代码:

package ch14; 
import java.util.Scanner; 

public class BMI { 


public static void main(String[] args) { 

    String inputString; 
    double bmi; 
    double pounds; 
    double inches; 
    String feet; 

    Scanner stdIn = new Scanner(System.in); 

    System.out.print("Please enter height using feet space inches (e.g., 5 6.25): "); 
    inputString = stdIn.nextLine(); 


    try 
    { 
     Double.parseDouble(inputString); 
    } 
    catch(NumberFormatException nfe) 
    { 
     System.out.println ("Invalid inch value. Number must be a decimal."); 
     System.out.print ("Re-enter height in inches: "); 
     inputString = stdIn.nextLine(); 
    } 

    inches=Double.parseDouble(inputString); 

    if(inches<=0) 
    { 
     System.out.println("Invalid inch value. Must be a positive number."); 
     System.out.print("Re-enter height in inches:"); 
     inputString = stdIn.nextLine(); 
     inches=Double.parseDouble(inputString); 
    } 

    System.out.print("enter weight(in pounds) "); 
    inputString = stdIn.nextLine(); 

    try 
    { 
     Double.parseDouble(inputString); 
    } 
    catch(NumberFormatException nfe) 
    { 
     System.out.println("Invalid pound value. Number must be a decimal."); 
     System.out.print("Re-enter the weight in pounds: "); 
     inputString = stdIn.nextLine(); 
    } 

    pounds=Double.parseDouble(inputString); 

    if(pounds <= 0) 
    { 
     System.out.println("Invalid pound value. Must be a positive number."); 
     System.out.print("Re-enter the weight in pounds:"); 
     inputString = stdIn.nextLine(); 
     pounds=Double.parseDouble(inputString); 
    } 

    System.out.println("Height = " + inches + "\""); 
    System.out.println("Weight = " + pounds + " pounds"); 
    System.out.printf("Body Mass Index = %.1f\n",(pounds * 703.)/(inches * inches)); 

    }//end main 
} 

回答

2

拆分使用.split所以,对于一个单一的空间...

mystring.split("\\s"); 

为任意数量的空格...

mystring.split("\\s+"); 

这两个操作都返回,然后可以像任何其他数组例如访问字符串数组...

String mystring = "Hello World"; 
String[] words = mystring.split("\\s"); 
System.out.println(words[0]); //<--- Would print "Hello" 

为了您的具体问题,你可能会想这些字符串对象转换到Double's之类的东西。你可以使用Double.parseDouble()来做到这一点。例如,

Double number = Double.parseDouble("1.0"); 

的代码这以上线上的"1.0"转换字符串对象到的值1.0双重。