2

トップダウン設計を開発し、農場ごとに協同組合農場グループのグルメポップコーン生産の棒グラフを作成するプログラムを作成します。

  • プログラムへの入力は、1行に1つずつの一連のデータセットであり、各セットは1つのファームの生産を表します。
  • 各データセットは、ファームの名前で構成されています。その後に、コンマと1つ以上のスペース、植えられたエーカーを表す10進数、1つ以上のスペース、およびその農場で生産されたポップコーンのパイントジャーの数を表す整数が続きます。
  • 出力は、各農場を識別し、エーカーあたりのトウモロコシのパイントでその生産量を表示する棒グラフです。
  • 出力は、各ファームの1行で、ファームの名前は線の最初の列から始まり、棒グラフは列30から始まります。棒グラフの各マークは、1エーカーあたり25パイントのポップコーンの瓶を表します。
  • 年間の生産目標は、1エーカーあたり500瓶です。この目標を達成していない生産量の農場の場合は縦棒がグラフに表示され、1エーカーあたり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. ************ |

これは私がすでに実行して正しい形式で出力するコードですが、例外エラーがあります。文字列をファーム名に分割し、次に2エーカー、最後にintjarに分割しようとしています。入力ファイルは

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();
    }

}
4

1 に答える 1

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);

これは宿題のようですので、そのままにしておきます。

于 2012-10-06T20:36:57.950 に答える