CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  1. #1
    Join Date
    Jul 2011
    Posts
    23

    More Java Help: Splitting an Array

    Hi All

    this question is from the same class as i have been writing: Basically i have the following method:

    public void makeTrainingSchedule(String[] courseNames, Integer[][] badges)
    {
    Map<String, Integer[]> map1 = new HashMap<String, Integer[]>();

    for(int num = 0; num < courseNames.length; num ++)
    {
    map1.put(courseNames[num], badges[num]);
    }
    this.trainingRoster = map1;
    }

    The above takes an array of string and an array of an array of integers and places them in a map. The map is then referenced to an instance variable called trainingRoster.
    Now i need to take all the integers out of trainingRoster and place them into a SortedSet as individuals.

    I know im probably overthinking this, but how do i go about accessing all the integers in the map of trainingRoster?
    Last edited by PhoenixHeights; July 16th, 2011 at 03:56 AM.

  2. #2
    Join Date
    Jul 2011
    Posts
    23

    Re: More Java Help: Splitting an Array

    Just to add i've tried this

    public void uniqueSortedBadges()
    {
    SortedSet<Integer> sortedBadge = new TreeSet<Integer>();

    List<Integer> list1 = new ArrayList<Integer>();

    Integer[] cloneBadge;
    Set<String> keys = this.trainingRoster.keySet();
    for(String item : keys)
    {
    cloneBadge = this.trainingRoster.get(item);
    }
    for(Integer item : cloneBadge)
    {
    }

    }

    but i just get told that cloneBadge hasnt been initilized

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: More Java Help: Splitting an Array

    Please use code tags when posting code.

    The warning is because you haven't explicitly initialized cloneBadge and it is possible that you will attempt to use it in an un-initialized state ie if trainingRoster is empty the first for-each loop will not be executed and so cloneBadge you not be set to any value. The answer is to either:

    1. Set it to null where you declare it and then test it for null after the first for-each loop and only execute the second loop if it is not null.
    2. Set it to an empty Integer array where you declare it in which case if the first for loop doesn't execute then the second one wont either.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: More Java Help: Splitting an Array

    If trainingRoster is a Map, why get all the keys and use each key to search for its value, when you can just get all the values (using Map.values()) and iterate over them directly (without using the keys)?

    To arrive at the simple is difficult...
    R. Elisha
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Jul 2011
    Posts
    23

    Re: More Java Help: Splitting an Array

    Quote Originally Posted by dlorde View Post
    If trainingRoster is a Map, why get all the keys and use each key to search for its value, when you can just get all the values (using Map.values()) and iterate over them directly (without using the keys)?

    To arrive at the simple is difficult...
    R. Elisha
    ive tried doing that but then i get told i couldnt mix Collection and Arrays together.

    Im really at a loss how to do this.


    Id like to add, i am by no means looking for the answer, as this is a uni assignment, just some direction how to go about it.

    i realise the i need to iterate through every element of the map values of trainingRoster, and then add each element to sortedBadge, im just not sure how to go about it.
    Last edited by PhoenixHeights; July 16th, 2011 at 08:19 AM.

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: More Java Help: Splitting an Array

    I've already told you how to get your existing code working.

    If you want to change your code to the way dlorde suggested (and if you aren't using the keys in this section of code then his suggestion it is a better way of doing it) post the code you have tried and we will help you.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Jul 2011
    Posts
    23

    Re: More Java Help: Splitting an Array

    Quote Originally Posted by PhoenixHeights View Post
    public void uniqueSortedBadges()
    {
    Set<Integer[]> sortedBadge = new TreeSet<Integer[]>();

    for(Integer[] item : this.trainingRoster.values())
    {
    sortedBadge.add(item);
    }

    System.out.println(String.valueOf(sortedBadge));

    }
    Clearly it just doesnt work! Like i said im noob and im learning!

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: More Java Help: Splitting an Array

    Please post code in code tags and not quote tags. And rather than just saying it doesn't work please tell us what doesn't work - is it failing to compile or just not doing what you expect? Finally post any compiler or run time error messages.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  9. #9
    Join Date
    Jul 2011
    Posts
    23

    Re: More Java Help: Splitting an Array

    Sorry i dont know how to do code tags!

    It comiles but i get the following error during run time

    Semantic error: [Ljava.lang.Integer; cannot be cast to java.lang.Comparable

  10. #10
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: More Java Help: Splitting an Array

    Please post the full text of the error message and the line(s) of code that it refers to.

    The message seems to say that you are trying to cast an Integer array.

    What do you expect/want this expression to do:
    String.valueOf(sortedBadge)

    What datatype/class is sortedBadge?
    What type of argument does the valueOf() method take?
    Norm

  11. #11
    Join Date
    Jul 2011
    Posts
    23

    Re: More Java Help: Splitting an Array

    Quote Originally Posted by Norm View Post
    Please post the full text of the error message and the line(s) of code that it refers to.

    The message seems to say that you are trying to cast an Integer array.

    What do you expect/want this expression to do:
    String.valueOf(sortedBadge)

    What datatype/class is sortedBadge?
    What type of argument does the valueOf() method take?
    Right, i want the method to take all the values from the Map instance variable trainingRoster, and place them into a Set as indiviual elements. The Values are an array of an array tho.

    Im afraid that is the full error message.

    If im honest im not sure about: String.valueOf(sortedBadge), i thought everything had to be converted to a string before its printed.

  12. #12
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: More Java Help: Splitting an Array

    Sorry i dont know how to do code tags!
    Read the bottom of each of my posts - it explains how to do it.

    The problem is because you are using a sorted set and items added to a sorted set must be Comparable. Integer arrays are not, but you can provide your own Comparator object when you construct the TreeSet.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  13. #13
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: More Java Help: Splitting an Array

    i thought everything had to be converted to a string before its printed
    sortedBadge is not an primitive, its a class. The String valueOf() method probably does not know what to do with it. You may have to extract its contents item by item and print them individually.

    What line of code causes the error?
    Norm

  14. #14
    Join Date
    Jul 2011
    Posts
    23

    Re: More Java Help: Splitting an Array

    Quote Originally Posted by keang View Post
    Read the bottom of each of my posts - it explains how to do it.

    The problem is because you are using a sorted set and items added to a sorted set must be Comparable. Integer arrays are not, but you can provide your own Comparator object when you construct the TreeSet.
    Sorry i dont see any signature at the bottom of your posts, i'll try to write a

    I'll write a new compareTo method that allows Integer arrays and let you know the results.

  15. #15
    Join Date
    Jul 2011
    Posts
    23

    Re: More Java Help: Splitting an Array

    Sorry, this is starting to get messy! Lets start again!

    I have the following code:

    Code:
     
    
       public void makeTrainingSchedule(String[] courseNames, Integer[][] badges)
       {
          Map<String, Integer[]> map1 = new HashMap<String, Integer[]>();
          
          for(int num = 0; num < courseNames.length; num ++)
          {
             map1.put(courseNames[num], badges[num]);
          }
          this.trainingRoster = map1;
       }
    This takes a string array and and integer array and places them into a map. so the result is something like this:

    two : [10, 20, 30, 40]
    one : [1, 2, 3]
    three : [200, 400, 100, 300, 500]


    Now i want a new method to take all those integers and place them into a Sorted Set! The problem im having is iterating over the Map trainingRoster' values and placing every individual element into a sorted set. well it doesnt have to be a sorted set i just need to be able to arrange the numbers in order.
    Last edited by PhoenixHeights; July 16th, 2011 at 11:21 AM.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured