Help how do I get the position out of an array
Hey I have a few problems. When I perform calculations on my array it seems sometimes it works like for finding the total or average of inputting all 20's but other times it doesn't. Also I'm trying to set a variable equal to the position of the max and min number in the array. This isn't working I'm not sure if its initializing it or not. Thanks.
Code :
import java.util.Scanner;
public class RainfallTester {
public static void main(String [] args){
mainRain();
}
public static void mainRain(){
Scanner scan = new Scanner(System.in);
Rainfall temp = new Rainfall();
double rain;
for(double j=0; j<temp.rainfall.length;j++ ){
System.out.print("What is the total rainfall for the month:");
rain = scan.nextDouble();
if(rain <= 0){
System.out.println("Error enter a non negative number.");
mainRain();
}
temp.setRain(rain);
}
temp.calc();
}
}
This is the class with all the proplems
Code :
public class Rainfall {
int hmonth = 0;
int lmonth = 0;
double rainfall[] = new double[12];
public void setRain(double rain){
for(int i =0;i<rainfall.length;i++){
rainfall[i] = rain;
}
}
public void calc(){
double sum =0;
double avg = 0;
double max = rainfall[0];
double min = rainfall[0];
//find total
for(int i =0; i<rainfall.length;i++){
sum += rainfall[i];
}
System.out.println("The total rainfall is "+sum);
//find average
avg = sum/rainfall.length;
System.out.println("The average rain fall is "+avg);
//find max
for (int i=0; i<rainfall.length;i++){
if (max < rainfall[i]){
max = rainfall[i];
hmonth = i;
}
}
System.out.print("The month with the most rain is ");
mostMonth();
System.out.println("with "+max+" rain");
//find min
for (int i=0; i<rainfall.length;i++){
if (min > rainfall[i]){
min = rainfall[i];
lmonth = i;
}
}
System.out.print("The month with the least rain is ");
leastMonth();
System.out.println("with "+min+" rain");
}
//find most month
public void mostMonth(){
if(hmonth == 0){
System.out.print("January ");
}else if(hmonth == 1){
System.out.print("Feburary ");
}else if(hmonth == 2){
System.out.print("March ");
}else if(hmonth == 3){
System.out.print("April ");
}else if(hmonth == 4){
System.out.print("May ");
}else if(hmonth == 5){
System.out.print("June ");
}else if(hmonth == 6){
System.out.print("July ");
}else if(hmonth == 7){
System.out.print("August ");
}else if(hmonth == 8){
System.out.print("September ");
}else if(hmonth == 9){
System.out.print("October ");
}else if(hmonth == 10){
System.out.print("November ");
}else{
System.out.print("December ");
}
}
//find least month
public void leastMonth(){
if(hmonth == 1){
System.out.print("January ");
}else if(hmonth == 2){
System.out.print("Feburary ");
}else if(hmonth == 3){
System.out.print("March ");
}else if(hmonth == 4){
System.out.print("April ");
}else if(hmonth == 5){
System.out.print("May ");
}else if(hmonth == 6){
System.out.print("June ");
}else if(hmonth == 7){
System.out.print("July ");
}else if(hmonth == 8){
System.out.print("August ");
}else if(hmonth == 9){
System.out.print("September ");
}else if(hmonth == 10){
System.out.print("October ");
}else if(hmonth == 11){
System.out.print("Novenmber ");
}else{
System.out.print("December ");
}
}
}
Re: Help how do I get the position out of an array
Quote:
This isn't working
Could you explain what "isn't working" means?
Perhaps post some output from the program and add some comments to it describing what is wrong.
Try debugging the code by adding some println statements that print out the values of the variables as they are given values and used.
Print out the contents of the rainfall array first thing in the calc() method so you can see if the data is correct.
Use the Arrays class's toString() method to format the array for printing:
Code :
System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
Re: Help how do I get the position out of an array
Re: Help how do I get the position out of an array
Okay thanks for answering I did what you said and found that my array isn't storing the right data. My setRain() seems to just be setting the last value entered in it. So my current questions is what's wrong with that part of my code. Thanks
Re: Help how do I get the position out of an array
Can you explain what the setRain() method is supposed to?
Then look at the code and see what it does.
Re: Help how do I get the position out of an array
Yes, from the rainfallTester class in mainRain() I want it to store the user input into the double rain. From there I'm calling the object temp to send it to my Rainfall class (temp.setRain(rain)). Once in the Rainfall class I want it to populate the array. So the for loop will start a position 0 and the first number the user entered will be stored there. I think where its going wrong is the for loop. Once the user inputs a value it stores it but each time the user inputs a value it starts over so it actualy is only storing the last value. Is that right and if so how do I store the users input. Thanks.
Re: Help how do I get the position out of an array
Should the setRain() method store the passed value in ALL of the slots in the rainfall array
or just in one slot in the array?
If only one, then it needs to define the index to the rainfall array as a class variable, use it to save the value and then increment it for the next time setRain() is called.
Re: Help how do I get the position out of an array
Yes I want it to store a value just in 1 slot. I'm not quit sure what you mean I'm sorry to bother but could you show me. Thanks this is a major help I think all would work if I got this right.
Re: Help how do I get the position out of an array
Which part don't you understand?
define the index to the rainfall array as a class variable,
use it to save the value
increment it for the next time
Re: Help how do I get the position out of an array
Mainly the define index as a class variable I'm more of a visual learner. If you could just type out the code for that sorry.
Re: Help how do I get the position out of an array
There are three class variables in the Rainfall class. Add the definition of the index next to the array it will be used to index into.
Re: Help how do I get the position out of an array
Never mind I understand alright that part works, if you don't mind can you help me with the part where I take max value along with the min value and set their positions in the array to a variable.
Code :
for (i=0; i<rainfall.length;i++){
if (max < rainfall[i]){
max = rainfall[i];
hmonth = i;
}
}
System.out.print("The month with the most rain is ");
mostMonth();
System.out.println("with "+max+" rain");
//find min
for (i=0; i<rainfall.length;i++){
if (min > rainfall[i]){
min = rainfall[i];
lmonth = i;
}
}
I think where its going wrong is all of the if statements here.
Code :
//find least month
public void leastMonth(){
if(hmonth == 1){
System.out.print("January ");
}else if(hmonth == 2){
System.out.print("Feburary ");
}else if(hmonth == 3){
System.out.print("March ");
}else if(hmonth == 4){
System.out.print("April ");
}else if(hmonth == 5){
System.out.print("May ");
}else if(hmonth == 6){
System.out.print("June ");
}else if(hmonth == 7){
System.out.print("July ");
}else if(hmonth == 8){
System.out.print("August ");
}else if(hmonth == 9){
System.out.print("September ");
}else if(hmonth == 10){
System.out.print("October ");
}else if(hmonth == 11){
System.out.print("Novenmber ");
}else{
System.out.print("December ");
}
}
}
Thanks so much.
Re: Help how do I get the position out of an array
Quote:
the part where I take max value along with the min value and set their positions in the array to a variable
Please explain what the code does now and what is wrong with that.
Re: Help how do I get the position out of an array
Never mind I had hmonth instead of lmonth. Thanks again for all your help. I'm new when it comes to all this thanks.