Results 1 to 3 of 3
  1. #1
    Jeremy720 is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default comparing array elements

    Hey, thanks all for your help with my last thread.

    I now have a new challange and can't seem to find a solution.
    I can reverse the order of array elements, but I need to reverse a two-word element in an array. In other words, if the array element contains two words, they need to be reversed. How do I pick out the two word elements from single word ones?

    code:

    ( the elements such as "polar bear" should read "bear, polar")

    public class ArcticAnimals
    {

    String [] animals = new String [] {"seal", "musk ox", "walrus", "polar bear",
    "reindeer", "caribou", "arctic fox", "gray wolf", "orca", "beluga", "whale",
    "sea otter", "snowshoe rabbit"};

    StringTokenizer st = new StringTokenizer("\"seal musk-ox walrus polar-bear " +
    "reindeer caribou arctic-fox grey-wolf orca beluga whale sea-otter " +
    "snowshoe-rabbit\" ");

    public int[] anCopy = new int[animals.length];

    public static void main (String [] args) throws IOException
    {
    ArcticAnimals app = new ArcticAnimals();
    app.doProcess();
    }

    public void doProcess() throws IOException
    {

    System.out.println("Array length is: " + animals.length);

    System.out.println("Before sorting: ");

    while (st.hasMoreTokens())
    {
    System.out.println(st.nextToken() + "");
    }

    Utility.sortString(animals);

    }

    public static void sortString (String [] animals)
    {
    Arrays.sort(animals);
    System.out.println(" After sorting: " + Arrays.asList(animals));
    }


    Thanks so much.
    -Jeremy

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Forget about the elements being in an array and just figure out how to reverse two words in a String. The split method should be of use. What character(s) define the boundary between words?
    Look for the boundary char in each element and when found do the reverse.

  3. #3
    Jeremy720 is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default Think I'm getting there

    Thanks man,
    I've worked more on this and this is what I've come up with. I needed to put commas in-between the two-word animals btw...

    import java.lang.*;
    import java.io.*;
    import java.text.DecimalFormat;
    import java.util.Arrays;
    import java.util.StringTokenizer;

    *public class ArcticAnimals
    {

    String [] animals = new String [] {"seal", "musk ox", "walrus",
    "polar bear", "reindeer", "caribou", "arctic fox", "gray wolf",
    "orca", "beluga", "whale", "sea otter", "snowshoe rabbit"};

    StringTokenizer st = new StringTokenizer("\"seal musk-ox walrus " +
    "polar-bear reindeer caribou arctic-fox grey-wolf orca beluga whale " +
    "sea-otter snowshoe-rabbit\" ");

    String revAn = "seal *musk*"+","+"*ox* walrus *polar*"+","+"*bear* " +
    "reindeer caribou *arctic*"+","+"*fox* *grey*"+","+"*wolf* orca *beluga*"+
    ","+"*whale* *sea*"+","+"*otter* *snowshoe*"+","+"*rabbit*";

    public int[] anCopy = new int[animals.length];


    public static void main (String [] args) throws IOException
    {
    ArcticAnimals app = new ArcticAnimals();
    app.doProcess();
    }

    public void doProcess() throws IOException
    {

    System.out.println("Array length is: " + animals.length);

    System.out.println("Before sorting: ");

    while (st.hasMoreTokens())
    {
    System.out.println(st.nextToken() + "");
    }

    Utility.sortString(animals);

    Utility.reverseString(revAn);

    }

    }

    The Utility class:

    public static void sortString (String [] animals)
    {
    Arrays.sort(animals);


    System.out.println("\n After sorting: " + Arrays.asList(animals));
    }


    public static void reverseString (String str2)
    {

    String reversed = "";

    String[] reverseArray = str2.split("\\*");

    for(int x = reverseArray.length - 1; x > 0; x--)

    reversed += reverseArray[x] + " ";

    reversed += reverseArray[0];

    System.out.println("\n The animals with two words in their name " +
    " are reversed as follows: " + reversed);

    }

    And the output prints names as entered, sorted alphabetically, and in reverse order with the two-word names separated by a comma.

    thanks again.
    -Jeremy

Similar Threads

  1. comparing array using character
    By Anseki in forum New To Java
    Replies: 7
    Last Post: 10-03-2008, 07:28 PM
  2. Replies: 8
    Last Post: 08-27-2008, 10:45 PM
  3. please i need the code of comparing these two array lists.
    By raj reddy in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 04-18-2008, 07:42 AM
  4. reference to elements in array
    By Igor in forum New To Java
    Replies: 1
    Last Post: 12-14-2007, 11:56 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 PM

Posting Permissions

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