JoeyObee
JoeyObee

Reputation: 21

Writing basic Java, Arrays and For Loop

HeyI was wondering if anyone can help me with this? Im basically attempting to write a program that reads data for the temp and rainfall for a week, stores the data in an array and then outputs the data in several different forms. I have it done (or so I thought) but for some bizarre reason its just not working. The program runs fine, takes the info, but all the data fields output as "0". Could use a fresh pair of eyes in a big way :) Any help gladly appreciated

import java.util.Scanner;
public class rainFallProject {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

            //Creates scanner object
            Scanner myScanner=new Scanner(System.in);

            //declare array for temperature
            int [] tempList = new int[7];

            //declare array for rainfall
            int [] rainList = new int[7];

            //NOTE all arrays need only be 7 places long, as there will only ever be 7 entries

            //declare a variable for the just entered temp
            int tempInput;

            //declare a variable for the just entered rainfall
            int rainInput;

            //declare variable for highest temp
            int highestTemp=0;

            //declare variable for lowest rainfall amount
            int lowestRain=0;



            for(int i = 0; i < tempList.length; i++){

                //Prompt the user to enter a temperature
                System.out.format("Please enter the temperature for day %d:", i+1);
                //stores the input as just entered temp
                tempInput = myScanner.nextInt();
                //Assigns the temp to its correct place in the array
                tempInput = tempList[i];
                    //updates the highest temp, if necessary
                    if(tempInput>highestTemp){
                        tempInput=highestTemp;
                    }


                //Prompt the user to enter the rainfall amount
                System.out.format("Please enter the rainfall for day %d:", i+1);
                //stores the input as just entered rainfall
                rainInput = myScanner.nextInt();
                //Assigns the rain to its correct place in the array
                rainInput = rainList[i];
                    //updates the lowest rain, if necessary
                    if(rainInput<lowestRain){
                        rainInput=lowestRain;
                    }

            }
            //Declare the total temperature
            int tempSum = (tempList[0]+tempList[1]+tempList[2]+tempList[3]+tempList[4]+tempList[5]+tempList[6]);

            //Declares the total rainfall
            int rainSum = (rainList[0]+rainList[1]+rainList[2]+rainList[3]+rainList[4]+rainList[5]+rainList[6]);
            //Output the results:

            //-Weekly average temperature ((sum of all temps)/7)
            System.out.format("The weekly average temperature was %d degrees celsius. %n", (tempSum/7));
            //-Highest temperature of the week
            System.out.format("The highest temperature for the week was %d degrees celsius.%n", highestTemp);
            //-Total weekly rainfall amount (sum of all rainfalls)
            System.out.format("The total weekly rainfall amount for the week was %d mm. %n", rainSum);
            //-Lowest daily rainfall amount
            System.out.format("The lowest amount of rainfall for the week was %d mm. %n", lowestRain);



    }

}

Upvotes: 1

Views: 262

Answers (4)

Richard Tingle
Richard Tingle

Reputation: 17236

Here you set tempInput to highestTemp, highestTemp is always zero

  //updates the highest temp, if necessary
  if(tempInput>highestTemp){
        tempInput=highestTemp;
  }

You seem to do this throughout the program, the thing on the left it set equal to the thing on the right, Not the other way round. Remember it is always:

variableToBeUpdated=newVariableValue

Lines of code are commands not equations

Possibly this confusion comes from how lines of java look like equations, but they are not. The following is a completely valid line of java code, but an invalid equation

a=a+1;

Now as an equation there are obvious problems with this, but as a command it makes sense

"Make (thing on left) equal to (thing on right) [by changing (thing on left)]"  
"Make (a) equal to (a+1) [by changing (a)]"

So a becomes one larger than it used to be

Upvotes: 3

TheLostMind
TheLostMind

Reputation: 36304

        rainInput = myScanner.nextInt();
        //Assigns the rain to its correct place in the array
        rainInput = rainList[i];

here you are overwriting the value you have read from the user with 0. (tempList[i] is 0). This is your problem...

Upvotes: 1

Tim B
Tim B

Reputation: 41208

             tempInput = tempList[i];

should be

            tempList[i] = tempInput;

You have the same mistake in a few other places too, with your assignments the wrong way around.

Upvotes: 2

Kayaman
Kayaman

Reputation: 73578

//stores the input as just entered temp
tempInput = myScanner.nextInt();
//Assigns the temp to its correct place in the array
tempInput = tempList[i];

<mythbusters voice> Well there's your problem! </mythbusters voice>

Upvotes: 1

Related Questions