1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

program help

Discussion in 'Java' started by taurus, Dec 3, 2006.

  1. taurus

    taurus New Member

    Joined:
    Dec 3, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    import javax.swing.*;
    public class test
    {
        public static void main(String[] args)
        {
            String input;
            int integer, total, another;
            do
            {
                input = JOptionPane.showInputDialog(null, "Enter a positive integer");
                integer = Integer.parseInt(input);
                if (isPositive(integer));
                {
                input = JOptionPane.showInputDialog(null, "Enter a positive integer");
                }
                total = addNumbers(integer); 
                JOptionPane.showMessageDialog(null,"The total of integers between  1 and " + integer + " is " + total);
                another = JOptionPane.showConfirmDialog(null, "Do you want to continue");
            }
            while (another == JOptionPane.YES_OPTION);
        }
    
    public static boolean isPositive(int integer)
        {
            while(integer <= 0)
            {
            return true;
            }
            return false;
               
        }
    public static int addNumbers(int integer)
        {
            int total = 0, count = 0;
            while(count < integer)
            {
                count = count + 1;
                total = total + count;
            }
            return total;
        }
    }
                
    
    

    Now i got a problem, u c my boolean method, well what i want to say is when its not a positive number then it should re prompt the user for a number(input = JOptionPane.showInputDialog(null, "Enter a positive integer")

    But right now its not workin correctly.
    can someone just help me wit that?
    thanx
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    You have to simply use the continue statement after checking for positive number. Find the rectified code below.

    Code:
    import javax.swing.*;
     public class test
     {
         public static void main(String[] args)
         {
             String input;
             int integer, total, another;
             do
             {
                 input = JOptionPane.showInputDialog(null, "Enter a positive integer");
                 integer = Integer.parseInt(input);
                 if (isPositive(integer))
                     continue;
                 total = addNumbers(integer); 
                 JOptionPane.showMessageDialog(null,"The total of integers between  1 and " + integer + " is " + total);
                 another = JOptionPane.showConfirmDialog(null, "Do you want to continue");
             }
             while (another == JOptionPane.YES_OPTION);
         }
     
     public static boolean isPositive(int integer)
     {
         while(integer <= 0)
         {
         return true;
         }
         return false;
            
     }
     
     public static int addNumbers(int integer)
     {
         int total = 0, count = 0;
         while(count < integer)
         {
             count = count + 1;
             total = total + count;
         }
         return total;
     }
     
     }
     
  3. taurus

    taurus New Member

    Joined:
    Dec 3, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    thanks man, helped alot.
     
  4. taurus

    taurus New Member

    Joined:
    Dec 3, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    problem, its saying: variable another might not have been initialized but it is. Wat do i do?
     
  5. taurus

    taurus New Member

    Joined:
    Dec 3, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    i need some help, its not workin.... HELP... it just doesnt want to do wat i ask it to do????
     
  6. taurus

    taurus New Member

    Joined:
    Dec 3, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    import javax.swing.*;
    public class test
    {
        public static void main(String[] args)
        {
            String input;
            int integer, total, another;
            
            do
            {     
                input = JOptionPane.showInputDialog(null, "Enter a positive integer");
                integer = Integer.parseInt(input);
               
                if (isPositive(integer))
                    input = JOptionPane.showInputDialog(null, "Enter a positive integer"); 
                
                total = addNumbers(integer);
                JOptionPane.showMessageDialog(null,"The total of integers between  1 and " + integer + " is " + total);
                another = JOptionPane.showConfirmDialog(null,"Do you want to contine");
    
            }
            while (another == JOptionPane.YES_OPTION);
        }
    
    public static boolean isPositive(int integer)
        {
            if (integer < 0)
    		      return true;
    		else 
    		      return false;
       
        
        }
    public static int addNumbers(int integer)
        {
            int total = 0, count = 0;
            while(count < integer)
            {
                count = count + 1;
                total = total + count;
            }
            return total;
        }
    }
                
    
    iv edited it to the following code. it works to a certain extent. IE. i enter a positive it does the calculation. then i enter a negative it asks to re input the number. then i type another negative but now it does the calcualtion with the first negative somehow.
    HOW DO I STOP THAT??
    or any other ideas.
    thanks
     
  7. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    But I already gave you the solution, didn't you try that?
     
  8. taurus

    taurus New Member

    Joined:
    Dec 3, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    it said variable already may not have been initialized??
     
  9. taurus

    taurus New Member

    Joined:
    Dec 3, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    ??? wats wrong wit it?
     
  10. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    With the continue statment you can send it to the beginning of the loop everytime a specific condition is not satisfied, in your case a positive integer.
     

Share This Page