# Help with arrays

• 02-20-2010, 05:29 AM
rp1783
Help with arrays
I am new currently in school for programming, and have just started learning Java. My assignment is to write a program that will have the user enter 100 temperatures then it will calculate and output the average temperature, and the max and min temperatures.

I am stuck on the input part, I need to know how to code the user's input and put it into an array....this is what I have so far, I'm not sure if it's even close to being right.

Oh....I forgot I have to write a method for the average, max and min temps.

Code:

```/*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */ package temps; import java.util.Scanner; /**  *  * @author Ryan  */ public class Main {     int temps[100];     /**     * @param args the command line arguments     */         public static void main(String[] args)     {         int temps[];         int [] temparray;                 int y=1;         while (y==y);         {         Scanner Input = new Scanner(System.in);         System.out.println("Please enter a temp:");         temp = Input.nextInt();         System.out.println("Would you like to enter another temp?");         y = Input.nextInt();         }         if (y!=y)         {           temparray[y]=y+1;         }     }     }//end of main       public static int calcAvg(temps [],count)     {     }//end of calcAvg }//end of class```
• 02-20-2010, 05:41 AM
collin389
Quote:

Originally Posted by rp1783
int y=1;
while (y==y);
{
Scanner Input = new Scanner(System.in);
temp = Input.nextInt();
System.out.println("Would you like to enter another temp?");
y = Input.nextInt();
}
if (y!=y)
{
temparray[y]=y+1;
}
}

First of all, y will always = y so if(y != y) will never happen. Also, look at you code and run through it in your head.
you have a number y that = 1.
while that number is equal to itself (it always will be) then:
ask for another temp. However, then you store this input to y (bad idea).
then if y doesn't equal itself (it never will)
you set the temp at pos y (the number the user entered) to the position + 1. this is pointless becuase you want to store the tempature, not the y.

to ask you input you should say:

Code:

```String continue; int counter = 0; do { //ask for tempature //store tempature in the temparray at position (counter) //counter++; System.out.println("Continue? [Y/N]"); continue = input.next(); } while (continue.equals("y") || continue.equals("Y") )```
• 02-20-2010, 05:58 AM
rp1783
Ok, based on what you said, I looked over and saw some things I was doing incorrectly, I'm just not sure how to store the input into the array.
• 02-20-2010, 06:00 AM
rp1783
Here is what I have now, I know there is still work to do, but I'm kind of lost.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package temps;
import java.util.Scanner;

/**
*
* @author Ryan
*/
public class Main
{
int temps[100];

/**
* @param args the command line arguments
*/

public static void main(String[] args)
{

int [] temparray;
temparray=new int[100];
int temp=0;
int y=1;
{
Scanner Input = new Scanner(System.in);
System.out.println("Please enter a temp between 1-100:");
temp = Input.nextInt();
System.out.println("Would you like to enter another temp?");
y = Input.nextInt();
}
if (y<100)
{
temparray[y]=y+1;
}
}

}//end of main

public static int calcAvg(temps [],count)
{

}//end of calcAvg

}//end of class
• 02-20-2010, 06:06 AM
Fubarable
Please use code tags when posting code so that it will retain its formatting and be readable. Please see my signature immediately below for details.
• 02-20-2010, 06:09 AM
collin389
An important thing you should know, to change a var in an array you say:
array[pos] = var;
where array is the name of your array
pos is the position you want to change
var is what you want that position to be;
in your case, you want to data to be the user input temp and the pos to be the counter: y
so you would say: temparray[y] = temp;
also, you dont have a do while loop in there
thirdly,
if (y<100)
{
temparray[y]=y+1;
}
doesn't do what you think it does. if you have a do while loop that ends when y gets to 99, then there is no need for an if statement. also, refer to my previous post where i told you to set the array = temp and then increase the counter by one. to do this you would say: y++; that means y = y +1