Skip to content

Instantly share code, notes, and snippets.

@desrtfx
Created March 8, 2014 21:59
Show Gist options
  • Save desrtfx/9439545 to your computer and use it in GitHub Desktop.
Save desrtfx/9439545 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.io.*;
public class Popcorn{
//establishing a print heading.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void processData(String lineToProcess)
{
int commaIndex = lineToProcess.indexOf(',');
String farmName = lineToProcess.substring(0, commaIndex);
String lastTwo = lineToProcess.substring(commaIndex + 1).trim();
String[] split = lastTwo.split(" ", 2);
double acres = Double.parseDouble(split[0]);
double jars = Double.parseDouble(split[1]);
int stars =(int)(jars/acres)/25;
printBarGraph(farmName, stars);
}
public static String printBarGraph(String farm, int stars)
{
System.out.printf("%-30s",farm);
for(int i = 0; i < stars; i++)
{
System.out.print("*");
if (i == 19)
{
System.out.print("#");
}
}
if (stars <19) {
for (int i = stars; i <= 19; i++) {
System.out.print(" ");
}
System.out.print("|");
}
return farm;
}
public static void main(String[] args)throws IOException{
Scanner in = new Scanner(System.in);
System.out.println("Please enter a file name. ");
String fileName = in.next();
//creating a file from user input
File file = new File(fileName);
//validating input and checking if file exists
//and reprompting user
while (!file.exists())
{
System.out.println("Error: Please enter a valid file name. ");
fileName = in.next();
file = new File(fileName);
}
//creating a new filereader and file scanner
FileReader inReader = new FileReader(fileName);
Scanner inFile = new Scanner(inReader);
System.out.printf("%30s", "Popcorn Co-op");
System.out.println();
System.out.println();
System.out.printf("%30s"," ");
System.out.print("Production in Hundreds");
System.out.println();
System.out.printf("%30s", " ");
System.out.print("of Pint Jars per Acre");
System.out.println();
//System.out.print("");
System.out.printf("%-30s", "Farm Name");
System.out.print(" 1 2 3 4 5 6 7 ");
System.out.println();
System.out.printf("%30s", " ");
System.out.print(" ---|---|---|---|---|---|---|---");
System.out.println();
while (inFile.hasNextLine()){
String fullLine = inFile.nextLine();
processData(fullLine);
System.out.println();
}
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment