> Java Program help?

Java Program help?

Posted at: 2014-12-18 
Hi there,

To answer your question, you will need to do the following:

1. Store the valid inputs of the user into an array of integers

2. Use an algorithm that searches for the smallest integer within that array

3. Define a method that takes the region number (e.g. 3) and returns the region name (e.g., East)

Here's how I would do it. Feel free to ask if you'd like to have an explanation of the code.

Cheers!

----------------------

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class accidents {

public static void main(String[] args) {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int[] regionAccidents = new int[5];

for (int regionIndex = 0; regionIndex < 5; regionIndex++) {

regionAccidents[regionIndex] = getNumAccidents(br, regionIndex + 1);

}

try {

br.close();

} catch (Exception e) {

System.out.println("Error in closing the buffered reader.");

e.printStackTrace();

br = null;

}

int lowestIndex = findLowest(regionAccidents);

System.out.println("Region with lowest accidents: " + getRegionName(lowestIndex + 1));

System.out.println("Number of accidents in region: " + regionAccidents[lowestIndex]);

System.out.println("Bye!");

}



private static int getNumAccidents(BufferedReader br, int regionId) {



String s = null;

int i = -1;

while (i < 0) {

System.out.print("Enter number of accidents in " + getRegionName(regionId) + " region: ");

try {

s = br.readLine();

i = validateInput(s);

} catch (Exception e) {

System.out.println("Error in reading the input.");

e.printStackTrace();

i = -1;

}

}

return i;

}



private static int validateInput(String s) {

int i = -1;

try {

i = Integer.parseInt(s);

if (i < 0) {

System.out.println("Invalid value! Please enter a value greater than or equal to zero.");

}

} catch (NumberFormatException nfe) {

System.out.println("Invalid format! Please enter whole numbers only.");

}

return i;

}

private static String getRegionName(int regionId) {

String regionName = null;

switch (regionId) {

case 1:

regionName = "North";

break;

case 2:

regionName = "South";

break;

case 3:

regionName = "East";

break;

case 4:

regionName = "West";

break;

case 5:

regionName = "Central";

break;

default:

break;

}

return regionName;

}



private static int findLowest(int[] regionAccidents) {

int lowestIndex = 0;

for (int i = 0; i < regionAccidents.length - 1; i++) {

for (int j = i + 1; j < regionAccidents.length; j++) {

if (regionAccidents[lowestIndex] > regionAccidents[j]) {

lowestIndex = j;

}

}

}

return lowestIndex;

}

}

The assignment is: Write a program that determines which of five geographic regions within a major city (1-north, 2-south, 3-east, 4-west, and 5-central) had the fewest reported automobile accidents reported in that region during the last year. It should have the following two methods, which are called by main.

? The method named getNumAccidents is passed the number of a region. It asks the user for the number of automobile accidents reported in that region during the last year, validates the input, then returns it. It should be called once for each city region.

? The method named findLowest is passed the five accident totals. It determines which is the smallest and prints the name of the region, along with its accident figure.

Input Validation: Do not accept an accident number that is less than 0.

How would I make the program able to recognize which region has the lowest amount of accidents after being entered by the user? I'm a little new to Java. Thanks!