Getting an error in my code

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 487 Views - Last Post: 08 December 2015 - 05:06 PM Rate Topic: -----

#1 CoffeeMethod   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-December 15

Getting an error in my code

Posted 08 December 2015 - 02:37 PM

Below is the problem i need to do and after that is my code at this point.
I am getting the error java.lang.ArrayIndexOutOfBoundsException: 1
Can you please tell me what is wrong and what i should look at so i can try to fix it on my own.


Write a method printFirstLast whose signature is given below. This method should print the names(s) passed in the input parameter name as follows:

Input name          Return
    
    W,Jeff          Jeff W
      
    Smith,          Smith
      
    Jones           (nothing is printed)
     
    ,               (nothing is printed)
    Doe,Roxanne     Roxanne Doe
    ,Susan          Susan
//In writing this methods you may use the indexOf and substring methods in the String class.

    /**

    * printFirstLast  prints out (to the screen) a name with the first name then the last name

    * @param: input  String = name as lastname,firstname or any other form

    * @param:return  String = name with first then last name

    */

    public String printFirstLast(String name)

    {
     String result = "";

    //your code here

    return result;
    }



 /**
    *   printFirstLast  prints out (to the screen) a name with the first name then the last name
    *   @param: input  String = name as lastname,firstname or any other for
    *   @param:return  string with fist name then last name
    */
    public String printFirstLast(String name)
    { 
        String result = "";
        String[] split= name.split(",");
        if(split.length == 0)
        {
            return null;
        }
        else
        {
            System.out.println(split[0] + " "+ split[1]);
        }
        return result;
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Getting an error in my code

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15268
  • View blog
  • Posts: 61,206
  • Joined: 12-June 08

Re: Getting an error in my code

Posted 08 December 2015 - 02:38 PM

What error are you getting? Please copy/paste it here.
Was This Post Helpful? 0
  • +
  • -

#3 CoffeeMethod   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-December 15

Re: Getting an error in my code

Posted 08 December 2015 - 02:41 PM

View Postmodi123_1, on 08 December 2015 - 02:38 PM, said:

What error are you getting? Please copy/paste it here.

The error i am getting is this java.lang.ArrayIndexOutOfBoundsException: 1
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,944
  • Joined: 25-December 13

Re: Getting an error in my code

Posted 08 December 2015 - 02:41 PM

Quote

java.lang.ArrayIndexOutOfBoundsException: 1

That says the code used an index (1) past the end of the array(the array has less than 2 elements).
Remember that the valid indexes for an array range from 0 to the array's length-1.

Check the code where the exception happened and see why it used an index past the end of the array.
The code should check that the array has two elements (.length >= 2) before using an index of 1.

Note: when posting error messages, you should copy the full text and paste it here. It should include the source line number.

This post has been edited by NormR: 08 December 2015 - 02:42 PM

Was This Post Helpful? 0
  • +
  • -

#5 CoffeeMethod   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-December 15

Re: Getting an error in my code

Posted 08 December 2015 - 02:45 PM

Sorry here is the full error message

java.lang.ArrayIndexOutOfBoundsException: 1
at StringProjectCoded.printFirstLast(StringProjectCoded.java:97)
at StringProjectCoded.main(StringProjectCoded.java:24)

View PostNormR, on 08 December 2015 - 02:41 PM, said:

Quote

java.lang.ArrayIndexOutOfBoundsException: 1

That says the code used an index (1) past the end of the array(the array has less than 2 elements).
Remember that the valid indexes for an array range from 0 to the array's length-1.

Check the code where the exception happened and see why it used an index past the end of the array.
The code should check that the array has two elements (.length >= 2) before using an index of 1.

Note: when posting error messages, you should copy the full text and paste it here. It should include the source line number.


I will look at the code and get back to you in a couple of minutes
Was This Post Helpful? 0
  • +
  • -

#6 CoffeeMethod   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-December 15

Re: Getting an error in my code

Posted 08 December 2015 - 03:08 PM

I tried doing this but i still get the same error code
public String printFirstLast(String name)
    { 
        String result = "";
        String[] split= name.split(",");
        if(split.length == 0)
        {
            return " ";
        }
        for(int i=0; i<=split.length;i++)
        {
            System.out.println(split[0] + " "+split[1]);
        }
        
        return result;
    }


Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,944
  • Joined: 25-December 13

Re: Getting an error in my code

Posted 08 December 2015 - 03:14 PM

Where does the code test the size of the array >= 2 before trying to use an index of 1?

What is the for loop on line 9 for? If the contents of split does not change inside of the loop, it will always print the same values at index 0 and 1.

The method returns a String: result but never assigns it a useful value.

This post has been edited by NormR: 08 December 2015 - 03:16 PM

Was This Post Helpful? 0
  • +
  • -

#8 CoffeeMethod   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-December 15

Re: Getting an error in my code

Posted 08 December 2015 - 03:17 PM

View PostNormR, on 08 December 2015 - 03:14 PM, said:

Where does the code test the size of the array >= 2 before trying to use an index of 1?

What is the for loop on line 9 for? If the contents of split does not change inside of the loop, it will always print the same values at index 0 and 1.

I am confused on how to test the size. I read in other places that the for loop could help but it doesnt so i will remove it
Was This Post Helpful? 0
  • +
  • -

#9 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,944
  • Joined: 25-December 13

Re: Getting an error in my code

Posted 08 December 2015 - 03:22 PM

Quote

how to test the size.

The code on line 5 and line 9 uses the .length field which contains the size.
Line 5 tests for a length of 0. The code should test if the length is >=2 before using an index of 1 on line 11

This is not the place for a for loop. Use an if statement like on line 5

This post has been edited by NormR: 08 December 2015 - 03:23 PM

Was This Post Helpful? 0
  • +
  • -

#10 CoffeeMethod   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-December 15

Re: Getting an error in my code

Posted 08 December 2015 - 03:27 PM

View PostNormR, on 08 December 2015 - 03:22 PM, said:

Quote

how to test the size.

The code on line 5 and line 9 uses the .length field which contains the size.
Line 5 tests for a length of 0. The code should test if the length is >=2 before using an index of 1 on line 11

This is not the place for a for loop. Use an if statement like on line 5

Ok So i replaced the for loop with an if statement that says
if(split.length>=2)

and it got rid of the error but now I am not getting the right output.
I am getting this

W Jeff
W,Jeff has a result of: 
Smith, has a result of: 
Jones has a result of: 
,  has a result of: 
Doe Roxanne
Doe,Roxanne has a result of: 
 Susan
,Susan has a result of: 


Was This Post Helpful? 0
  • +
  • -

#11 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,944
  • Joined: 25-December 13

Re: Getting an error in my code

Posted 08 December 2015 - 03:30 PM

Quote

I am not getting the right output.

Please explain what is wrong with the output and show what it should be.
Was This Post Helpful? 0
  • +
  • -

#12 CoffeeMethod   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-December 15

Re: Getting an error in my code

Posted 08 December 2015 - 03:37 PM

View PostNormR, on 08 December 2015 - 03:30 PM, said:

Quote

I am not getting the right output.

Please explain what is wrong with the output and show what it should be.


Ok i just fixed part of the problem so it now looks like this:
Jeff W
W,Jeff has a result of: 
Smith, has a result of: 
Jones has a result of: 
,  has a result of: 
Roxanne Doe
Doe,Roxanne has a result of: 
Susan 
,Susan has a result of: 



But what i need it to look like is this:
W,Jeff has a result of: Jeff W
Smith, has a result of: Smith
Jones has a result of: 
,  has a result of: 
Doe,Roxanne has a result of: Roxanne Doe
,Susan has a result of:Susan  


Was This Post Helpful? 0
  • +
  • -

#13 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,944
  • Joined: 25-December 13

Re: Getting an error in my code

Posted 08 December 2015 - 03:43 PM

What's the difference? A blank String where there should be something?

Look at the comment I made about result. Where is it given a value?

This post has been edited by NormR: 08 December 2015 - 03:44 PM

Was This Post Helpful? 0
  • +
  • -

#14 CoffeeMethod   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-December 15

Re: Getting an error in my code

Posted 08 December 2015 - 04:44 PM

Ok I fixed the problem with them lining up but now i get this
W,Jeff has a result of: Jeff W
Smith, has a result of: Smith,
Jones has a result of: Jones
,  has a result of: 
Doe,Roxanne has a result of: Roxanne Doe
,Susan has a result of: Susan 




The problem here is that Jones is supposed to print out nothing.

Here is the updated code.
 public String printFirstLast(String name)
    { 
        String[] split= name.split(",");
        if(split.length == 0)
        {
            return "";
        }
        if(split.length>=2)
        {
            name = (split[1] + " "+split[0]);
        }
        
       return name;
    }


Was This Post Helpful? 0
  • +
  • -

#15 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,944
  • Joined: 25-December 13

Re: Getting an error in my code

Posted 08 December 2015 - 05:00 PM

Quote

Jones is supposed to print out nothing.

What can the code test for so that it prints out nothing?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2