2012-10-06 22 views
2

開發自上而下的設計並編寫一個程序,以便在農場/農場的基礎上爲合作農場組製作美食爆米花生產條形圖。編程任務的麻煩農場生產

  • 輸入到程序是一系列數據集,每行一個,具有 每一組表示在產品用於一個場。
  • 每個數據集由一個農場的名稱組成。後面跟一個逗號和 一個或多個空格,一個十進制數字代表播種面積,一個 或更多空格,以及一個整數,表示爲該農場生產的爆米花的品脫罐數量 。
  • 輸出是一個條形圖,用於標識每個農場,並顯示其每英畝品種的玉米品種。
  • 輸出爲每個農場的單行,其中農場名稱 從行的第一列開始,條形圖從 列30開始。條形圖中的每個標記代表25品脫罐 爆米花每英畝。
  • 本年度的生產目標是每英畝500罐。生產的 豎條 應該出現在不符合此目標的生產中,並且特殊標記用於生產 大於或等於500英畝每英畝的農場。

例如,給定的輸入文件

Orville’s Acres, 114.8 43801 
Hoffman’s Hills, 77.2 36229 
Jiffy Quick Farm, 89.4 24812 
Jolly Good Plantation, 183.2 104570 
Organically Grown Inc., 45.5 14683 

輸出將是:

Popcorn Co-op 
Production in Hundreds 
of Pint Jars per Acre 
Farm Name 1 2 3 4 5 6 
---|---|---|---|---|---| 
Orville's Acres *************** | 
Hoffman's Hills ****************** | 
Jiffy Quick Farm *********** | 
Jolly Good Plantation *******************#** 
Organically Grown Inc. ************ | 

這是我的代碼已經運行和打印正確的格式,但它有一個異常錯誤。我正在嘗試將一個字符串分成農場名稱,然後是一個兩英畝,最後是一個int罐。輸入文件是

Orville’s Acres, 114.8 43801 
Hoffman’s Hills, 77.2 36229 
Jiffy Quick Farm,  89.4 24812 
Jolly Good Plantation, 183.2 104570 
Organically Grown Inc., 45.5  14683 

所有空格的原因是我希望它只是讀取數據行。

這裏是我的源代碼

import java.util.Scanner; 
import java.io.*; 

public class Popcorn 
{ 

public static void main(String args[])throws Exception 
{ 

    printHeading(); 

    System.out.print("Input file name you want to read from: "); 

    Scanner in = new Scanner(System.in);  

    String fileName = in.next(); //declares fileName a string that uses in as input 

    Scanner input = new Scanner(new FileReader(fileName)); //Declares input as the fileName and reads the file in 

    int jars; 
    double acres; 
    String amount; 
    String farmName; 
    System.out.println(); 
    System.out.println("\t\t\tPopcorn Co-op"); 
    System.out.println(); 
    System.out.println("\t\t\t\tProduction in Hundreds"); 
    System.out.println("\t\t\t\tof Pint Jars per Acre"); 
    System.out.println("Farm Name\t\t\t 1 2 3 4 5 6"); 
    System.out.println("\t\t\t\t---|---|---|---|---|---|"); 


    while (input.hasNextLine()) 
    { 


     String inputLine = input.nextLine();  

     if (inputLine.isEmpty()) 
     { 

     } 
     else 
     { 
      farmName = inputLine.substring(0,inputLine.indexOf(',')); 
      String inputLine2 = inputLine.substring(inputLine.indexOf(','),inputLine.length()); 
      Scanner line = new Scanner(inputLine2); 
      acres = line.nextDouble(); 
      jars = line.nextInt(); 
      System.out.println(farmName + jars + acres); 

     } 

    } 
} 

private static void printHeading(){ 


    System.out.println("Name"); //this assigns what printHeading will print 
    System.out.println("CSC 201-55468 Section 01PR"); 
    System.out.println("Fall 2012"); 
    System.out.println("Popcorn Project"); 
    System.out.println(); 
    } 

} 
+0

這是一個問題?你能添加異常嗎? –

回答

0
String inputLine2 = inputLine.substring(inputLine.indexOf(',') + 1); 

這跳過逗號,其原線沒有。

也許是:

line.skip("[ \t]+"); 

所需要跳過前面的空間。


用的indexOf + 1試圖代碼(不跳過)後:

... 
Orville’s Acres43801114.8 
Hoffman’s Hills3622977.2 
Jiffy Quick Farm2481289.4 
Jolly Good Plantation104570183.2 
Organically Grown Inc.1468345.5 

這似乎很好,考慮到:

  System.out.println(farmName + jars + acres); 

由於這似乎功課,我離開它。

+0

@JoopEggen ..從這個答案中可以理解什麼?你想把它放在哪裏?你應該至少解釋一下.. –

+0

你是對的@RohitJain,但是當輸入它時,我看到這個問題被大量編輯 - 看起來更多的信息。我的行代替了'String inputLine2 = ...'。 –

+0

不,我瞭解他 – Eons384