Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: whats wrong with this one....

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default whats wrong with this one....

    public class Sample {
     
     
        private static int selection = Integer.parseInt(JOptionPane.showInputDialog(
                                       null, "Please Selecti"));;
     
        public static boolean Condition() {
     
            if (selection >= 4) {
     
                JOptionPane.showMessageDialog(null, "There are only three choices!");
                return true;
            }
            else if (selection <= 0) {
     
                JOptionPane.showMessageDialog(null, "You didn't Make A Choice!");
                return true;
            }
            else {              //if i remove this else block the program will ask
                                //for a return statement
                return false;
            }
        }
     
        public void selection() {
     
            switch (selection) {
     
                case 1:
     
                    JOptionPane.showMessageDialog(null, "Chocolate");
                    break;
     
                case 2:
     
                    JOptionPane.showMessageDialog(null, "Vanilla");
                    break;
     
                case 3:
     
                    JOptionPane.showMessageDialog(null, "Banana");
                    break;
            }
        }
     
        public static void main(String[] args) {
     
            do {
     
                Sample a = new Sample();
     
                a.selection();
            }
            while (Condition());
        }
    }

    try to run it in false condition...
    in true condition everything is fine but whats going on in false condition.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: whats wrong with this one....

    If I run this and input 3 the loop will stop because the condition method returns false, and thats what you want isnt it, because the user selected a value (3) which is a valid value between 0 and 4.

    As long as the user puts a zero, negative or a positive value higher than 3 you will keep showing him/her the input box, but if the value is within the right range, return false and the loop stops.

    You need to figure out exactly what you want to happen, design your flow and then you will know how its supposed to work and why it isnt working like you want it to.

    What is your intention with this code?

    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (October 2nd, 2009)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: whats wrong with this one....

    i really owe you a lot sir JSON... actually that program was ryt,this is the right code

    public class ReturnStatementInIfElse_UNCLARIFIED_UNFINISHED {
     
        //IM HAVING SOME PROBLEM IN FALSE CONDITION
        // RUN THE PROGRAM IN FALSE CONDITION
        //YOU WILL SEE THE PROBLEM
        //I DECLARED THE selection as an input variable
        private static int selection;
     
     
        public static boolean Condition() {
     
            if (selection >= 4) {
     
                JOptionPane.showMessageDialog(null, "There are only three choices!");
                return true;
            }
            else if (selection <= 0) {
     
                JOptionPane.showMessageDialog(null, "You didn't Make A Choice!");
                return true;
            }
            else {              //if i remove this else block the program will ask
                                //for a return statement
                return false;
            }
        }
     
        public void selection() {
     
            selection = Integer.parseInt(JOptionPane.showInputDialog(
                                       null, "Please Selecti"));
     
            switch (selection) {
     
                case 1:
     
                    JOptionPane.showMessageDialog(null, "Chocolate");
                    break;
     
                case 2:
     
                    JOptionPane.showMessageDialog(null, "Vanilla");
                    break;
     
                case 3:
     
                    JOptionPane.showMessageDialog(null, "Banana");
                    break;
            }
        }
     
        public static void main(String[] args) {
     
            do {
     
                ReturnStatementInIfElse_UNCLARIFIED_UNFINISHED a = new ReturnStatementInIfElse_UNCLARIFIED_UNFINISHED();
     
                a.selection();
            }
            while (Condition());
        }
    }

    i did this part, the declaration of the class data member selection (at the first part)
        private static int selection = Integer.parseInt(JOptionPane.showInputDialog(
                                       null, "Please Selecti"));


    one of my question is... why is it like that when i declared the data member as an input variable?
    it became like that one , the first code that i posted....

    and i will add another question... what i did is not ryt? or not efficient?

    whaa.. i cant express it more briefly but i hope you understand what im trying to imply.... i hope you understand it..

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: whats wrong with this one....

    and one more thing.. when I run the first code.. and i input the value in false condition...
    the messagebox doesnt stop anymore... and i have to cancel the runtime manualy ... why is it like that?

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: whats wrong with this one....

    in your condition method, you're checking to see if a number is greater or equal to 4 and if a number is less than or equal to 0 and returning true, that means false only occurs when selection is 1,2, or 3.

    It should be the opposite way around:
    if (selection > 0 && selection < 4)
    {
         return true;
    }
    else
    {
         return false;
    }

  7. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: whats wrong with this one....

    the point is i just want to understand why is that error happens with the first code...

  8. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: whats wrong with this one....

    Ok, here's what's wrong with the first code:

    you're initializing the static variable selection, but inside the main while loop, there is never an option to re-choose selection.

    To fix this, ask the user for selection inside the main while-loop, and don't ask them on initializing selection

    private static int selection;

    do
    {
         selection = Integer.parseInt(JOptionPane
    			.showInputDialog(null, "Please Selecti"));
         Sample.selection();
    } while (Sample.condition());

    Also, your original code in the while loop is creating an object to call selection(). Since you've made selection static, however, it's useless to create that variable, you just need to put the static keyword in from of the selection() method and call it statically:

    Sample.selection();

    Special note:

    Since your main method happens to be a static method of Sample, technically you can leave it off because it's understood that you're trying to call another static method from Sample

    public class Sample
    {
         public static void doIt1()
         {
              doIt2();     // equivalent calls here
              Sample.doIt2();
         }
         public static void doIt2(){}
    }

    The same goes with instanced objects of type sample, except it will call any methods (instanced or static) inside that class without the this. or Sample. qualifiers.

  9. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: whats wrong with this one....

    so its fine to create a data member static/or not static and declare it as an input variable ,
    what happens in my program is that my MAIN program which will loop the input cannot repeat it..

    ryt?

  10. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: whats wrong with this one....

    yes on static members, not positive on non-static members, but I think yes there too.

  11. #10
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: whats wrong with this one....

    it will just depends on how where and when will I use that "class"

Similar Threads

  1. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 06:34 AM
  2. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM
  3. [SOLVED] Difference between public and private variable and their uses
    By napenthia in forum Java Theory & Questions
    Replies: 1
    Last Post: April 22nd, 2009, 11:36 AM