2016-04-11 151 views
0

Java程序:Java:解析輸入,用逗號分隔

下面的輸入只是我的頭頂。我正在尋找的是如何根據它們是字符串還是基本類型來獲取輸入數據的特定部分...在這裏我認爲使用以某種方式將它們分開的逗號可能會很有用。

/* Given input: 
    * 
    * Massachusetts, Ma, Boston, 10000, 20000 
    * California, CA, Los Angeles, 30000, 40000 

    * How would you print: 

    * Massachusetts 20000 
    * California  40000 
    */ 

public static void main(String[] args) { 
    Scanner userInput = new Scanner(System.in) ; 

    while (userInput.hasNextLine()) { 
     String state = userInput.nextLine() ; 
     Double population = userInput.nextDouble() ; 
     String temp = userInput.nextLine() ; // for the new line 

     System.out.printf("%s %2f" , state , population) ; 
    } 
} 

我也見過一個叫useDelimiter()方法,我在想,如果這可能有助於從我的掃描儀分手的輸入。

+0

如果你問代碼的問題,它總是有幫助的添加標籤爲您所使用的語言。您可能想要[編輯]這樣做。 –

+1

只需閱讀整行內容並將其拆分爲「,」。然後你可以輕鬆獲得你的價值觀。 – ManoDestra

+0

只是在ManoDestras的評論頭上。如果您希望數據正確(例如,第一個數據是城市名稱,第二個數據是狀態數據,第三個數據總是相同等等),那麼使用'String.split()'...... – Bonatti

回答

0

鑑於串Massachusetts, Ma, Boston, 10000, 20000,您可以使用split得到陣列[Massachusetts, Ma, Boston, 10000, 20000](注意,這是不是一個字符串了)

String input = "Massachusetts, Ma, Boston, 10000, 20000"; 
String[] fields = input.split(", ?"); 
String state = fields[0]; 
String xx = fields[4]; 
System.out.println(state+", "+xx); // Massachusetts, 20000 

注意,正則表達式, ?意味着一個逗號,然後進行或不進行空間。此外,因爲這將是用戶輸入,你應該檢查是否fields陣列具有所需的長度(在這種情況下,因爲您正在訪問第四個字段,長度應該至少爲5)

我猜你可以輕鬆調整你的代碼;)

0

一個解決方案是使用正則表達式。

Pattern p = Pattern.compile("([A-z]+), ([A-z]{2}), ([A-z]+), (\\d+), (\\d+)"); 
String line = userInput.nextLine(); 
Matcher lineMatcher = p.matcher(line); 
if (lineMatcher.matches()) { 
    System.out.println(lineMatcher.group(0)); //prints Massachusetts 
    System.out.println(lineMatcher.group(1)); //prints Ma 
    System.out.println(lineMatcher.group(2)); //prints Boston 
    System.out.println(lineMatcher.group(3)); //prints 10000 
    System.out.println(lineMatcher.group(4)); //prints 20000 
} 
0

如果我正確地理解你的問題,你的輸入是String類型的,你可以用這個方法:

公衆的String []分裂(字符串正則表達式),你的正則表達式是「」。

它會返回給你一個String []。您可以使用索引單獨打印每個值,並使用Float.parseFloat(String value)將字符串轉換爲浮點類型。

String userInput = "Massachusetts, Ma, Boston, 10000, 20000"; 

    userInput = userInput.replaceAll("\\s+", ""); //replaces whitespace 
    String[] values = userInput.split(","); 

    System.out.println(values[0]); 
    System.out.println(values[4]); 

    String state = values[0]; 

    // you probably do not want a float for population 
    float population = Float.parseFloat(values[4]); 
    System.out.printf("%s %2f" , state , population); 

    // you probably want an integer, so try this 
    int integerPopulation = Integer.parseInt(values[4]); 
    System.out.printf("%s %2d" , state , integerPopulation); 
0

給你的代碼,你可以做以下變化:

String[] temp = new String[5]; 
while (userInput.hasNextLine()) { 
    String input = userInput.nextLine(); 
    temp[] = input.split(","); 
    System.out.printf("%s %s" ,temp[0] , temp[4]) ; 
} 

當你調用nextLine(),你可以得到該輸入的整條生產線。從逗號分割()它並將其存儲到一個臨時數組中。然後根據您的要求打印這些值。如果需要進行任何計算,則可以將數值類型從temp數組中轉換爲該值。

您可以將字符串值轉換爲使用該行浮動:

Float.valueOf(temp[4]);