Home > Back-end >  Why is my program printing two statements including of my previously entered number?
Why is my program printing two statements including of my previously entered number?

Time:10-22

'So question was to find pytho triplet using the number inputted but number has to be greater than one and natural.

The error I am facing is that suppose I enter -4, then it runs method again and asks me to enter number again and I enter suppose 9 but while printing final statement, it prints triplets of both -4 and 9. How can I fix it as I only want triplet of 9 to be printed?

import java.util.*;
public class Pythagorean_Triplet
{
    private static int a;
    public static void main(String[]args) //main method 
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter number to find Pythagorean Triplet : ");
        int a=sc.nextInt();
        if(a<2) //our program only works on natural numbers greater than 1
        {
            System.out.println("");
            System.out.println("Your number is not a natural number or is 1");
            System.out.println();
            main(args);
        }
        double p1= 2*a;
        double p2=Math.pow(a,2) - 1; 
        double p3=Math.pow(a,2)   1;  
        System.out.println("");
        System.out.println(p1   ", "   p2   " and "   p3   " form a 'Pythagorean Triplet'");
    }
}

CodePudding user response:

You are calling the main method again (recursive call) so after you entered "9" the main(args); call returns and print the output for the first input.

So you should ask for a new number until you get a correct input.

import java.util.Scanner;

public class PythagoreanTriplet {
    public static void main(String[]args) //main method
    {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter number to find Pythagorean Triplet : ");
        int a = sc.nextInt();
        while(a<2) //our program only works on natural numbers greater than 1
        {
            System.out.println("");
            System.out.println("Your number is not a natural number or is 1");
            System.out.println();

            System.out.println("Please retry with a number greater than 1 : ");
            a = sc.nextInt();
        }
        double p1= 2*a;
        double p2=Math.pow(a,2) - 1;
        double p3=Math.pow(a,2)   1;
        System.out.println("");
        System.out.println(p1   ", "   p2   " and "   p3   " form a 'Pythagorean Triplet'");
    }
}

CodePudding user response:

Calling a method doesn't stop the current method from executing. When the method you call completes, the current method continues.

For example, when you do this:

System.out.println();

You are calling a method. That method performs its logic, completes, and then this code continues on to the next statement. So when you do this:

main();

The same exact thing happens. The method performs its logic (in this case accepts your new input and produces output), completes, and then this code continues on to the next statement. That next logic is to produce the rest of the output.


Basically, recursion is the wrong tool for this job. Instead, loop over reading the input until correct input is received. Perhaps something like this:

Scanner sc = new Scanner(System.in);
int a = 0;
while (a < 2) {
    // your loop logic
}

Notice how a is initialized to something that will cause us to enter the loop. Then in the loop is the operation we want to repeat, where we will continually ask for input until a is a valid value:

Scanner sc = new Scanner(System.in);
int a = 0;
while (a < 2) {
    System.out.println("Enter number to find Pythagorean Triplet : ");
    a = sc.nextInt();
    if (a < 2) {
        System.out.println("");
        System.out.println("Your number is not a natural number or is 1");
        System.out.println();
    }
}

There are different ways to structure it. In this approach I've repeated the a < 2 logic. Another approach might ask outside the loop then re-ask within the loop, repeating the nextInt() logic. How you structure it generally comes down to the UX you want in the prompts and personal preference on how to organize the logic.

But overall the point here is that you want a loop, not recursion.

CodePudding user response:

Since none of the above answers are accepted as the answer to your problem, i guess you actually want to work with recursion. Following is a representation of what your program currently does with the your example inputs.

enter image description here

what we need to do now, is to stop the first 'main()' call from printing its output. to achive that, we can simply 'return' after calling 'main()' the second time, or put the block of code which prints the output into the 'else' block of your if statement. your program would then look something like this:

using 'return' to stop wrong outputs

import java.util.*;
public class Pythagorean_Triplet
{
    private static int a;
    public static void main(String[]args) //main method 
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter number to find Pythagorean Triplet : ");
        int a=sc.nextInt();
        if(a<2) //our program only works on natural numbers greater than 1
        {
            System.out.println("");
            System.out.println("Your number is not a natural number or is 1");
            System.out.println();
            main(args);
            return;
        }
        double p1= 2*a;
        double p2=Math.pow(a,2) - 1; 
        double p3=Math.pow(a,2)   1;  
        System.out.println("");
        System.out.println(p1   ", "   p2   " and "   p3   " form a 'Pythagorean Triplet'");
    }
}

using 'else' to stop wrong outputs

import java.util.*;
public class Pythagorean_Triplet
{
    private static int a;
    public static void main(String[]args) //main method 
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter number to find Pythagorean Triplet : ");
        int a=sc.nextInt();
        if(a<2) //our program only works on natural numbers greater than 1
        {
            System.out.println("");
            System.out.println("Your number is not a natural number or is 1");
            System.out.println();
            main(args);
        } else {
            double p1= 2*a;
            double p2=Math.pow(a,2) - 1; 
            double p3=Math.pow(a,2)   1;  
            System.out.println("");
            System.out.println(p1   ", "   p2   " and "   p3   " form a 'Pythagorean Triplet'");
        }
    }
}
  •  Tags:  
  • java
  • Related