Results 1 to 10 of 10
  1. #1
    MomenT is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default If-Else statement problem

    import javax.swing.*;
    public class PracticeWS04
    {
    public static void main (String [] args)
    {
    String input1, result, again;
    int sales=0;
    do
    {

    input1=JOptionPane.showInputDialog ("Please enter your annual total of sales:");
    sales=Integer.parseInt(input1);

    if (sales < 50000)result = "No bonus.";
    else
    if (sales >= 50000)result = "10% bonus for you.";
    else
    if (sales < 100000)result = "10% bonus for you.";
    else
    if (sales >= 100000)result = "20% bonus for you.";

    JOptionPane.showMessageDialog (null, result);
    again = JOptionPane.showInputDialog ("Do you wish to continue (Y/N)");
    }
    while (again.equalsIgnoreCase ("Y"));
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    do you have a question?

  3. #3
    MomenT is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default

    yes..

    JOptionPane.showMessageDialog (null, result);

    had error and i dun get it.. i check through.. it shouldnt be a problem.. i am stuck at there

  4. #4
    MomenT is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default

    any help available?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    had error and i dun get it
    What error? did it compile? throw an error when running?? If there's an error message, reading it carefully and using the information to correct things will often help quite a bit.

    The bottom line here is usually the more thought and effort you put into creating your question, the better your chances are of a volunteer here taking the time and effort to consider it and give you a helpful answer. You would benefit greatly by reading this link (one I try to re-study at least every other week as it helps me):
    How To Ask Questions The Smart Way
    Last edited by Fubarable; 10-23-2008 at 08:29 AM.

  6. #6
    MomenT is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default

    ok.. when i compile the coding using BlueJ, the JOptionPane.showMessageDialog line had an error message "variable result might not have been initialized"

    from the coding, I did string the 'result'

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    so,... initialize it. problem fixed.

  8. #8
    MomenT is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default

    alright.. i try it..

  9. #9
    MomenT is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default

    import javax.swing.*;
    public class PracticeWS04
    {
    public static void main (String [] args)
    {
    String input1, result, again;
    int sale;
    do
    {

    input1=JOptionPane.showInputDialog ("Please enter your annual total of sales:");
    sale=Integer.parseInt(input1);

    if (sale < 50000)result = "No bonus.";
    else
    if (sale >= 50000)result = "Bonus! 10% of sales!";
    else
    if (sale < 100000)result = "Bonus! 10% of sales!";
    else
    result = "Bonus! 20% of sales!";

    JOptionPane.showMessageDialog (null, result);
    again = JOptionPane.showInputDialog ("Do you wish to continue? (Y/N)");
    }
    while (again.equalsIgnoreCase ("Y"));
    }
    }


    The above code has been changed, I can compile it but the results is not what I had expect.

    if (sale < 100000)result = "Bonus! 10% of sales!";
    else
    result = "Bonus! 20% of sales!";

    With this statement, when i key in 100000 i get 10% of sales instead of 20% of sales. Is there anything i miss again?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your if logic is wrong. To see where, simplify your code (always a good debugging technique -- you should use it). try this and you will see:
    Java Code:
    public class Fubar2
    {
      public static void main(String[] args)
      {
        System.out.printf("%7s: %s\n", "sale", "result");
        String result = "";
        for (int i = 0; i < 20; i++)
        {
          int sale = i * 10000;
    
          if (sale < 50000)
            result = "< 50,000";
          else if (sale >= 50000)
            result = ">= 50,000";
          else if (sale < 100000)
            result = "< 100,000";
          else
            result = "else";
          
          System.out.printf("%7d: %s\n", sale, result);
        }
      }
    
    }
    Also, when posting code, please use code tags so your code will be readable. To do this, highlight your code and press the code button.

Similar Threads

  1. ForLoop statement problem
    By MomenT in forum New To Java
    Replies: 4
    Last Post: 10-01-2008, 02:01 PM
  2. Please help me with this Problem Statement
    By KMS in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 09:44 PM
  3. Beginner's Problem on Loop/If statement
    By obdi in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 02:41 AM
  4. Problem with a switch statement in Java
    By baltimore in forum New To Java
    Replies: 2
    Last Post: 08-02-2007, 05:43 AM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •