0

I'm supposed to display the rainfall value for the STATE that I want to search. How do I go about doing this?

rainfall class:

package rainfallassignment;

public class StateRainfall {

double rainAmt[]; //declare array value entered by user
int num_state = 11; //number of states in west malaysia

public StateRainfall(double rain[]) {
    rainAmt = rain;
}   


}

Test program:

package rainfallassignment;

import java.util.Scanner;

public class TestRainfall {

    public static void main(String[] arg) {
        Scanner reader = new Scanner(System.in);

        //declare constant to store size of array
        int num_state = 11;

        //declare array to store rainfall data
        double rain[] = new double[num_state];

        //array representing states
        String[] state = {"Perlis", "Kedah", "Penang", "Perak",
            "Kelantan", "Terengganu", "Pahang",
            "Selangor", "Negeri Sembilan", "Malacca", "Johor"};

        for (int i = 0; i < num_state; i++) {
            System.out.println("Enter Rain value for each state: " 
                    + state[i] + ": ");
            rain[i] = reader.nextDouble();

        }

        //create rainfall object
        StateRainfall rain1 = new StateRainfall(rain);
        for (int i = 0; i < num_state; i++) {
            System.out.println("Rainfall for " + state[i] + " is: " + rain[i]);

        }

    }

}

Is there a special command I should use to search and display the rainfall for a particular state?

3 Answers 3

2

Several remarks to your code:

  1. Instead of having two (synchronised) arrays better use a Map. The main purpose of a Map is to associate a value to a key:

  2. Iterating is better done with forEach, or - since java8 - with streams.

  3. Always be careful with naming your variables. A variable name should clearly show its intention. An array called "state" is misleading when reading the code. Since it is a collection, better use the plural "states".

  4. Constants: Your comment "declare constant to store size of array" is wrong, since you define a local variable. Constants are better defined outside methods within the class body as

        private static int NUM_STATE= 11;
    

It is also good practice to choose another naming style for constants (here: capital letters), which helps you later reading your own code.

  1. A resource (like Scanner) should always be closed in order to release used memory. In my example done by a try-with-resource construct. You can also use try..finally blocks.

Following these tips, your code would boil down to this:

public static void main( String[] args)
{
    Scanner reader = new Scanner(System.in);

    Map<String, Double> rainMap = new HashMap<>();

    //array representing states
    String[] state = {"Perlis", "Kedah", "Penang", "Perak",
        "Kelantan", "Terengganu", "Pahang",
        "Selangor", "Negeri Sembilan", "Malacca", "Johor"};

    for ( String s : state )
    {
        System.out.println("Enter Rain value for each state: " 
            + s + ": ");
        rainMap.put( s, reader.nextDouble());
    }

    rainMap.keySet().forEach(k -> System.out.println("Rainfall for " + k + " is: " + rainMap.get(k)));
}

I think, the question for "search and display" is implicitely also answered.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the remarks. Will put it into practice.
1

I suggest that your StateRainfall keep tracks of both states and rain values, like this

// class
class StateRainfall {
    String[] states;
    double rainAmt[]; //declare array value entered by user
    int num_state; //number of states in west malaysia

    public StateRainfall(String[] states, double rain[]) {
        this.states = states;
        rainAmt = rain;
        num_state = states.length;
    }
}

This will allow you to look into states array and return the corresponding rain value from the index

double getRain(String state) {
    for (int i = 0; i < num_state; i++) {
        if (states[i].equals(state)) {
            return rainAmt[i];
        }
    }
    return -1;
}

// Just a toString() method for convenience
@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < num_state; i++) {
        sb.append("Rainfall for ").append(states[i]).append(" is: ").append(rainAmt[i]);
    }
    return sb.toString();
}

And use all of this like

StateRainfall rain1 = new StateRainfall(state, rain);
System.out.println(rain1);
System.out.println(rain1.getRain("Kelantan"));

1 Comment

Thank you. I got it to work. This was very helpful. Appreciate the help. Cheers.
1

You can implement such thing using Map like this :

public class StateRainfall {
    Map<String, Double> rainfall;

    public StateRainfall(String[] states, double[] rainFall) {
        this.rainfall = new HashMap<>();
        for (int i = 0; i < states.length; i++)
            this.rainfall.put(states[i], rainFall[i]);
    }

    public static void main(String[] arg) {
        Scanner reader = new Scanner(System.in);

        //declare constant to store size of array
        int num_state = 11;

        //declare array to store rainfall data
        double[] rain = new double[num_state];

        //array representing states
        String[] state = {"Perlis", "Kedah", "Penang", "Perak",
                "Kelantan", "Terengganu", "Pahang",
                "Selangor", "Negeri Sembilan", "Malacca", "Johor"};

        for (int i = 0; i < num_state; i++) {
            System.out.println("Enter Rain value for each state: "
                    + state[i] + ": ");
            rain[i] = reader.nextDouble();

        }

        //create rainfall object
        StateRainfall rain1 = new StateRainfall(state, rain);

        System.out.println("Enter state for rainfall report:");
        String stateName = reader.next();
        System.out.println("Rainfall for " + stateName + "  is: " + rain1.getRainFall(stateName));
    }

    public double getRainFall(String state) {
        return this.rainfall.get(state);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.