Results 1 to 16 of 16
  1. #1
    sonofJack3 is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Arrays made from user input

    Hey all,
    I'm wondering how I'm supposed to get a variable list of input values into an array. I know that the method header "public methodName (int ... list)" can be used when the list is known, but can this be used when the list is user input?

    Basically, I want the user to input a set of integers; any number of integers is allowed. Each integer needs to be added to an array, but I'm totally stuck on how to get each one in. Any help?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Arrays made from user input

    You will need a loop and a way for the user to signal they have finished. Inside the loop read a number and add it to the array. If the array is full you will need to create a new bigger array, copy all values across and then add the new value. Try writing the code and experiment. When you get stuck post your code and include error messages.

  3. #3
    sonofJack3 is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: Arrays made from user input

    import java.util.Scanner;

    public class HistogramGenerator
    {
    public static void main (String[] args)
    {
    Scanner scan = new Scanner(System.in);
    int[] list = new int[10];
    int num = 1;

    for (int count = 0; num != 0 && count <= 9; count++)
    {
    System.out.println("Enter an integer (0 to quit): ");
    num = scan.nextInt();

    while (num > 100 || num < 0)
    {
    System.out.println("Invalid. Enter an integer (0 to quit): ");
    num = scan.nextInt();
    }

    list[count] = num;
    }


    }
    }

    Thanks for your help!
    No errors yet, although I'm a little fuzzy on whether this works. I have a question about the number of integers; the question defines the program as being able to read an "arbitrary" number of integers in the range of 1 to 100 (hence the bounds I put on the loop and the use of 0 to signal the loop's end). Do you think the word arbitrary means I'm able to ask the user for the number of integers they will input (this is something I may email my professor about)? If I'm unable to do that, what should I do to expand the size of the array?

  4. #4
    Join Date
    Nov 2011
    Posts
    28
    Rep Power
    0

    Default Re: Arrays made from user input

    Why are you using just an array if you don't know the amount of elements? Something like an ArrayList would be better suited, similar to a vector in c++. It will expand itself when need be

  5. #5
    sonofJack3 is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: Arrays made from user input

    Oh I know, but this chapter is all about arrays and doesn't say anything about ArrayLists, so I'm just assuming arrays are necessary.

  6. #6
    Join Date
    Nov 2011
    Posts
    28
    Rep Power
    0

    Default Re: Arrays made from user input

    Ah hmm, well in that case. You could declare the array, and then in a constructor have the user input how many elements they wish to store. Then initialize the array like
    Java Code:
    int [] array;
    
    public constructor(int size)
    array = new int[size];
    Or you could just copy the elements and move them to a larger array. Or use both!

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Arrays made from user input

    Quote Originally Posted by sonofJack3 View Post
    If I'm unable to do that, what should I do to expand the size of the array?
    I already told you in my first reply.

  8. #8
    sonofJack3 is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: Arrays made from user input

    Okay guys, I'm just gonna lay this out. The question asks to make a program that prompts the user for a set of integers. Then, the program makes a frequency distribution based on how many integers fell into the categories 1-10, 11-20, etc. up to 91-100 using asterisks. Here is the program:

    import java.util.Scanner;

    public class HistogramGeneratorEasy
    {
    public static void main (String[] args)
    {
    Scanner scan = new Scanner(System.in);

    System.out.println("How many elements will be entered? ");
    final int LIMIT = scan.nextInt();

    int[] list = new int[LIMIT];
    int num = 1;

    for (int count = 0; (count + 1) <= list.length; count++)
    {
    System.out.println("Enter an integer between 1 and 100: ");
    num = scan.nextInt();

    while (num > 100 || num < 1)
    {
    System.out.println("Invalid. Enter an integer between 1 and 100: ");
    num = scan.nextInt();
    }

    list[count] = num;
    }

    System.out.print("1 - 10 | ");
    for (int j = 0; j <= list.length; j++)
    {
    if (list[j] < 11)
    System.out.print("*");
    }

    System.out.print("11 - 20 | ");
    for (int j = 0; j <= list.length; j++)
    {
    if (list[j] > 10 && list[j] < 21)
    System.out.print("*");
    }

    System.out.print("21 - 30 | ");
    for (int j = 0; j <= list.length; j++)
    {
    if (list[j] > 20 && list[j] < 31)
    System.out.print("*");
    }

    System.out.print("31 - 40 | ");
    for (int j = 0; j <= list.length; j++)
    {
    if (list[j] > 30 && list[j] < 41)
    System.out.print("*");
    }

    System.out.print("41 - 50 | ");
    for (int j = 0; j <= list.length; j++)
    {
    if (list[j] > 40 && list[j] < 51)
    System.out.print("*");
    }

    System.out.print("51 - 60 | ");
    for (int j = 0; j <= list.length; j++)
    {
    if (list[j] > 50 && list[j] < 61)
    System.out.print("*");
    }

    System.out.print("61 - 70 | ");
    for (int j = 0; j <= list.length; j++)
    {
    if (list[j] > 60 && list[j] < 71)
    System.out.print("*");
    }

    System.out.print("71 - 80 | ");
    for (int j = 0; j <= list.length; j++)
    {
    if (list[j] > 70 && list[j] < 81)
    System.out.print("*");
    }

    System.out.print("81 - 90 | ");
    for (int j = 0; j <= list.length; j++)
    {
    if (list[j] > 80 && list[j] < 91)
    System.out.print("*");
    }

    System.out.print("91 - 100 | ");
    for (int j = 0; j <= list.length; j++)
    {
    if (list[j] > 91 && list[j] < 100)
    System.out.print("*");
    }
    }
    }

    It's a little long-winded, especially all those for loops at the end, but I can't figure out a way to compress it any more. It should work in theory, but I'm getting the error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at HistogramGeneratorEasy.main(HistogramGeneratorEasy .java:38)
    (The number 10 is the number of elements I entered on that attempt).
    This has something to do with my first for loop, but I'm not sure what.
    Can somebody evaluate this program and explain why I'm getting this error?

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Arrays made from user input

    When you create an array with a length of 10 the valid indicies are 0 - 9. Your code is looping until it is less than or equal to length. Thus 10 throws an AIOOBE. Why do you have so many loops iterating over the same array of numbers again and again? This is very inefficient. How about 1 loop that iterates over the array once and increases a counter for each range.

  10. #10
    sonofJack3 is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: Arrays made from user input

    Okay, I got the error to go away, but as for the efficiency thing, I need each set of asterisks to be created separately, basically within their own loops. If I put them all in one loop, only one asterisk can be printed on each iteration and when it goes through each successive time the categories ("1-10", etc) will be printed over again. Unless I'm not understanding this right?

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Arrays made from user input

    I didn't say have one loop printing out the stars (although this can be achieved by using a method). I suggested having one loop that checks which range each score is in and updates a variable. Then you can print out the stars for each range.
    Java Code:
    loop {
        if current score is less than or equal to 10 {
            increment 1-10 variable
        } else if current score is less than or equal to 20 {
            increment 11-20 variable
        } etc
    }
    printStars(1-10 variable);
    printStars(11-20 variable);
    etc

  12. #12
    sonofJack3 is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: Arrays made from user input

    Okay, I've got it now, thank you. I have another unrelated question lol. I'm making a sentinel-controlled loop that is reading integers input from the user (all integers are valid). But my sentinel has to be an integer... what can I use as a sentinel to get me out of the loop (or is there another way)?

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Arrays made from user input

    If this is for the same program then I imagine a negative value would not be a valid value.

  14. #14
    sonofJack3 is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: Arrays made from user input

    It's actually a different program... any integer is valid, but there needs to be a way to get out of the loop; I used 0 for now, but that is a valid integer, as far as I know.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Arrays made from user input

    If any integer is valid then there is no way to do it. What you will need to do is ask the user if they want to continue. If they say no set a boolean to false.

  16. #16
    sonofJack3 is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: Arrays made from user input

    I was afraid of that... okay thanks for all your help.

Similar Threads

  1. Replies: 4
    Last Post: 03-09-2011, 05:54 AM
  2. Need help getting input(first/last name) from user
    By nightrise420 in forum New To Java
    Replies: 11
    Last Post: 09-11-2010, 03:09 AM
  3. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 01:59 AM
  4. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 04:47 AM
  5. Making arrays by reading user input
    By apfroggy0408 in forum New To Java
    Replies: 23
    Last Post: 04-30-2008, 01:23 AM

Tags for this Thread

Posting Permissions

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