How to recognize a string in alphabetical order

return a string in alphabetical order.

Page 1 of 1

8 Replies - 5430 Views - Last Post: 14 November 2010 - 01:07 PM Rate Topic: -----

#1 rockerboy23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-November 10

How to recognize a string in alphabetical order

Posted 11 November 2010 - 07:20 PM

My program will have to read in a string which contains a number of words, and report the first word alphabetically and the last word alphabetically to the user.
I have the first part of the program, which prints out the words given by the user in the order given, but I am completely lost on how to determine alphabetical order in Java.

Please help!

This post has been edited by rockerboy23: 11 November 2010 - 07:21 PM

Is This A Good Question/Topic? 0
  • +

Replies To: How to recognize a string in alphabetical order

#2 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: How to recognize a string in alphabetical order

Posted 11 November 2010 - 09:35 PM

you can convert a char to an int. From there you can compare the numerical values
Was This Post Helpful? 0
  • +
  • -

#3 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: How to recognize a string in alphabetical order

Posted 11 November 2010 - 09:43 PM

Easiest way is to split your user's input into a string array and sort the array using the sort method in the Arrays class.
Scanner in = new Scanner(System.in);
String[] userInput = in.nextLine().trim().split(" ");
Arrays.sort(userInput);

// now your array is sorted a-z; print out 1st element and last element
System.out.println("First: " + userInput[0]);
System.out.println("Last: " + userInput[userInput.length - 1]);


Was This Post Helpful? 1
  • +
  • -

#4 rockerboy23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-November 10

Re: How to recognize a string in alphabetical order

Posted 12 November 2010 - 03:19 PM

View Postbcranger, on 11 November 2010 - 08:43 PM, said:

Easiest way is to split your user's input into a string array and sort the array using the sort method in the Arrays class.
Scanner in = new Scanner(System.in);
String[] userInput = in.nextLine().trim().split(" ");
Arrays.sort(userInput);

// now your array is sorted a-z; print out 1st element and last element
System.out.println("First: " + userInput[0]);
System.out.println("Last: " + userInput[userInput.length - 1]);





How can I do this without using array?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: How to recognize a string in alphabetical order

Posted 12 November 2010 - 03:21 PM

How about showing what you've written? Post your code in code tags, as shown below:

:code:
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: How to recognize a string in alphabetical order

Posted 12 November 2010 - 03:42 PM

There is no good way to do this without arrays. You basically have to use split(), which returns a String[], unless you want to way overcomplicate things with substring(). And such overuse of substring() is a horrid solution.
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: How to recognize a string in alphabetical order

Posted 13 November 2010 - 06:45 AM

Quote

How can I do this without using array?


Here's a clue how to do it without an array:

        String max = " ";
	String min = "~";
	Scanner s = new Scanner(input);
	while(s.hasNext()) {
	    String newS = s.next();
	    if(newS.compareTo(max) > 0) {
	       max = newS;
	    }
....


This post has been edited by g00se: 13 November 2010 - 06:46 AM

Was This Post Helpful? 1
  • +
  • -

#8 rockerboy23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-November 10

Re: How to recognize a string in alphabetical order

Posted 14 November 2010 - 01:05 PM

this is what I have so fare

 This is what I have so far 

 * This program will read in a list of words, separated by spaces, and report them
 * back to the user with the first word alphabetically, and also the last word 
 * alphabetically.
 */
import java.util.Scanner;
class WordsInOrder {
    public static void main (String [ ] args) {
    
    String str ; // string entered by user
    int index; // stores index value
    String next; // hold value of substring
    String first; //
    String last;// 
    int len;// fnds length of word, needed when user doesn't enter input
    Scanner input = new Scanner (System.in);
    System.out.print ( " Enter a list of words:  "   );
    str=input.nextLine ( );
    len = str.length( );
    if ( len == 0) {// if the user does not enter any input
        System.out.println (" Please enter a word or multiple words ");
        str=input.nextLine ( );
    } // if the user does not enter any input
    index = str.indexOf (" "); // finds index of space
    while (index >-1 ) { // while words are followed by spaces
        next= str.substring (0, index); // finds word before space
        str = str.substring (index +1); // finds everything after space
        index = str.indexOf (" "); // finds index of space
        System.out.println (next); 
    } // while words are followed by spaces
    System.out.println (str); 
  
    if (next.compareTo(first) < 0) { // before first
        first=next; // replace first
        System.out.println (" first word is " + first );
    } // before first
    else if (next.compareTo(last ) > 0) { // after last
        last=next; // replace last
        System.out.println ("last word is " + last  );
    } // after last
} // end main
    } // class



I have to do this using only substring, no arrays

Edited by macosxnerd101: Please, :code:.
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: How to recognize a string in alphabetical order

Posted 14 November 2010 - 01:07 PM

There's no array in my example - only a Scanner (which you're already using)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1