Results 1 to 12 of 12

Thread: empty arrays.

  1. #1
    circuspeanuts is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default empty arrays.

    Hello, I'm new to the community, and would like some help with this third java program I'm in the process of writing :) if you can.

    I'm working on a java program that allows the user to enter a random unspecified amount of integers, determine how many positive or negative numbers there are, and computes the total and average of the input values, not including zeros. Zero will be used to end the program. (for instance if you entered 1, 2, and 0, your average would be 1.5).

    I have just started writing the code (as I have just finished with my age lab to determine ability to vote, drink, retire, etc.)

  2. #2
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    what have you got so far, is there an error message? This should be really easy to do.

  3. #3
    circuspeanuts is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    well from the jist of it so far from what I'm understanding I'm basically pulling something such as:

    int Numbers[] = new Numbers[]

    to pull off and declare my array, but when I look at it I can't help by notice that it doesn't really have an open array feel to it. So...I'm looking through the book that I have, which is "Introduction to Java Programming" by Y. Daniel Liang.

  4. #4
    circuspeanuts is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    nevermind, it would be something along the lines of

    String Choices = JOptionPane.showInputDialog("Please pick a random amount of integers (0 to quit)");

    int Choices = Integer.parseInt(Choices);

    int[] aChoices = new int[Choices]




    EDIT:


    ok I've got all that down, but now how do I allow them to enter 0 to quit and then take the averages of numbers?
    Last edited by circuspeanuts; 04-06-2009 at 05:18 PM.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    To exit your program when 0 is specified:
    Java Code:
    String Choices = JOptionPane.showInputDialog("Please pick a random amount of integers (0 to quit)");
    
    int Choices = Integer.parseInt(Choices);
    
    [B][COLOR="Blue"]if (Choices == 0)
      {
       System.out.print("Bye !!!");
       System.exit(0);
      }[/COLOR][/B]
    
    int[] aChoices = new int[Choices]
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    circuspeanuts is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    awesome, I'll just replace that with the closing function and that'll be set.

    I was wrong on the array values though. I tried it out, and it only allows for 1 variable to be input. I was thinking of using a do while loop for the array values. Something along the lines of:
    do
    {
    JOptionPane.showMessageDialog(null, "Please enter a number (or press 0 to quit).")'
    } while (iChoices != 0);
    but that doesn't work properly..
    Last edited by circuspeanuts; 04-06-2009 at 05:43 PM.

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    I think you need to do some planning. Have you done a flow chart or pseudo code of what your code is supposed to do? Maybe some pseudo code like:
    Java Code:
    userNum = 0
    count = 0
    average = 0
    totalSum = 0
    start loop
    ask user for number
    If userNum == 0
        display total sum, 
        calculate average (totalSum/count)and display it
        exit program
    else 
    count++
    total = total + userNum
    end loop
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    circuspeanuts is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    I have thought it through as I've been writing, and I have made some pseudocode, which is what I've been trying to impliment and see what I have to change around for it to work. Although, I never had a count in my code. I didn't see the need for the count in there. You're not thinking on the same strain of thought I am, which is fine becasue this method may work better :)

    my thought on this then is to use a do while loop, and then when the user enters zero have it switched over to an end function? All of the loops that I have in my book require an ending value.

    Another thing too is that I'm parsing it in right after I ask for the integer everytime. I don't know if that's a smart thing to do or not. It own't mess up the program will it?

    EDIT: I'm not entirely sure how to add the integers from the array together either. Does anyone happen to have a reference page I can use for examples and such? or how to input those variables into an array...
    Last edited by circuspeanuts; 04-06-2009 at 06:16 PM.

  9. #9
    circuspeanuts is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    come to think about it, if I make an array our of this and wrap it around, is it going to be able to recognize the negative numbers I enter in?

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    OK... I forgot that you're using an array which is even easier... with an array, you don't need the count variable (the for loop would already be using one). Since you don't know how many numbers will be entered, shouldn't you be using an arraylist?
    Java Code:
    start loop
    ask user for number
    If userNum == 0
        display total sum, 
        calculate average (totalSum/[B][COLOR="Blue"]number of loops[/COLOR][/B])and display it
        exit program
    else 
    [COLOR="blue"][B]add userNum to array
    calculate totalSum (this step can be skipped if you have a method that does totalSum calculation)[/B][/COLOR]
    end loop
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    circuspeanuts is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    well now, this is my code...just to update everyone:

    import javax.swing.*;

    public class Main {

    private
    int UsrNum = 0;
    int Average = 0;
    int TotalSum = 0;
    int Count = 0;
    int NumArray [];

    public void SetTotalandSum()
    {

    do
    {
    String UserNum = JOptionPane.showInputDialog("Please enter an integer");
    UsrNum = Integer.parseInt(UserNum);

    if (UsrNum == 0)
    {
    JOptionPane.showMessageDialog(null, "The total sum of your integers is " + TotalSum);
    Average = TotalSum/Count;
    JOptionPane.showMessageDialog(null, "The Average of your integers is " + Average);
    System.exit(0);
    }
    else
    Count ++;
    }while (UsrNum != 0);

    }

    public Main() {
    }

    public static void main(String[] args) {
    Main oAve = new Main();

    oAve.SetTotalandSum();

    }

    }
    so I took your advice and redid it in your way, CJSLMAN, and it's worked tons better than my original idea. However, now I'm slightly more confused as to your change. I'm trying to get my array in my program now after I've written it, which isn't the best idea, I know.

  12. #12
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Since you don't know the amount numbers that the user will input, I would use an ArrayList. It's the same as an array, except that it doesn't have the restriction of the before hand capacity and it contains primitive types.

    Java Code:
    import javax.swing.*;
    
    public class Main {
    
    private
    int UsrNum = 0;
    int Average = 0;
    int TotalSum = 0;
    int Count = 0;
    //int NumArray [];
    [COLOR="Blue"][B]ArrayList<Integer> numArray = new ArrayList<Integer>();[/B][/COLOR]
    public void SetTotalandSum()
    {
    
    do
    {
    String UserNum = JOptionPane.showInputDialog("Please enter an integer"); 
    UsrNum = Integer.parseInt(UserNum);
    
    if (UsrNum == 0)
    {
    JOptionPane.showMessageDialog(null, "The total sum of your integers is " + TotalSum);
    //Average = TotalSum/Count;
    [COLOR="blue"][B]Average = TotalSum/numArray.size();[/B][/COLOR] 
    JOptionPane.showMessageDialog(null, "The Average of your integers is " + Average);
    System.exit(0);
    }
    else
    {
     [B][COLOR="blue"]numArray.add(UsrNum);[/COLOR][/B]
     //Count ++;
    
    }while (UsrNum != 0);
    
    } 
    
    public Main() {
    }
    
    public static void main(String[] args) {
    Main oAve = new Main();
    
    oAve.SetTotalandSum();
    
    }
    
    }
    Here's a couple of links about ArrayLists and it's methods ... they're very useful:

    ArrayList (Java Platform SE 6)
    Java ArrayList example - AnyExample.com

    I haven't run the code, so you might have to tweak it some. I'lll leave the totalSum up to you (hint: make it a method).
    Suggestion: name your variables with the first letter not capitalized (ex: someVariable)... it's a Java standard.


    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. JXL Empty cell problem
    By ironicending in forum New To Java
    Replies: 1
    Last Post: 01-26-2009, 04:32 PM
  2. KeyBindings Empty Key?
    By Unome in forum Java Applets
    Replies: 1
    Last Post: 10-24-2008, 08:28 PM
  3. Empty ResultSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 09:36 PM
  4. Deleting an empty directory
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:17 AM
  5. BufferedReader empty
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 07:52 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
  •