按控制台输入字符串

时间:2012-12-12 09:53:04

标签: java

我需要一个代码在控制台中编写String并与Array进行比较。

String Array = {"skill"};
Console co=System.console();

然后将Array与co ...进行比较

2 个答案:

答案 0 :(得分:5)

这是一个从键盘读取字符串并将字符串与另一个字符串

进行比较的示例
import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        String input = null;
        String compareString = "hello world";
        Scanner inputReader = new Scanner(System.in);
        System.out.println("please enter: hello world" );

        // here is how you take the input from keyboard

        input = inputReader.nextLine();
                // this is how you write to console
        System.out.println("You entered :" + input);

        //Here is how you compare two string
        if(compareString.equals(input))
        {
            System.out.println("input is: hello world");
        }
        else
        {
            System.out.println("input is not: hello world");
        }
    }
}      

Execution1:

Please enter: hello world

输入:

hello world

输出:

You entered :hello world
input is: hello world

Execution2:

Please enter: hello world

输入:

hello

输出:

You entered :hello 
input is not: hello world

答案 1 :(得分:0)

Jon Skeet早些时候已经回答了这个问题。

Compare string with array of strings

也许就是你所需要的一切)