如何在一行中打印第二个单词

时间:2014-04-16 22:29:08

标签: java

我有这个输出,我想确定价格最高的电脑

                 Name      Capacity  Price
                -----     -------   -----
Computer  1      Dell        170     292.30
Computer  2        HP         94     452.30
Computer  3    Compaq        167     933.30
Computer  4   Toshiba        162    1171.30
Computer  5      Dell        189    1550.30
Computer  6        HP         53    2027.30
Computer  7     Apple         29    2288.30
Computer  8      Dell         48    2542.30
Computer  9      Dell        128    2700.30
Computer 10    Lenovo        171    2828.30
the computer with max price is :
    Lenovo        171    2828.30

我得到了代码,但它打印整行,我只想要那台计算机的名称 这是代码

System.out.println("the computer with max price is :");
        SortByPrice(list);
        int k = list.length;
        list[k-1].printComputer();

我想将该行的第一个索引作为计算机的名称

4 个答案:

答案 0 :(得分:1)

您需要一种方法来返回计算机的名称,或者只需要列出[k-1] .name'如果名字是公开的。

答案 1 :(得分:1)

您可以使用string.split

执行此操作

list[k-1].printComputer().split("\\s+")[2]

空格分割正则表达式:How do I split a string with any whitespace chars as delimiters?

答案 2 :(得分:0)

我认为你需要拆分标签而不是空格,所以试试:

list[k-1].printComputer().split("\t")[2]

答案 3 :(得分:0)

您可以使用:

list[k-1].printComputer().split("\\s+")[0]
相关问题