0

I have this output and I want to determine the computer with largest price

                 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

I got the code, but it print the whole line, I only want the name of that computer here is the code

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

I am thinking like having the first index of that line as the name of the computer

xlecoustillier
  • 16,183
  • 14
  • 60
  • 85

4 Answers4

1

You need a method that returns the name of the computer, or just do 'list[k-1].name' if name is public.

Bary12
  • 1,060
  • 9
  • 23
1

You could do it with string.split:

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

whitespace split regex from: How do I split a string with any whitespace chars as delimiters?

Community
  • 1
  • 1
pennstatephil
  • 1,593
  • 3
  • 22
  • 43
  • this assumes `printComputer()` returns a string. if not, you'll have to put the split in wherever it's printing. – pennstatephil Apr 16 '14 at 22:38
  • It seems that `printComputer()` doesn't return a string but prints that line to the standard output. Moreover, name is probably the first element after the split. – damgad Apr 16 '14 at 22:38
  • `Computer 10 Lenovo` -- the whitespace split on the string would be: computer=0, 10=1, Lenovo=2 – pennstatephil Apr 16 '14 at 22:39
  • But as you can see `the computer with max price is : Lenovo 171 2828.30` so the Lenovo=0, 171=1, 2828.30=2 . It all depends on `printComputer()` :) – damgad Apr 16 '14 at 22:41
  • I assumed he was faking the output? We really need to see `printComputer()` to know for sure... – pennstatephil Apr 16 '14 at 22:42
0

I think you need to split on tabs instead of spaces, so try:

list[k-1].printComputer().split("\t")[2]
Griffin M
  • 451
  • 4
  • 12
0

You can use:

list[k-1].printComputer().split("\\s+")[0]
Ertuğrul Çetin
  • 5,131
  • 5
  • 37
  • 76