用户输入到Array

时间:2017-01-20 15:57:37

标签: java java.util.scanner

我想获取用户输入并将其添加到字符串数组中。我只想在数组中添加一个元素。然而,程序打印“输入新任务”后,程序停止运行所以我没有时间进入任务。它说建立成功。我做错了什么?

我的代码:

CREATE TABLE #tblBatch
(
    BatchId INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    Sname VARCHAR(50) NOT NULL,
    BatchNo int,
    BatchSequenceNo int
)

---Load some sample data
INSERT INTO #tblBatch(Sname)
    SELECT TOP 2010 
        'A -SampleText-' + CAST(row_number() over(order by t1.number) as varchar) as N
    FROM
        master..spt_values t1
    CROSS JOIN
        master..spt_values t2 

输出:

 Scanner reader = new Scanner(System.in);
 String[] taskList= new String[100];

 System.out.println("Enter new task");
 taskList[0] = reader.nextLine();

2 个答案:

答案 0 :(得分:0)

尝试以下代码。扫描仪每次读取一行

Scanner reader = new Scanner(System.in);
String[] taskList= new String[100];

System.out.println("Enter new task");
reader.nextLine();
taskList[0] = reader.nextLine();

答案 1 :(得分:0)

请尝试使用此代码:

String[] tasks = new String[1];
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter new task: ");
String task = reader.readLine();
tasks[0] = task;
System.out.println("Your task is: " + task);

希望它有所帮助。