如何按字母顺序打印输入单词?

时间:2017-11-13 11:44:17

标签: java

你能帮我解决这个问题吗?我找不到正确的方法来做到这一点!

所以它必须像下面的例子一样,扫描仪输入不同!

INPUT:
Two words word1, word2 each on separate lines.
OUTPUT:
Whichever word appears first in the dictionary
SAMPLE INPUT:
hello help
SAMPLE OUTPUT:
hello

1 个答案:

答案 0 :(得分:1)

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

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
    String[] strings = { "anupam", "lucky", "hello", "help" };
    Arrays.sort(strings);
    }
}

或者如果你在arraylist中输入,那么程序将是:

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

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
    ArrayList<String> list=new ArrayList<>();
    Scanner sc=new Scanner(System.in);
    int n;
    n=sc.nextInt();
    for(int i=0;i<n;i++)
    {
        list.add(sc.next());
    }
    Collections.sort(list);
    System.out.println(list);
    }
}