Results 1 to 6 of 6
  1. #1
    rp1783 is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default 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 04:53 AM. Reason: code tags added

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Quote Originally Posted by rp1783 View Post
    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 temp
    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
    {
    //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") )

  3. #3
    rp1783 is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

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

    Default

    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. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #6
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    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

Similar Threads

  1. Arrays
    By tejens23 in forum New To Java
    Replies: 5
    Last Post: 11-25-2009, 10:46 PM
  2. Please help me with arrays!
    By ddrcan in forum New To Java
    Replies: 2
    Last Post: 08-10-2009, 04:30 PM
  3. Help with arrays!
    By lilac87 in forum New To Java
    Replies: 2
    Last Post: 07-28-2009, 04:57 AM
  4. Need help regarding Arrays Help pls....
    By shaggyoo7 in forum New To Java
    Replies: 3
    Last Post: 01-14-2009, 04:36 AM
  5. A little help with arrays..
    By zeppelin in forum New To Java
    Replies: 8
    Last Post: 01-05-2009, 12:33 PM

Posting Permissions

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