Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cannot capture values in array

    Code:
    import javax.swing.JOptionPane;
    public class TestB
    {
    public static void main(String args[]){
    int[] num =new int[5];
    int count=0;
    int sum=0;
    String showval;
    
    for (int i = 0; i < 5; i++){
    //while (count<5){
    showval=JOptionPane.showInputDialog("Enter the number: " + (count+1));
    
    sum=sum+num[count];
    
    count++;
    }
    int average = sum / num.length;
    JOptionPane.showMessageDialog(null, 
    "Your number is: " + (num[]) + "\n" + "Your average is: " + average );
    }
    }
    //}

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,841
    Thanks
    42
    Thanked 200 Times in 199 Posts
    in the future please actually pose a question- the problem though is you never assign values to the int's in the array num[]... so when you try to look at num[whatevernumberitdoesntmatter] it will be 0 (or null, idk what Java defaults this to). So also with that, the line sum = sum + num[count] will always be 0 = 0 + num[#] but all the numbers in num are 0 so 0 = 0 + 0 equaling 0.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

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