2012-04-25 130 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雙重。