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 4 of 4

Thread: Help about the While Loop

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help about the While Loop

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.StringTokenizer;
     
    public class GCDCalculator2 {
      public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter two integers separated by space:");
        String s = br.readLine();
        StringTokenizer st = new StringTokenizer(s);
        int K= Integer.parseInt(st.nextToken());
        int N= Integer.parseInt(st.nextToken());
        while (K != 0){
        int R = K % N;
        K = N;
        N = R;
       	System.out.println(" Greatest Common Divisor is " + K);
        br.close();
        }
      }
    }

    Why my output is this
    Enter two integers separated by space:
    34 68
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at GCDCalculator2.main(GCDCalculator2.java:16)

    Greatest Common Divisor is 68
    Greatest Common Divisor is 34


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,410
    My Mood
    Mellow
    Thanks
    85
    Thanked 473 Times in 464 Posts

    Default Re: Help about the While Loop

    What is happening and why is fairly obvious in this simple program, so I'm not really sure what you're asking for help with other than some hand holding that you really don't need. I suggest you add some print statements to see what is going on. Using print statements is a basic technique to debug or troubleshoot code. Add a print to determine the initial values of the variables K and N, and then another print or two inside the while loop to see what is then happening to K, N, and R.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help about the While Loop

    Thanks for the suggestion i am a beginner i am asking how can i remove the exception error in my program Thanks

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,410
    My Mood
    Mellow
    Thanks
    85
    Thanked 473 Times in 464 Posts

    Default Re: Help about the While Loop

    To remove errors:

    1. Understand them: You already know that dividing by zero, "/ by 0", is bad.

    2. Find the source of them: The error message points to the source of the error: "at GCDCalculator2.main(GCDCalculator2.java:16)". The number '16' is a line number.

    There may be additional steps to understanding and finding, like "Where is division occurring?", "How can that divisor be zero?", etc.

    3. Eliminate the source of them/eliminate them: In this case, make sure that the divisor is never zero. Sometimes "protecting" is used in place of "eliminating." If that approach was chosen in this case, I'd rephrase the first sentence to: Make sure that the statement causing the error is not executed IF the divisor is zero.

    You can do this on your own, and it's an important, even vital, skill to learn. You need to learn how to debug and fix a simple program like this one so that you can then scale those skills as the complexity of your programs increases. Having someone tell you how to fix errors is not the same as learning how to fix them.

    Get to work.

Similar Threads

  1. help with when the for loop is met and i want to run the while loop again
    By m49er704 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 22nd, 2013, 09:03 AM
  2. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 10:22 PM
  4. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 07:57 PM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM

Tags for this Thread