string to array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: string to array

  1. #1
    Join Date
    Nov 2002
    Posts
    2

    string to array

    Hello,
    I'm new to Java so it's a stupid question but i ask it anyway.

    I want to sort 2 strings alphabetacly and like to put the two strings in one outputbox. I thought I could access the strings like I do in Delphi but it doesn't work.

    Can someone please help me. This is the program:

    Comment
    invoer_1, invoer_2, uitvoer = TextField
    bewerken = button
    string_1,string_2,tijdelijk = string




    public boolean action( Event e, Object o )
    {
    if ( e.target == invoer_1 )
    { string_1= invoer_1.getText(); }
    if ( e.target == invoer_2 )
    { string_2 = invoer_2.getText() ; }
    if (e.target == bewerken )
    { if (string_1 == null)
    { uitvoer.setText("String 1 niet ingevuld");}
    else if (string_2 == null)
    { uitvoer.setText("String 2 niet ingevuld");}
    else
    { String tijdelijk=string_1+string_2;
    }
    }
    return false;
    }


    I would like to sort the string tijdelijk.

  2. #2
    Join Date
    Nov 2002
    Posts
    59
    The way that I would sort the String is to first change it to a character array by using toCharArray() of the String. From there, you could just do a normal sorting algorithm just as if it were numbers...for example:

    Code:
    for (int i = 0; i < char.length-1; i++) {
      if char[i+1] < char[i] {
        char temp = char[i];
        char[i] = char[i+1];
        char[i+1] = temp;
      }
    }
    This hasn't been compiled, so it may have a few errors, possibly an off by one error...I can never get my sorting on the first try. But hope this helps.

  3. #3
    Join Date
    Nov 2002
    Posts
    2
    Hello,
    Thanks for te reply, you were right it had a bug but after that was removed it worked perfectly. So I thank you again.

    Greetings
    Sky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center