-1

I need to create a program that tests whether three integers entered can equal the sides of a triangle. The assignment requires the use of a static method that runs the test and then the main method would call the static method.

If none of the integers entered are greater than or equal to the sum of the other two integers then the system would output "True". Otherwise, it would output "False".

Whenever I input my three numbers and then click run, the program just sits there. It doesn't give me any errors or output anything.

public class TriangularTest1 {

    public static boolean isTriangular (int a, int b, int c)
    {
        return (a + b > c && b + c > a && c + a > b);
        }
    
    public static void main(String args[]) {
        int s1 = StdIn.readInt();
        int s2 = StdIn.readInt();
        int s3 = StdIn.readInt();
        
        if (isTriangular(s1, s2, s3) == true)
            System.out.print("True");
        else
            System.out.print("False");      

    }
}
  • From your description it sounds like you don't input anything into the console which you should, considering you take input via `STDIN` Do you initialize `STDIN` as a scanner somewhere? – 0xff Oct 02 '21 at 17:27
  • Maybe your console is line-buffered, and it doesn't print anything because you used `print` instead of `println`. – kaya3 Oct 02 '21 at 17:27
  • 1
    Have you reduce the problem to a smaller working piece? I mean if you have a program that just takes one integer and outputs it back, does it work? If you program doesn't terminate, then it's quite likely it'd be waiting before calling your function. If `isTriangular` would be called it'd just print and exit, and the exit will flush all buffered output. – memoselyk Oct 02 '21 at 17:29
  • 1
    In any case, we can't reproduce your issue from the code you provided because `StdIn` is not part of the Java class library and you didn't provide it. So we can only guess. Please see [mcve]. – kaya3 Oct 02 '21 at 17:30
  • I'm typing 3 integers into the console when I try to run the program. After I click enter, the program doesn't output anything. – gizmo.java Oct 02 '21 at 18:49

1 Answers1

0

The readInt() method of DataInputStream class in Java is used to read four input bytes and returns a integer value. This method reads the next four bytes from the input stream and interprets it into integer type and returns.

To read-in values from console, you need to use nextInt() with Scanner class.

Sample code:

import java.util.Scanner;
public class TriangularTest1
{
    public static boolean isTriangular (int a, int b, int c)
    {
        return (a + b > c && b + c > a && c + a > b);
    }

    public static void main(String args[])
    {
        Scanner scanner = new Scanner(System.in);  // Create a Scanner object

        int s1 = scanner.nextInt();
        int s2 = scanner.nextInt();
        int s3 = scanner.nextInt();

        if (isTriangular(s1, s2, s3) == true)
            System.out.print("True");
        else
            System.out.print("False");

    }
}

To know more about nextInt() and readInt(), please check below resources:

Md. Faisal Habib
  • 1,014
  • 1
  • 6
  • 14