我必须使用字符串Tokenizer和String方法按字母顺序排列单词。我是Java的初学者所以我只知道像char,比较,布尔操作,if语句,switch语句for,while循环和String 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()
{
}
}
答案 0 :(得分:0)
字符串tokenizer类允许应用程序将字符串分解为标记。对于你的情况,你可以使用标记器来分隔单词然后你可以使用一个比较函数,它对一些对象集合强加了一个总排序,你可以找到更多关于引用这些文档[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());
}
}