Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Arrays/Looping probelm?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Arrays/Looping probelm?

    PROBLEM SOLVED....THANK YOU. I thought there was too much code just in case someone wanted to copy the entire thing, so I deleted it.
    Last edited by dx8292; February 6th, 2012 at 05:23 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Arrays/Looping probelm?

    Alright, first of for your end number, 111, would it be a valid number to get the average of? Would it be unreasonable for someone to use the number 111 when calculating an average? Can you think of any number that would, if in the list of numbers, be skipped? [Hint: This number would not affect the sum at all.]

    Also, because you are not sure the size of the array, this would be a great time for an ArrayList, which will do all the work for you [Although it speeds up the process if you give it a rough estimation about how many numbers, EG 15]. However if you cannot, for some reason, use an arraylist, could you think of any way to send how many numbers were actually used? (Hint, send)

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Arrays/Looping probelm?

    I'm not too familiar with ArrayList.

    Can you use ArrayList with the Scanner and Array?

    array.size() or numinput.size() //like I said, not too familiar with it

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Arrays/Looping probelm?

    You could use an ArrayList with a scanner, but I'm not sure what you mean by with an Array?

    Example of using a scanner
    /*
     * Precondition: END_VAR is a int that is initialized
     */
    Scanner in = new Scanner(System.in);
    int i;
    ArrayList<Integer> array = new ArrayList<Integer>();
    System.out.print("Type your first integer");
    i = in.readInt();
    while(i != END_VAR)
    {
      array.add(i);
      System.out.print("Type another integer or type "+END_VAR+" to exit.");
      i = in.readInt();
    }
    System.out.println("You listed these numbers: "+array);

    Example run (If it was called):
    Type your first integer 5
    Type another integer or 111 to exit. 26
    Type another integer or 111 to exit. 45
    Type another integer or 111 to exit. 15
    Type another integer or 111 to exit. 111
    You listed these numbers: [5, 26, 45, 15]

    As for its size, as you said you would use .size()... instead of .length in a regular array.

    http://docs.oracle.com/javase/7/docs...ArrayList.html
    Last edited by Tjstretch; February 6th, 2012 at 03:05 PM.

  5. The Following User Says Thank You to Tjstretch For This Useful Post:

    dx8292 (February 6th, 2012)

  6. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Arrays/Looping probelm?

    This does seem less complicated, but I think am supposed to stick with arrays. From what I have looked up ArrayList "is similar to Arrays," which means I can't use it with an array, unless I'm interpreting it the wrong way.

    Do you think it would work if I did array.size() or would it be numinput.size();?

  7. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Arrays/Looping probelm?

    Ok the size() method is on objects only, if you were using a regular array you would nee to use the variable length, like:
    /*
     * PRECONDITION: array is a regular array that is not null
     */
    System.out.println("The array has "+array.length+" things in it.");

    However, would it make sense to pass that every time, if that method already has access to the size of the array?

    Hint: The counter variable you increment every time the user enters a number might be useful to your average method (The number of useful numbers in the array) Although I just
    realized that since the array is going to initialize the numbers to 0, this will only make it more efficient, not necessarily more accurate.

  8. The Following User Says Thank You to Tjstretch For This Useful Post:

    dx8292 (February 6th, 2012)

  9. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Arrays/Looping probelm?

    FINALLY GOT IT.....took me half the day for something so ridiculously easy, but I GOT IT.

    Just took out the average method and put the code directly into the main method and did total/count.

    TJstretch thanks for helping with the ArrayList and everything....I'll use that next time when I'm not limited to just Arrays.

  10. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Arrays/Looping probelm?

    Do not delete your original thread! This makes the thread useles for people with the same issues.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Looping Over and Over Again?
    By avalanche72 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 1st, 2012, 04:11 AM
  2. looping and arrays
    By Trunk Monkeey in forum What's Wrong With My Code?
    Replies: 23
    Last Post: January 7th, 2011, 09:16 PM
  3. Java class probelm
    By spikestar in forum Object Oriented Programming
    Replies: 3
    Last Post: May 5th, 2010, 04:32 AM
  4. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM
  5. Probelm in Path and Paths Classes
    By neo_2010 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: July 14th, 2009, 10:39 AM