0

I'm new to java and I'm banging my head against a wall with a task. I need to get this to work. Can anyone tell me where I went wrong? I need to write an application for Carl’s Carpentry that shows a user a list of available items: table, desk, dresser, or entertainment center. Allow the user to enter a string that corresponds to one of the options, and display the price as $250, $325, $420, or $600, accordingly. Display an error message if the user enters an invalid item. The program MUST contain parallel arrays.

import javax.swing.*;
public class CarpentryChoice
{
    public static void main(String[] args)
    {
        String entry;
        String [] item = {"table","desk","dresser","entertainment center"};
        int [] price = {250, 325, 420, 600};
        String strPiece;
        int x, fi = 99;
        String prompt = "Please select an item\n" +
        "Our furniture is:\n" + "Table\n" +
        "Desk\n" +
        "Dresser\n" +
        "Entertainment center\n" +
        "Enter an item letter";
        entry = JOptionPane.showInputDialog(null, prompt);
        entry = strPiece.ToString();
            for(x = 0; x < item.length; ++x)
            if(strPiece == item[x])
                fi = x;
                if(fi == 99)
                    JOptionPane.showMessageDialog(null,
                    "Invalid item code entered");
                else
                {
                    if (fi > 2)
                        fi = fi - 3;
                        JOptionPane.showMessageDialog(null, "Furniture item " +
                        strPiece + " is priced at $" +
                        price[fi]);
                }
                System.exit(0);
    } 
}

Any help is MUCH appreciated!!

Thanks!

M A
  • 71,713
  • 13
  • 134
  • 174
haldav
  • 127
  • 1
  • 13

1 Answers1

0
  1. It is toString instead of ToString.

  2. Why are you assigning entry twice? The second assignment will override what the user input via the prompt dialog. You don't need the variable strPiece in this case. You can just use entry:

    entry = JOptionPane.showInputDialog(null, prompt);            
        for(x = 0; x < item.length; ++x)
    
  3. To compare Strings, use the equals instead of the == operator:

    if(entry.equals(item[x]))

And in the part that displays the result dialog, use entry again instead of strPiece (simply remove strPiece from the entire code):

JOptionPane.showMessageDialog(null, "Furniture item " +
                    entry + " is priced at $" +
                    price[fi]);
Community
  • 1
  • 1
M A
  • 71,713
  • 13
  • 134
  • 174
  • You could add, that his code misses a lot of braces (`{`, `}`). `The second assignment will override` ... more likely will it throw a `NullPointerException`. – Tom Oct 23 '14 at 19:51
  • 1
    @Tom I may be wrong, but I think it is just incorrect indenting. – Stephen Buttolph Oct 23 '14 at 19:53
  • @StephenB .. maybe. Let's hope so. – Tom Oct 23 '14 at 19:54
  • Okay. Thanks. I made the changes and now I'm getting a weird output. It says "Furniture item [Ljava.lang.string;@5ce65a89 is priced at $250". The characters after the ampersand are always different. The price is correct, but, obviously the Ljava part is not. What did I do incorrectly here? – haldav Oct 23 '14 at 20:16
  • @haldav updated the answer. I think it's related to the last part where you display the result. – M A Oct 23 '14 at 20:29
  • Interestingly enough, manouti, right after I posted my reply, I saw the change I needed to make on my own and when came back to post an update you had already posted. That's EXACTLY what I did to fix it. Thanks a bunch! It's always the obvious things! – haldav Oct 23 '14 at 20:33