in Education by
I am trying to do a telephone directory using arrays (I have to use arrays). I am trying to write the method for adding new entries. I decided to add a new entry with a line that will be split using the split method into three parts (surname, initials, number) with tabs. When I tried to do the testing for the method I got thrown an IndexOutOfBoundsException. This is the addEntry method @Override public void addEntry(String line) { String[] entryLine = line.split("\\t"); String surname = entryLine[0]; String initial = entryLine[1]; String number = entryLine[2]; Entry entry = new Entry(surname, initial, number); count++; if (surname == null || initial == null || number == null) { throw new IllegalArgumentException("Please fill all the required fields, [surname,initials,number]"); } if (count == entries.length) { Entry[] tempEntries = new Entry[2 * count]; System.arraycopy(entries, 0, tempEntries, 0, count); entries = tempEntries; } else { int size = entries.length; for (int i = 0; i < size - 1; i++) { for (int j = i + 1; j < entries.length; j++) { String one = entry.getSurname(); if (one.toLowerCase().compareTo(surname.toLowerCase()) > 0) { Entry tempE = entries[i]; entries[i] = entries[j]; entries[j] = tempE; } } } } } This is the entry I tried to add: arrayDirectory.addEntry("Smith SK 005598"); JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
If the String you're entering is really Smith SK 005598 Then your splitting regex \\t (tab) cannot work, as the pieces are not separated by tabs. Instead, you need to use line.split("\\s+"); As \s+ will match any number of spaces. The output will correctly result in [Smith, SK, 005598] To have each piece separated by a tab, you'd use Smith\tSK\t005598 Only then your original regex will work.

Related questions

0 votes
    I want to raise an exception in Python and in future want to detect it using an except block, Can someone tell me how to do it? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Which of the following keywords is used for throwing exception manually? (a) finally (b) try (c) throw ( ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    When I'm calling a Dll method it sometimes throws an exception, and sometimes doesn't. I'm calling ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    Which of the following keywords is used for throwing exception manually? (a) finally (b) try (c) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    import re text = 'The quick. black n brown? fox jumps*over the lazy dog.' print(re.split('; | ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    import re text = 'The quick. black n brown? fox jumps*over the lazy dog.' print(re.split('; | ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    import re text = 'The quick. black n brown? fox jumps*over the lazy dog.' print(re.split('; | ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    import re text = 'The quick. black n brown? fox jumps*over the lazy dog.' print(re.split('; | ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I'm trying to test my Category class. I'm using Mockery::mock() method, with 'overload:' prefix ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm trying to test my Category class. I'm using Mockery::mock() method, with 'overload:' prefix ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    Which exception is thrown by read() method? (a) IOException (b) InterruptedException (c) SystemException (d) ... Applets of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    It is a good practise to not throw which exception in close() method of autocloseable? (a) IOException (b) ... & API of Java Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    When I am running a Hadoop .jar file from the command prompt, it throws an exception saying no such method ... (Child.java:264) Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    Which exception is thrown by read() method? (a) IOException (b) InterruptedException (c) SystemException (d ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    It is a good practise to not throw which exception in close() method of autocloseable? (a) IOException ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
...