# Thread: Help with arrays

1. Member
Join Date
Feb 2010
Posts
3
Rep Power
0

## 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.

Java 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```
moderator edit: code tags added
Last edited by Fubarable; 02-20-2010 at 05:53 AM. Reason: code tags added

2. Senior Member
Join Date
Nov 2009
Posts
235
Rep Power
8
Originally Posted by rp1783
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;
}
}
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:

Java Code:
```String continue;
int counter = 0;
do
{
//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") )```

3. Member
Join Date
Feb 2010
Posts
3
Rep Power
0
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.

4. Member
Join Date
Feb 2010
Posts
3
Rep Power
0
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

5. 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.

6. Senior Member
Join Date
Nov 2009
Posts
235
Rep Power
8
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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•