Ask Experts Questions for FREE Help !
Ask
    iyea070's Avatar
    iyea070 Posts: 1, Reputation: 1
    New Member
     
    #1

    Jun 8, 2014, 06:31 AM
    Java: Array Not Working
    Hey everyone,

    I have an issue where I have a method and I am trying to check if the name of an object is the same as a String parameter for the method. It does not seem to be working for me. If anyone could assist me, I would be grateful:

    Code:
    public String toString(String checkName)
    {
        int i=0;
        while (i < count) //count is equal to 3
        {
            if (!vehicle[i].getName().equals(checkName)) //assume this method is in a 'Vehicle' class and the name for this object is being accessed through the getName() method
            {
                i++;
            }
        }
        i--; //When running, the if statement is not working how I want it to work, so it continues incrementing 'i' till the value is '3', so this 'i--' decrements the value of 'i' to 2 so there isn't an ArrayIndexOutOfBoundsException exception.
        return "Vehicle's name: "+vehicle[i].getName();
        
    }
    If it is not completely clear, I am trying to make it so that the if statement checks if that particular 'vehicle' has the same name to 'checkName', and if it does not, then the value of 'i' is incremented and the next 'vehicle' is checked, etc.

    The returned String is:
    Code:
    Vehicle's name:
    As you can see, the vehicle's name is being taken from the very last vehicle object, but the name has been set the first vehicle object.

    I don't want to just do something like
    Code:
    vehicle[0].getName()
    because I need the method to check each vehicle object's name.

    Thanks!
    (Sorry if I haven't made it completely understandable)

Check out some similar questions!

Java Question of passing an Array into a method [ 0 Answers ]

Define an integer array ‘arr’ with size 2 in the main method and initialize the two elements in ‘arr’ to be 1 and 2 Write a method ‘swap’ and pass the ‘arr’ into the method, swap two elements of the ‘arr’ in the method Print out all elements in ‘arr’ in the main method System.out.print(arr + ‘ ‘...

Java is not working [ 3 Answers ]

Hi, I cannot get java to work on my laptop. I've tried all the help suggestions. Any ideas would be greatly appreciated. Thanks

Unknown Array Java Error [ 3 Answers ]

I'm having a problem with some java code involving an array and I don't understand the error that appears: CD.java:67: array required, but CDDriver found System.out.println(myCD); I am trying to print out the total of all the prices that are in the array Any help would be great here is...

Java: using an array from the main in a method [ 2 Answers ]

I have the array names in the main of my program and I want to use it in a method FindName.. How would I wright the method header and the call to the method in the main?

Java Error: array required, but PRmList found [ 2 Answers ]

Here is my code: I get the error: Array required, but PRmList found. Any help would be GREATLY appreciated. Thanks! Public class PRm { Private String PRm; Private String movieTitle; Private String movieRating; Private int seats;


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.