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 4 of 4

Thread: array troubles

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default array troubles

    Hi There
    Im having difficulty with sorting out an element in my code.

    I have an array with 6 spots that need to increment every time the user enters one of the figures.
    That part i have done

    I need to show what the highest and lowest scores were
    Ive done that too

    But now I need to attach a string to the highest and lowest. The problem is that if there are multiple spots e.g 2,2,2 for the lowest. the computer says lowest = 2 which is good but when I attach a string to match the correct array element three strings will print out. I only need it to say one, more importantly the lowest array element.

    array
    Assume the user knows what colour represent which number
    yellow = 1, green = 2, purple = 3, white = 4, black = 5, blue = 6, red = 7

    int colours[]={1,2,3,4,5,6,};
    int counter[]={0,0,0,0,0,0};
    e.g user enters numbers
    2
    2
    3
    3
    4
    4
    4
    5
    5
    5
    6
    6
    6
    7
    7
    7
    7
    I want the print out to look like this:

    the lowest number was "green" with 2
    the highest number was "red" with 7


    Except its doing this:

    The lowest number was "green" with 2
    the lowest number was "purple" with 2
    and so on

    Any help would be awesome

    Thanks


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: array troubles

    The problem is that if there are multiple spots e.g 2,2,2 for the lowest. the computer says lowest = 2 which is good but when I attach a string to match the correct array element three strings will print out. I only need it to say one, more importantly the lowest array element.
    You did not tell how to attach a String to match the correct array element so I do not know what is wrong.

    Why not use a pair of arrays like this:

    int colours[]={1,2,3,4,5,6,};
    String colorNames[]={"yellow", "green", "purple", "white", "black", "blue"};

    immutable objects
    Last edited by ha.minh.nam; December 4th, 2011 at 07:29 PM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: array troubles

    the highest number was "red" with 7
    I'm confused why the print out shows 7 when the list of numbers you posted only has 4 7's in it?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: array troubles

    It would help if you posted your code.
    Improving the world one idiot at a time!

Similar Threads

  1. Instantiation troubles
    By hello_world in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 16th, 2011, 08:24 PM
  2. ArrayList troubles
    By javapenguin in forum What's Wrong With My Code?
    Replies: 20
    Last Post: November 18th, 2010, 06:03 PM
  3. [SOLVED] ArrayList Troubles.
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 2nd, 2010, 10:19 AM
  4. JSP Troubles
    By sdkeslar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 12th, 2010, 02:26 PM
  5. Array Troubles
    By Leeds_Champion in forum Collections and Generics
    Replies: 6
    Last Post: October 22nd, 2009, 11:05 AM