I'm trying to figure out how to use JOptionPane to determine which deduction someone will receive. S - Single and M - Married. I just need to use the first letter of the input though.

public double calcinsDeduct()
    {
        double insDeduct;

        if (status.startsWith(s))

            insDeduct = 50.00;

        else if (status.startsWith(m))

            insDeduct = 100.00;

        return insDeduct;
    }

Recommended Answers

All 4 Replies

Here's my Example

public double calcinsDeduct()
	{
      String cmdString =
        JOptionPane.showInputDialog("M-Male or F-Female: ");
 
        if(cmdString == ("M")){
          JOptionPane.showMessageDialog(null,"50.00 - deducted"); //shows the message...you can edit this 
	 insDeduct = 50.00;
	}
       else if(cmdString == ("F")){
          JOptionPane.showMessageDialog(null,"100.00 - deducted");
	 insDeduct = 100.00;
	}
	else{
          JOptionPane.showMessageDialog(null, "enter either M or F");
	}
	return insDeduct;  
	}

you should specify how the JOptionPane should look or work like

check here for more examples
http://http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html#features

nope ...
very bad code, and wouldn't always work.
it looks correct, but, when it looks like a duck, and it cuacks like a duck, it could (in cases like this) also be a swann.
it's the 'if-expression' where you're going wrong.
you're not supposed to check equality of two String objects (which is what you are doing) with '==' but by using the .equals() method.
'==' will check whether or not the value is using the exact same space in your memory, not whether the values are equal.

what you want to do is:

if ( cmdString.equals("M") )
...
if ( cmdString.equals("F") )

if he wants both 'M' and 'm' to be correct, he'll just need to adjust it to

if ( cmdString.equalsIgnoreCase("m") )
...
if ( cmdString.equalsIgnoreCase("f") )
...

if, on the other hand, he actually wants to compare chars, and both upper and lower case, what he could do is:

String checkFirstChar = cmdString.toLowerCase();
if ( checkFirstChar.charAt(0) == 'm' )
...
if ( checkFirstChar.charAt(0) == 'f' )
...

@stultuske thanks for correcting my code anyway I forgot to remark it as incomplete and
rushed.I just wanted to show here how a part of how JOptionpane works.

Sorry about that I should have posted a more "friendly" solution rather than give branflake a new problem along the way ...my bad :p

the problem is some won't look further into it and just implement it "as is"
giving hints as too where they went wrong 'll be more likely to persuade him to look something up for himself :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.