Input/Output Problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ttoboobz
    New Member
    • Oct 2006
    • 9

    #1

    Input/Output Problems

    Very new to Programming, need help...

    I need codes that would make a user to try again if they inputed a wrong value...
    the program that I am making will ask the user to enter two positive integer, then the program will compute the greatest common divisor of the two integer. I used JOptionPane as the interface to the user. I already have an idea on how to get the GCD, that my problem is only on How to make the program go back to asking the user if they inputed a wrong value, let us say, a negative integer, or a String, or character. Here's what I have done so far:

    import javax.swing.JOp tionPane;

    public class GreatestCommonD ivisor {

    public static void main(String[] args) {

    String s1 = JOptionPane.sho wInputDialog(nu ll,"Enter an integer","Probl em 1: Greatest Common Divisor Input", JOptionPane.QUE STION_MESSAGE);
    int num1 = Integer.parseIn t(s1);
    String s2 = JOptionPane.sho wInputDialog(nu ll,"Enter another integer","Probl em 1: Greatest Common Divisor Input", JOptionPane.QUE STION_MESSAGE);

    int num2 = Integer.parseIn t(s2);
    int gcd = 1;
    int k = 1;
    int x=num1;
    int divisor[] = new int[num1+1];

    System.out.prin tln("");
    System.out.prin tln("The divisors of "+num1+" that do not divide "+num2+" are:");
    System.out.prin tln("");

    while(x>=1)
    {
    divisor[x] = x;
    if(num1%x==0 && num2%x!=0)
    System.out.prin tln(" "+divisor[x]);
    x--;
    }


    while(k<=num1 && k<=num2)
    {
    if(num1%k==0 && num2%k==0)
    gcd = k;
    k++;
    }

    System.out.prin tln("");
    System.out.prin tln("The GCD of "+num1+" and "+num2+" is "+gcd);
    System.out.prin tln("");
    }

    }
  • pranjalsingh
    New Member
    • Oct 2006
    • 3

    #2
    Originally posted by ttoboobz
    Very new to Programming, need help...

    I need codes that would make a user to try again if they inputed a wrong value...
    the program that I am making will ask the user to enter two positive integer, then the program will compute the greatest common divisor of the two integer. I used JOptionPane as the interface to the user. I already have an idea on how to get the GCD, that my problem is only on How to make the program go back to asking the user if they inputed a wrong value, let us say, a negative integer, or a String, or character. Here's what I have done so far:

    import javax.swing.JOp tionPane;

    public class GreatestCommonD ivisor {

    public static void main(String[] args) {

    String s1 = JOptionPane.sho wInputDialog(nu ll,"Enter an integer","Probl em 1: Greatest Common Divisor Input", JOptionPane.QUE STION_MESSAGE);
    int num1 = Integer.parseIn t(s1);
    String s2 = JOptionPane.sho wInputDialog(nu ll,"Enter another integer","Probl em 1: Greatest Common Divisor Input", JOptionPane.QUE STION_MESSAGE);

    int num2 = Integer.parseIn t(s2);
    int gcd = 1;
    int k = 1;
    int x=num1;
    int divisor[] = new int[num1+1];

    System.out.prin tln("");
    System.out.prin tln("The divisors of "+num1+" that do not divide "+num2+" are:");
    System.out.prin tln("");

    while(x>=1)
    {
    divisor[x] = x;
    if(num1%x==0 && num2%x!=0)
    System.out.prin tln(" "+divisor[x]);
    x--;
    }


    while(k<=num1 && k<=num2)
    {
    if(num1%k==0 && num2%k==0)
    gcd = k;
    k++;
    }

    System.out.prin tln("");
    System.out.prin tln("The GCD of "+num1+" and "+num2+" is "+gcd);
    System.out.prin tln("");
    }

    }


    Hi,
    I've did the changes, now it should work perfectly whatever problem u've. So Check that & enjoy...

    public class GreatestCommonD ivisor {

    public static void main(String[] args) {
    A:
    try
    {
    String s1 = JOptionPane.sho wInputDialog(nu ll,"Enter an integer","Probl em 1: Greatest Common Divisor Input", JOptionPane.QUE STION_MESSAGE);
    int num1 = Integer.parseIn t(s1);
    String s2 = JOptionPane.sho wInputDialog(nu ll,"Enter another integer","Probl em 1: Greatest Common Divisor Input", JOptionPane.QUE STION_MESSAGE);

    int num2 = Integer.parseIn t(s2);
    if(num1<=0 or num2<=0)
    throw new Exception("Exce ption");
    }catch(Exceptio n e)
    {
    continue A;
    }
    int gcd = 1;
    int k = 1;
    int x=num1;
    int divisor[] = new int[num1+1];

    System.out.prin tln("");
    System.out.prin tln("The divisors of "+num1+" that do not divide "+num2+" are:");
    System.out.prin tln("");

    while(x>=1)
    {
    divisor[x] = x;
    if(num1%x==0 && num2%x!=0)
    System.out.prin tln(" "+divisor[x]);
    x--;
    }


    while(k<=num1 && k<=num2)
    {
    if(num1%k==0 && num2%k==0)
    gcd = k;
    k++;
    }

    System.out.prin tln("");
    System.out.prin tln("The GCD of "+num1+" and "+num2+" is "+gcd);
    System.out.prin tln("");
    }

    }

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by ttoboobz
      Very new to Programming, need help...

      I need codes that would make a user to try again if they inputed a wrong value...
      the program that I am making will ask the user to enter two positive integer, then the program will compute the greatest common divisor of the two integer. I used JOptionPane as the interface to the user. I already have an idea on how to get the GCD, that my problem is only on How to make the program go back to asking the user if they inputed a wrong value, let us say, a negative integer, or a String, or character. Here's what I have done so far:

      import javax.swing.JOp tionPane;

      public class GreatestCommonD ivisor {

      public static void main(String[] args) {

      String s1 = JOptionPane.sho wInputDialog(nu ll,"Enter an integer","Probl em 1: Greatest Common Divisor Input", JOptionPane.QUE STION_MESSAGE);
      int num1 = Integer.parseIn t(s1);
      String s2 = JOptionPane.sho wInputDialog(nu ll,"Enter another integer","Probl em 1: Greatest Common Divisor Input", JOptionPane.QUE STION_MESSAGE);

      int num2 = Integer.parseIn t(s2);
      int gcd = 1;
      int k = 1;
      int x=num1;
      int divisor[] = new int[num1+1];

      System.out.prin tln("");
      System.out.prin tln("The divisors of "+num1+" that do not divide "+num2+" are:");
      System.out.prin tln("");

      while(x>=1)
      {
      divisor[x] = x;
      if(num1%x==0 && num2%x!=0)
      System.out.prin tln(" "+divisor[x]);
      x--;
      }


      while(k<=num1 && k<=num2)
      {
      if(num1%k==0 && num2%k==0)
      gcd = k;
      k++;
      }

      System.out.prin tln("");
      System.out.prin tln("The GCD of "+num1+" and "+num2+" is "+gcd);
      System.out.prin tln("");
      }

      }
      Please use code tags next time when posting code.
      You can use exception handling:
      Code:
      boolean valid = false;
      while(!valid) {
      String s1 = JOptionPane.showInputDialog(null,"Enter an intege");
       try {
           int num = Integer.parseInt(s1);
           if(num > 0) {
                valid = true;
           }
       }
       catch(NumberFormatException nFE) {
      
       }
      }

      Comment

      • arivazhaganit
        New Member
        • Oct 2006
        • 9

        #4
        /* new way of Coding with exception for above mentioned problem */

        import javax.swing.JOp tionPane;
        public class GreatestCommonD ivisor {

        public static void main(String[] args) {
        find(getInput(1 ),getInput(2)); }

        static void find(int num1,int num2)
        {
        int gcd = 1;
        int k = 1;
        while(k<=num1 && k<=num2)
        {
        if(num1%k==0 && num2%k==0)
        gcd = k;
        k++;
        }
        JOptionPane.sho wMessageDialog( null,"The GCD of "+num1+" and "+num2+" is "+gcd);
        }

        static int getInput(int cnt)
        {
        int num=0;
        try{
        String s = JOptionPane.sho wInputDialog(nu ll,"Enter Integer "+cnt,"Prob lem 1: Greatest Common Divisor Input", JOptionPane.QUE STION_MESSAGE);
        num = Integer.parseIn t(s);
        if(num<0) throw new Exception("Neag ative Number");
        } catch(Exception e){ JOptionPane.sho wMessageDialog( null,e);return getInput(cnt);}
        return num;
        }
        }

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by arivazhaganit
          /* new way of Coding with exception for above mentioned problem */

          import javax.swing.JOp tionPane;
          public class GreatestCommonD ivisor {

          public static void main(String[] args) {
          find(getInput(1 ),getInput(2)); }

          static void find(int num1,int num2)
          {
          int gcd = 1;
          int k = 1;
          while(k<=num1 && k<=num2)
          {
          if(num1%k==0 && num2%k==0)
          gcd = k;
          k++;
          }
          JOptionPane.sho wMessageDialog( null,"The GCD of "+num1+" and "+num2+" is "+gcd);
          }

          static int getInput(int cnt)
          {
          int num=0;
          try{
          String s = JOptionPane.sho wInputDialog(nu ll,"Enter Integer "+cnt,"Prob lem 1: Greatest Common Divisor Input", JOptionPane.QUE STION_MESSAGE);
          num = Integer.parseIn t(s);
          if(num<0) throw new Exception("Neag ative Number");
          } catch(Exception e){ JOptionPane.sho wMessageDialog( null,e);return getInput(cnt);}
          return num;
          }
          }
          The code tags are where you have the # sign just above the textarea you post in.
          I would not have to make a function just for taking in input for this simple program and I would certainly not use recursion for that input function

          Comment

          • arivazhaganit
            New Member
            • Oct 2006
            • 9

            #6
            hi r035198x ;

            There are some errors in your coding, their is no continue variable in java with out using loop.

            logically it will make problem , whenever you enter a wrong value ,again you have to repeat to get two values instead of giving only a value for wrong one.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by arivazhaganit
              hi r035198x ;

              There are some errors in your coding, their is no continue variable in java with out using loop.

              logically it will make problem , whenever you enter a wrong value ,again you have to repeat to get two values instead of giving only a value for wrong one.
              Sorry but you must be mistaken. I posted my code in reply #3. You will not find a continue there. Personally I always avoid using break and continue if I can help it. You must have mistaken someone else's code for mine.

              Comment

              • arivazhaganit
                New Member
                • Oct 2006
                • 9

                #8
                hi r035198x ...

                I'm very sorry , I mistaken some one coding as your coding , plz forgive me...

                with regards
                Arivu

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by arivazhaganit
                  hi r035198x ...

                  I'm very sorry , I mistaken some one coding as your coding , plz forgive me...

                  with regards
                  Arivu
                  Get over it.
                  We all make mistakes everyday. I was not offended.

                  Comment

                  Working...