Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    MomenT is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default JOptionPane problem

    Can anyone tell me what is wrong with my coding, it state variable result is not initialise?


    import javax.swing.JOptionPane;
    public class Condition
    {
    public static void main (String [] args)
    {
    String numStr, result, again;
    int num=0;
    do
    {
    numStr = JOptionPane.showInputDialog ("Please enter your purchase total: ");
    num = Integer.parseInt(numStr);
    if (num >= 500)
    result = "You are entitled 10% discount.";
    else
    if (num >= 300)
    result = "You are entitled 5% discount.";
    else
    if (num >= 100)
    result = "You are entitled 2% discount.";
    else
    if (num < 100)
    result = "You are not entitled for any discount.";
    else

    JOptionPane.showMessageDialog (null, result);
    again = JOptionPane.showInputDialog ("Do you want to continue (y/n)?");
    }
    while (again.equalsIgnoreCase ("y"));
    }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    First of all, that else block will never be used, since num will always have a value, but, if it were used, it is in that position in which result has not been defined. To fix the problem, remove that last else statement, and change that last else if statement to an else statement.

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

    Default

    ok.. it worked! ^_^ thanks alot..

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

    Default

    I've got another problem.. this time i tried all methods to solve it but the "while expected" keeps showing up when i compile it... -.-

    import javax.swing.JOptionPane;
    public class Condition2
    {
    public static void main (String [] args)
    {
    String numStr, result, again;
    int num=0;
    do
    {
    numStr = JOptionPane.showInputDialog ("Please enter your purchase total: ");
    numStr = JOptionPane.showMessageDialog ("\nOption 1: $500 and above", "Option 2: $300 and above", "Option 3: $100 and above", "Option 4: $99 and below");
    String mark=input.readLine();
    int amt=Integer.parseInt(mark);
    {
    switch (amt)
    {
    case 1:result = "You are entitled 10% discount";break;
    case 2:result = "You are entitled 5% discount";break;
    case 3:result = "You are entitled 2% discount";break;
    default: result = "Not eligible for discount";break;

    JOptionPane.showMessageDialog (null, result);
    again = JOptionPane.showInputDialog ("Do you want to continue (y/n)?");
    }
    while (again.equalsIgnoreCase ("y"));
    }
    }
    }
    }

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Here, your code condensed (and properly indented). Can you see the problem?
    Java Code:
    public class Condition2
    {
        public static void main (String [] args)
        {
            ...
            do
            {
                ...
                int amt=Integer.parseInt(mark); 
                {
                    switch (amt)
                    {
                        ...
                        JOptionPane.showMessageDialog (null, result);
                        again = JOptionPane.showInputDialog ("Do you want to continue (y/n)?");
                    } 
                    while (again.equalsIgnoreCase ("y"));
                }
            }
        }
    }

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

    Default

    switch (amt) ?

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You have your braces all wrong. Why is the display inside the switch? Why is there a block starting after int? The while belongs after the closing brace of the do block, and currently it is after the closing brace of the switch block.

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

    Default

    i see.. i try to to redo it..

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

    Default

    import javax.swing.JOptionPane;
    public class Homework4
    {
    public static void main (String [] args)
    {
    String numStr, result, again;
    int num=0;
    do
    {
    numStr = JOptionPane.showInputDialog ("Please enter the following number: \n1: $500 and above \n2: $300 and above \n3: $100 and above \n4: $99 and below");



    {
    switch (num)
    {
    case 1:result = "You are entitled 10% discount";break;
    case 2:result = "You are entitled 5% discount";break;
    case 3:result = "You are entitled 2% discount";break;
    default: result = "Not eligible for discount";break;

    JOptionPane.showMessageDialog (null, result); // unreachable statement
    again = JOptionPane.showInputDialog ("Do you want to continue (y/n)?");
    }

    }
    }
    while (again.equalsIgnoreCase ("y"));
    }
    }

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

    Default

    somehow i paste the whole coding and it align automatically for me. -.-

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    An opening brace comes at the beginning of every block (i.e. after a do, if, for, switch, while (when not part of a do . while)), and a closing brace comes at that blocks end. So, why are starting a new block in the middle of your do loop, there is no reason for it. Remove that opening brace and the associated close brace.

    You still have the two JOptionPane lines inside the switch block, they should be after it.

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

    Default

    i see.. ok.. i try it..

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

    Default

    Thanks! i can compile it.. but the break; is it compulsory to be there in order for the selection to be correct?

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

    Default

    import javax.swing.JOptionPane;
    public class Homework4
    {
    public static void main (String [] args)
    {
    String numStr, result, again;
    int num=0;
    do
    {
    numStr = JOptionPane.showInputDialog ("Please enter the following number: \n1: $500 and above \n2: $300 and above \n3: $100 and above \n4: $99 and below");
    {
    switch (num)
    {
    case 1:result = "You are entitled 10% discount";
    case 2:result = "You are entitled 5% discount";
    case 3:result = "You are entitled 2% discount";
    default: result = "Not eligible for discount";
    }
    }
    JOptionPane.showMessageDialog (null, result);
    again = JOptionPane.showInputDialog ("Do you want to continue (y/n)?");
    }
    while (again.equalsIgnoreCase ("y"));
    }
    }

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Yes, the break is required. It will compile without it, but it leads to a fallthrough effect when it is not there. I.E. with how it is now, if num 1 the result will be

    You are entitled 10% discountYou are entitled 5% discountYou are entitled 2% discountNot eligible for discount

    When num is 3 the result will be
    You are entitled 2% discountNot eligible for discount

    With the breaks, that won't happen.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    What happens now? Does it compile, execute, give good answers?

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

    Default

    why it works.. but it keeps displaying not eligible for discount.. weird..

    import javax.swing.JOptionPane;
    public class Homework4
    {
    public static void main (String [] args)
    {
    String numStr, result, again;
    int num=0;
    do
    {
    numStr = JOptionPane.showInputDialog ("Please enter the following number: \n1: $500 and above \n2: $300 and above \n3: $100 and above \n4: $99 and below");
    {
    switch (num)
    {
    case 1:result = "You are entitled 10% discount";break;
    case 2:result = "You are entitled 5% discount";break;
    case 3:result = "You are entitled 2% discount";break;
    default: result = "Not eligible for discount";break;
    }
    }
    JOptionPane.showMessageDialog (null, result);
    again = JOptionPane.showInputDialog ("Do you want to continue (y/n)?");
    }
    while (again.equalsIgnoreCase ("y"));
    }
    }

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Did you see the post from masijade? He explained why you need the break statement.

    When you post a response about what your program does, you need to post the FULL TEXT of its output. Dont' edit it!!!

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

    Default

    yes i saw it.. i just added the breaks.. so i have to change the num to 1,2,3,4 ? can't i just type the number in the input field so it will appear as stated in the option..

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    i have to change the num to 1,2,3,4 ? can't i just type the number in the input field so it will appear as stated in the option..
    What you type in is read into the program as a char or a String.
    switch requires ints so you will have to convert it from String to int.
    The switch statement works with individual int values. If you want to be able to test for a range of values, for example > 500 then you will need to use a condition in an if statement:
    if(value >= 500) ...
    That was how you started this thread.

Page 1 of 2 12 LastLast

Similar Threads

  1. JOptionPane
    By Mir in forum New To Java
    Replies: 5
    Last Post: 11-29-2008, 02:04 AM
  2. [SOLVED] On Exit JoptionPane problem
    By Mir in forum New To Java
    Replies: 24
    Last Post: 07-15-2008, 07:39 AM
  3. About JOptionPane.showMessageDialog
    By jhetfield18 in forum AWT / Swing
    Replies: 2
    Last Post: 11-02-2007, 10:45 PM
  4. About JOptionPane.showMessageDialog
    By jhetfield18 in forum Advanced Java
    Replies: 0
    Last Post: 11-02-2007, 10:56 AM
  5. Problem with JOptionPane.showInputDialog()
    By romina in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:33 AM

Posting Permissions

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