2016-12-12 52 views
0

我必須按字母順序使用字符串Tokenizer和String方法放置單詞。我是一個Java初學者,所以我只知道像char,比較,布爾操作,if語句,switch語句,while循環和String Tokenizer等... 我有這個到目前爲止。使用字符串Tokenizer按字母順序放置單詞

import java.util.*; 
public class Alphabetize 
{ 
    // Establish Global Class-State Variables 
    static StringTokenizer st; 
    public static void main() 
    { 
     // Input Data 
     inData(); 
     //Variable Assigment 
     varAssign(); 
     //Process Data 
     alphaOrder(); 
     //Output Information 
     outPrint(); 
    } 

    public static void inData() 
    { 
     // Input Data 
     String n= "Hailee James Kate Emily Alex Jackie"; 
     st = new StringTokenizer (Hailee James Kate Emily Alex Jackie); 
    } 

    public static void varAssign() 
    { 
     //Process Data 

    } 

    public static void alphaOrder() 
    { 
     //Outprint Information 
    } 

    public static void outPrint() 
    { 

    } 
} 
+1

開始通過讀取的Javadoc https://docs.oracle.com/ja vase/7/docs/api/java/util/StringTokenizer.html –

回答

0

字符串標記器類允許應用程序將字符串分解爲標記。對於你的情況,你可以用分詞器分離的話,然後你可以使用一個比較函數強行對某些對象collection進行整體排序,你可以找到更多關於參照這些文檔[1] [2]

* package whatever; // don't place package name! */ 

import java.util.*; 
import java.lang.*; 
import java.io.*; 

/* Name of the class has to be "Main" only if the class is public. */ 
class Alphabetize 
{ 
    public static void main (String[] args) throws java.lang.Exception 
    { 
     String n= "Hailee James Kate Emily Alex Jackie"; // try to use meaningful identifiers(instead of n use sentence or etc..) 
     StringTokenizer tokenizer = new StringTokenizer(n); 
     List<String> sortedArrary = new ArrayList<String>(); 

    while(tokenizer.hasMoreTokens()) { 
    sortedArrary.add(tokenizer.nextToken()); 
} 

Collections.sort(sortedArrary, new Comparator<String>() { 
    @Override 
    public int compare(String o1, String o2) { 
     return o1.compareToIgnoreCase(o2); 
    } 
}); 
    System.out.println(sortedArrary.toString()); 
    } 
} 
+0

感謝您解答我的問題! –