1

Develop a top-down design and write a program to produce a bar chart of gourmet-popcorn production for a cooperative farm group on a farm-by-farm basis.

The input to the program is a series of data sets, one per line, with each set representing the production for one farm.

Each data set consists of the name of a farm. Followed by a comma and one or more spaces, a decimal number representing acres planted, one or more spaces, and an integer representing the number of pint jars of popcorn produced for that farm.

The output is a bar chart that identifies each farm and displays its production in pints of corn per acre.

The output is a single line for each farm, with the name of the farm starting in the first column on a line and the bar chart starting in column 30. Each mark in the bar chart represents 25 pint jars of popcorn per acre.

The production goal for the year is 500 jars per acre. A vertical bar should appear in the chart for farms with production that does not meet this goal, and a special mark is used for farms with production greater than or equal to 500 jars per acre.

For example, given the input file (Popcorn.dat)

Oriville's Acres* 114.8 438010.0 
Hoffman's Hills* 77.2 362290.0 
Jiffy Quick Farm* 89.4 248120.0 
Jolly Good Plantation* 183.2 1045700.0 
Organically Grown Inc.* 45.5 146830.0 

the output would be:

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. ************ |

Here is my code:

import java.io.File;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.FileNotFoundException;

public class barchart {
    public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       System.out.print("Enter the name of the file: ");
       String filename = in.next();
       File file;
       String line;
       StringTokenizer tokens;
       String FarmName;
       String values;
       double acres_planted;
       int production;
       double prod_per_acre;
       double aux;
       int marks;
       try{
           //to avoid the FileNotFoundException, it is surrounded this part with try and catch, to handle the exception.
           file = new File(filename);
           in= new Scanner(file);
       }catch(FileNotFoundException e){
           //in case of error, a message is showed, and the program exits
           System.out.println("Error loading the file: "+filename+". The program will exit");
           return;
       }
       System.out.println("Popcorn Co-op Production in Hundreds of Pint Jars per Acre:");
       System.out.println("Farm Name 1 2 3 4 5 6 ---|---|---|---|---|---|");
       while(in.hasNextLine()){
           line=in.nextLine();
           tokens=new StringTokenizer(line,",");
           FarmName=tokens.nextToken();
           values=tokens.nextToken();
           tokens=new StringTokenizer(values," ");
           acres_planted=Double.parseDouble(tokens.nextToken());
           production=Integer.parseInt(tokens.nextToken());
           prod_per_acre=production/acres_planted;
           aux=prod_per_acre/25;
           marks=(int) aux;
           System.out.print(FarmName);
           for(int i=FarmName.length();i<30;i++){
               System.out.print(" ");
           }
           for(int i=0;i<marks;i++){
               if(i==15 && prod_per_acre>=400 ){
                   System.out.print("#*");
               }else{
                   System.out.print("*");

               }
           }
           if(prod_per_acre<400){
               System.out.print("|");              
           }
           System.out.println("");              

       }



    }
}

When I run the program, it outputs an exception error:

Enter the name of the file: Popcorn.dat
Popcorn Co-op Production in Hundreds of Pint Jars per Acre:
Farm Name 1 2 3 4 5 6 ---|---|---|---|---|---|
Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(Unknown Source)
    at barchart.main(barchart.java:36)

Could someone please help me out by fixing the error so the program runs? I have tried changing the data file:


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

It still outputs a different error:

Enter the name of the file: Popcorn.dat
Popcorn Co-op Production in Hundreds of Pint Jars per Acre:
Farm Name 1 2 3 4 5 6 ---|---|---|---|---|---|
Exception in thread "main" java.lang.NumberFormatException: For input string: "438010.0"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at barchart.main(barchart.java:39)

Help would be appreciated. Thank you!

Tristan Remus
  • 34
  • 1
  • 8
  • `at barchart.main(barchart.java:39)` we cannot see line numbers in stack overflow. – FailingCoder Nov 12 '19 at 20:21
  • Sample data does not show comma. and wile getting the token it might be throwing the exception as the string cannot be tokenised. – Swaraj Nov 12 '19 at 20:42

0 Answers0