How to use String methods

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mfshake
    New Member
    • Nov 2007
    • 16

    How to use String methods

    I have a driver and server and i can't figure out how to print the # of characters for each input they enter
    I need help on figuring out how to alphabetize the user inputs
    Someone plz help me
    thanks

    THIS IS SERVER.

    import java.util.Scann er;

    public class AlphabetizePara puzha {

    static Scanner reader = new Scanner (System.in);
    private String [] names = new String [4];//the array that holds 4 names
    private int option;//the user option
    private int count;//the counter
    private String name;//close name

    public AlphabetizePara puzha(){

    while(true){
    menu();
    selection();
    }//close whlie
    }//close main

    public int menu()
    {
    while(true){
    String menu = "\n\4ENTER GAME\4\n1)Print \n2)Enter 4 Strings"
    + "\n3)Quit" + "\nMake a selection: ";
    try{
    System.out.prin t(menu);
    option = reader.nextInt( );
    if(option >= 1 && option <= 3){
    reader.nextLine ();//CONSUME EXTRA CHARACTER
    break;
    }else{
    System.out.prin t("\nEnter a 1 or 2:\n");
    reader.nextLine ();
    }//close if
    }catch(Exceptio n e){
    System.out.prin t("\nEnter a number answer:\n");
    reader.nextLine ();
    }//exception
    }//close whlie
    return option;
    }//close menu

    public void setChoice(int answer)
    {
    option = answer;
    }//close setChoice

    public int getChoice()
    {
    return option;
    }//close getChoice

    public int selection()
    {
    if(getChoice() == 3){
    quit();
    }else if(getChoice() == 2){
    for(int i = 0; count < names.length; i++){
    System.out.prin t("\nEnter Your name: ");
    name = reader.nextLine ();
    names[count] = name;
    count++;
    }//close for
    }else{
    print();
    }//close if
    return option;
    }//close getRun

    public void quit()
    {
    System.exit(99) ;
    }//close quit

    public void print()
    {
    System.out.prin t("\nCOLOUM #2:\n");
    for(int i = 0; i < count; i++){
    int l = name.length();
    System.out.prin t("# of Characters are " + l + "\n");
    }//close for
    }//close getPrint

    }//AlphabetizePara puzha
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Use code tags when posting code.
    2.) What do you mean by "alphabetiz e the input"?
    3.) If you are having problems with how to use String methods, then you should check the API docs for it.

    Comment

    • pronerd
      Recognized Expert Contributor
      • Nov 2006
      • 392

      #3
      Originally posted by mfshake
      i can't figure out how to print the # of characters for each input they enter

      If just want the string length of the values you are reading in it would be.....

      Code:
      name = reader.nextLine();
      int len = name.length();


      Originally posted by mfshake
      I need help on figuring out how to alphabetize the user inputs
      You will want to review the Java tutorials on Java collections. Specifically the sortable collections. Google can also provide a lot of examples of this.

      Comment

      Working...