-2

I have an JList and I want to get the information of the Element. This is my toString output and I want to get the Element of Kundennummer.

Privatkunde [Vorname= Max| Nachname= Mustermann| Telefonnummer= 017632447658| E-Mail= musterman@max.de| Geburtsdatum= 08.03.1993| Kundennummer= KU543-10] Adresse [Adresszeile 1= Maxstraße, Adresszeile 2= 22, PLZ= 12139, Ort= Berlin]

That is my code how I want to proceed it but it does not work.

 jL.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent evt){
        if(evt.getClickCount()==2 && evt.getButton() == MouseEvent.BUTTON1){
            int index = jL.locationToIndex(evt.getPoint());

            String ausgewaehlteKdnr = model.getElementAt(index).substring(30,2);
            System.out.println(ausgewaehlteKdnr);
            jD.dispose();
        }
    }
});

It gives me the Error. I think that it just count from Privatkunde and finish but that is not what I want. I am glad if someone can say me how I fix it or a better way to get the information

java.lang.StringIndexOutOfBoundsException: String index out of range: -28

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

1 Answers1

-1

substring(30,2) is wrong. The first parameter is the beginIndex, while the second parameter is the endIndex. That is why the exception you see says something about '-28'. Try to invert the two parameters: substring(2,30).

  • Yeah ok that is what I understand of the Exception 2. But if I just want this Line Kundennummer= KU543-10, does it mean that this cant be split from a String method. – MaskulinerJunge Jan 21 '17 at 14:01
  • 1
    @MaskulinerJunge: for better help, create and post a valid [mcve]. – Hovercraft Full Of Eels Jan 21 '17 at 14:41
  • @MaskulinerJunge: Also: 1) You don't `toString()` the Item obtained, 2) using the toString and subString looks like a dangerous kludge. Better would be to get the actual object and call its methods to extract the information that you need. – Hovercraft Full Of Eels Jan 21 '17 at 14:44