Use this sample code: public class Bargraph { //start method public static void main…
- User
Use this sample code: public class Bargraph { //start method public static void main (String args[])throws IOException { //defining all my variables I will use int spaceNeed = 0; int spaceHave = 0; String newLine = ""; String line = ""; int firstIndex = 0; int done = 0; double barHeight = 0; int canvasHeight = 400; int maxPossible = 19000000; int currentStatePop = 0; int left = 0; //rand method define Random rand = new Random(); //Reads in the file and creates the scanner
codeFile theData = new File("stateInfo (1).txt"); Scanner dataReader = new Scanner(theData); //gets user input and makes sure its "men" or "women" String userInput = SimpleIO.getString("Which do you prefer? women or men"); //loop till men or women is answered while (!(userInput.equals("men") || userInput.equals("women"))){ userInput = SimpleIO.getString("Which do you prefer? women or men"); userInput = userInput.toLowerCase(); } //goes to 3rd collumn if men if (userInput.equals("men")){ spaceNeed = 3; } //if women goes to 4th collumn else{ spaceNeed = 4; } //define canvas MyroCanvas canvas = new MyroCanvas("canvas", 800, 400); //skip first row dataReader.nextLine(); //reads each line while(dataReader.hasNext()){ line = (dataReader.nextLine()); //itterates through collumns for (int i = 0; i < line.length(); i++){ if (line.substring(i,i+1).equals(" ")){ spaceHave += 1; } if (spaceHave == spaceNeed){ if (done == 0){ firstIndex = i; done = 1; } } if (spaceHave == (spaceNeed + 1)){ if (done == 1){ //gets the targeted collumn newLine = line.substring(firstIndex + 1,i); done = 2; } } //skips if the targeted collumn and row string has no number in it if (newLine.equals("")){ continue; } } //converts string to integer currentStatePop = Integer.parseInt(newLine); //defines bar height barHeight = canvasHeight * ((double) currentStatePop / maxPossible); //makes bar MyroRectangle rectangle = new MyroRectangle(canvas, left, 400 - (int)barHeight, 16, (int) barHeight); rectangle.visible(); left += 16; Color rColor = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)); rectangle.setFillColor(rColor); rectangle.makeFilled(); spaceHave = 0; done = 0; } } - Assistant