简体   繁体   中英

How do I compare this string to my array?

I have a small program here that essentially takes a service entered via user input and then relates it to a parallel array to determine a price for the service. I'm having trouble comparing the first String (small) to the String array (subs) which is supposed to take the first 3 characters entered. I don't know why it's not working, but to my knowledge, the first for loop is set up correctly. Even if I just type something like "oil", it will take me to the "Sorry - invalid service" line....

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");
    }
}

The problem is here:

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

Since you have a loop that loops x from 0 to the last index of the subs array, you should make use of x . Check to see if the element at index x is equal to small :

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

If they are equal, you don't need to continue looping anymore, so I suggest you add a break here. The whole thing will be like this:

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;
    }
}

Alternatively, you can use this method to quickly know the index of an element:

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

EDIT:

I just noticed that your array is declared wrongly:

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

It should be:

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

Full code here:

    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");
}

您可能打算这样做:

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

Besides from what Maurice already wrote, that you have to access the individual element of the array using

subs[x].equals(small)

in your comparison, your code will still not work due to your definition of the subs array. If you look closely, you can see even though it's an array of Strings, it's still holding only one element.

You wrote

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

In this array you only have one single string, being "oil, tir, bat, bra".

What you probably meant to write was

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

Note that every element has to be enclosed in double quotes. Tested this and it should work.

Besides from that you might consider creating the subs array programmatically by substringing every element in you services array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM