如何将此字符串与我的数组进行比较?

时间:2017-03-28 06:07:00

标签: java arrays

我这里有一个小程序,主要是通过用户输入输入服务,然后将其与并行数组相关联,以确定服务的价格。我在比较第一个String(小)和String数组(subs)时遇到了麻烦,它应该输入前3个字符。我不知道为什么它不起作用,但据我所知,第一个for循环是正确设置的。即使我只输入类似“油”的东西,它也会把我带到“抱歉 - 无效服务”行....

import java.util.Arrays;

import javax.swing.JOptionPane;

public class CarCareChoice2
{
    public static void main(String[] args)
    {
        String[] services = {"oil change", "tire rotation", "battery check",    "brake inspection"};
        int[] prices = {25, 22, 15, 5};
        String[] subs = {"oil, tir, bat, bra"};
        String ordered;
        boolean choice = false;
        boolean choice2 = false;
        String strserv;
        int cost = 0;
        strserv = JOptionPane.showInputDialog(null, "Enter the service you'd like today."
            + "\n 1: oil change" + "\n2: tire rotation" +
              "\n3: battery check" + "\n4: brake inspection");
        String small = strserv.substring(0,3);

        for (int x = 0; x < subs.length; ++x)
        {
            if(subs.equals(small))
            {
                choice = true;
                cost = prices[x];
            }
        }

        for (int x = 0; x < services.length; ++x)
        {
            if(strserv.length() == services[x].length())
            {
                choice = true;
                cost = prices[x];

                //  System.out.println(" " + strserv.charAt(0));
                //  System.out.println(" " + strserv.substring(0, 3));
                //  String data = strserv.substring(0, 3);
            }
        }

        if(choice)
            JOptionPane.showMessageDialog(null, "The price for "
                + strserv + " is $" + cost);
        else
            JOptionPane.showMessageDialog(null, 
                "Sorry - invalid service");
    }
}

3 个答案:

答案 0 :(得分:1)

问题在于:

for (int x = 0; x < subs.length; ++x)
{
    if(subs.equals(small)) //<--- subs and small can never be equal!
    {
        choice = true;
        cost = prices[x];
    }
}

由于你有一个循环,它将x从0循环到子数组的最后一个索引,你应该使用x。检查索引x处的元素是否等于small

if(subs[x].equals(small))

如果它们相同,则您不再需要继续循环,因此我建议您在此处添加break。整件事情都是这样的:

for (int x = 0; x < subs.length; ++x)
{
    if(subs[x].equals(small)) //<--- subs and small can never be equal!
    {
        choice = true;
        cost = prices[x];
        break;
    }
}

或者,您可以使用此方法快速了解元素的索引:

int indexOfChoice = Arrays.asList(subs).indexOf(small);
if (indexOfChoice != -1) { // it returns -1 if not found
    choice = true
    cost = prices[indexOfChoice];
}

编辑:

我刚注意到你的数组被错误地声明了:

String[] subs = {"oil, tir, bat, bra"};

应该是:

String[] subs = {"oil", "tir", "bat", "bra"};

完整代码:

    String[] services = {"oil change", "tire rotation", "battery check",    "brake inspection"};
    int[] prices = {25, 22, 15, 5};
    String[] subs = {"oil", "tir", "bat", "bra"};
    String ordered;
    boolean choice = false;
    boolean choice2 = false;
    String strserv;
    int cost = 0;
    strserv = JOptionPane.showInputDialog(null, "Enter the service you'd like today."
            + "\n 1: oil change" + "\n2: tire rotation" +
            "\n3: battery check" + "\n4: brake inspection");
    String small = strserv.substring(0,3);

    int indexOfChoice = Arrays.asList(subs).indexOf(small);
    if (indexOfChoice != -1) { // it returns -1 if not found
        choice = true;
        cost = prices[indexOfChoice];
    }

    for (int x = 0; x < services.length; ++x)
    {
        if(strserv.length() == services[x].length())
        {
            choice = true;
            cost = prices[x];

            //  System.out.println(" " + strserv.charAt(0));
            //  System.out.println(" " + strserv.substring(0, 3));
            //  String data = strserv.substring(0, 3);
        }
    }

    if(choice)
        JOptionPane.showMessageDialog(null, "The price for "
                + strserv + " is $" + cost);
    else
        JOptionPane.showMessageDialog(null,
                "Sorry - invalid service");
}

答案 1 :(得分:0)

你可能打算这样做:

    if(subs[x].equals(small))

答案 2 :(得分:0)

除了Maurice已经编写的内容之外,您必须使用

访问数组的单个元素
subs[x].equals(small)

在您的比较中,由于您对subs数组的定义,您的代码仍然无效。如果仔细观察,即使它是一系列字符串,你仍可以看到,它仍然只包含一个元素。

你写了

String[] subs = {"oil, tir, bat, bra"}

在这个数组中你只有一个字符串,即&#34; oil,tir,bat,bra&#34;。

你可能想写的是

String[] subs = {"oil", "tir", "bat", "bra"}

请注意,每个元素都必须用双引号括起来。经过测试,它应该可以工作。

除此之外,您可以考虑通过对服务数组中的每个元素进行子字符串编程来编程创建子数组。