Results 1 to 4 of 4

Thread: Array help

  1. #1
    clerd is offline Member
    Join Date
    Apr 2013
    Posts
    15
    Rep Power
    0

    Default Array help

    I'm trying to use the JOptionPane and a while loop to input two lists ( so two arrays?) of 32 numbers, which are made up by the user. The data cannot be sorted before it is entered into the arrays. It is supposed to ignore the numbers -1000 and 1000. I need to be able to print the average high and average low of the numbers and lows or highs position in order with the day( the index of the array?). I'm new at this so any help is appreciated. Here's what I have so far.

    Java Code:
    import java.util.*;
    
    
    public class rainfall {
    
     public static int[] Low;
     public static int[] High;
    	public static void main(String[] args) {
    		Init();
    		Print();
    		Load();
    		
    
    	}
    	public static void Init()
    	{
    		Low = new int[32];
    		High = new int[32];
    		for (int i = 1; i <= 31; i++){
    			Low[i] = 1000;
    			High[i] = -1000;
    	}
    		
    		public static void Load(int [] A)
    		{
    			int [] Low = new int[31];
    			int [] High = new int [31];
    			int i = 0;
    			while (i < Low.length){
    				JOptionPane?
    		}
    		
    			public static void Print()
    			{
    				
    			}
    			
    		}
    	}
    
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Array help

    A couple of things: first use standard Java coding conventions. Classes start with an uppercase letter (Rainfall) and methods and variables with a lowercase letter (low, init() etc).

    Secondly once you have an approach figured out (your code suggests you have, but you don't say) then work on *one* small piece at a time. Don't worry about load() until you have init() finished, compiling without error and tested as well as you can.

    ---

    So the first thing to do is remove load() and print() and get init() functioning. At the moment you have mismatched braces associated with that method and they have to be fixed. That problem will be a lot more visible if you remove the clutter of things that are to come.

    Then you can work on "JOptionPane?". Think about the actual steps you have to go through to put a number into an array. (Any textbook should help) Don't get panicked by the fact that it's happening in a for loop: that's a detail, the important thing is how you (a) prompt the user, (b) get the user's response (a String?), (c) turn the user's response into an int and (d) put the int into an array.

  3. #3
    clerd is offline Member
    Join Date
    Apr 2013
    Posts
    15
    Rep Power
    0

    Default Re: Array help

    Quote Originally Posted by pbrockway2 View Post
    A couple of things: first use standard Java coding conventions. Classes start with an uppercase letter (Rainfall) and methods and variables with a lowercase letter (low, init() etc).

    Secondly once you have an approach figured out (your code suggests you have, but you don't say) then work on *one* small piece at a time. Don't worry about load() until you have init() finished, compiling without error and tested as well as you can.

    ---

    So the first thing to do is remove load() and print() and get init() functioning. At the moment you have mismatched braces associated with that method and they have to be fixed. That problem will be a lot more visible if you remove the clutter of things that are to come.

    Then you can work on "JOptionPane?". Think about the actual steps you have to go through to put a number into an array. (Any textbook should help) Don't get panicked by the fact that it's happening in a for loop: that's a detail, the important thing is how you (a) prompt the user, (b) get the user's response (a String?), (c) turn the user's response into an int and (d) put the int into an array.
    I know it's not formatted very well right now, I just feel like I can't do anything else until I get the Load method completed and I'm not sure how to go about that. :(

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Array help

    Do what pbrockway2 said, look up java conventions. also what is the point of your load method? so far the while loop will always evaluate to true since i is always ==0.

    this code wont compile you have too many "}" remove the final 2 } from the class and it should be ok in terms of }.

    You already know how to increment an integer look at your for loop (i++) so inside the while loop after your JOptionPane you will need to increment this i value otherwise you will get an infinite loop.

    But seriously sort out the format of the code, you can loop up the JOptionPane API to find out the exact set of parameters you need to solve your problem. get the basics down first though.


    Also your code wont compile because your load method takes an array as a argument but when you calling in in your main you arent passing anything into it see below:

    Your method:

    load(int [] a) {

    // rest of code

    }

    your call:

    load();

    this will throw an error, you have to call it as

    load(int[]) int[] being either high or low. Another way around this would be to make your load method take no arguments and deal with the passed in array inside the method.

    Why does it even have the (int[] A), the load method doesnt use A anywhere in its function?
    Last edited by monkeyjr97; 04-23-2013 at 02:31 AM.

Similar Threads

  1. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  2. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

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