-2

I have a problem with the arrayList. this is my codes for the arrayList

I know there are some problems with my arrayList method but i cant spot it.

method for the arrayList

public ArrayList<Tour> countryList(String country)
    {
        ArrayList<Tour> newList = null;
        switch (country)
        {
        case "Indonesia":
            country = "IN";
            break;
        case "Thailand":
            country = "TH";
            break;
        case "Singapore":
            country = "SI";
            break;
        case "Brunei":
            country = "BR";
            break;
        case "Philipines":
            country = "PH";
            break;
        case "Vietnam":
            country = "VI";
            break;
        case "Laos":
            country = "LA";
            break;
        case "Myammmar":
            country = "MY";
            break;
        case "Malaysia":
            country = "MS";
            break;
        }
        if(TList.indexOf(country)!= 1)
        {
            return newList;
        }

        return newList;

    }

in the main class

private static void displayAllToursCountry()
    {
        System.out.print("Please enter the name of the country: ");
        String country = sc.next();

        while (country == "")
        {
            System.out.print("Please enter a valid country name: ");
            country = sc.next();
        }
        System.out.println("Country:" + country);
        System.out.println(ta1.countryList(country));
    }

why are there no values coming out? where did i did wrongly? Thanks

Noah Skull Weijian
  • 129
  • 1
  • 2
  • 8
  • 1
    Please clarify for us exactly how your code is misbehaving. What do you mean by "no values coming out"? What exactly is happening? Also, don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *objects* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Mar 13 '15 at 15:18
  • 3
    `while (country == "")`== bad. [How to compare Strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – kolossus Mar 13 '15 at 15:18
  • Indeed, use `while("".equals(country))` – EpicPandaForce Mar 13 '15 at 15:26
  • The body of your method can be replaced by `return null;` – David Conrad Mar 13 '15 at 15:31

1 Answers1

7

You're not putting anything into the ArrayList.

You're just doing

ArrayList<Tour> newList = null;

and then return newList;

It isn't even initialized.

You should initialize it: ArrayList<Tour> newList = new ArrayList<>() and then put some elements in: newList.add(myTour);

Malt
  • 28,965
  • 9
  • 65
  • 105