Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

java - values set by user

Options
  • 02-12-2012 6:28pm
    #1
    Registered Users Posts: 16


    Hey all,
    Just wondering how it is possible to ask a user how many values they wish to set and then what each of those values are?
    As in, if you were to ask a user to enter a set of numbers, by first asking how many numbers they wish to enter.
    Is the best way to do this with an array?
    I had started to write something like this:

    String in = JOptionPane.showInputDialog(null, "how many values do you wish to enter? ");

    int n = Integer.parseInt(in);

    int [] arr = new int [n];

    but I am confused about how to actually set the values. Am I way off with even using an array? I just don't know!

    Once the values are set the sum and average will be calculated, but I'm fine for doing that, it's just getting the data in that's causing me stress!
    Tagged:


Comments

  • Registered Users Posts: 1,731 ✭✭✭GreenWolfe


    You're on the right track. You'll need to use a for loop to set the value for each element in the array.


  • Registered Users Posts: 16 awney


    You'll need to use a for loop to set the value for each element in the array.

    so once i've done that, with a for loop i can calculate the sum and the average?


  • Registered Users Posts: 1,731 ✭✭✭GreenWolfe


    awney wrote: »
    so once i've done that, with a for loop i can calculate the sum and the average?

    That should work fine.


  • Registered Users Posts: 16 awney


    i've moved on a bit, but am equally confused. where abouts should i be putting the code to calculate the sum and the average? here's what i've done:

    String in = JOptionPane.showInputDialog(null, "how many values do you wish to enter? ");
    int n = Integer.parseInt(in);

    int [] arr = new int [n];

    int i;
    int sum = 0;

    for (i=0; i<arr.length; i++){
    String numStr =JOptionPane.showInputDialog(null, "Enter value: ");
    int arr1 = Integer.parseInt(numStr);
    arr = arr1;


    }

    sum = sum + arr;
    int avg = (sum / n);
    JOptionPane.showMessageDialog(null, "the sum of the numbers you entered is: " + sum + " and the average is " + avg);


  • Registered Users Posts: 1,731 ✭✭✭GreenWolfe


    You should put your code into code tags, it will be easier to read. Check the # symbol in the post editor.

    If you've run that code, you'll notice that you're getting an ArrayIndexOutOfBounds exception at
    sum = sum + arr[i];
    
    It's trying to go beyond the bounds of the array. How can this be prevented?

    Your sum and avg calculations are already there, they seem OK. Also, there isn't a need to have a counter variable with that much scope (int i). They can be kept within their loops in this case.


  • Advertisement
  • Registered Users Posts: 8,324 ✭✭✭chrislad


    You don't need to declare 'i' outside the for loop. You can just do int i = 0; i < arr.length; i++;

    It's a simple problem. Would there be a better place to add the value of the array to the sum so that it would be done over and over until all elements of the array are added?


Advertisement