2

Ok, so basically, I linked all my methods up, everything is looking good (through my nooby eyes). I have a loop and question whether the user wants to continue. I initially had a if else statement based on a y/n input. If n is entered, I use a break; statement. However, I initialize the scanner and the print statement in the line where I comment //THIS DOESN'T WORK. Why won't it let me enter something once I type that?

Main Class


import java.util.Scanner;
public class Triangle
{
    public static void main (String[]args)  
    {
        Scanner input = new Scanner(System.in);
        int x, y, z;
        for (int loop = 1; loop <= Integer.MAX_VALUE; loop++)
        {
            System.out.println("Please enter the 3 sides of the triangle.");
            x = input.nextInt();
            y = input.nextInt();
            z = input.nextInt();
            boolean goAgain;
            String userInput;

            TriMethods triTest = new TriMethods(x, y, z);

            System.out.println("You entered a triangle with the sides " + triTest.toString() + ": ");

            triTest.isEqual();
            triTest.isIsos();
            triTest.isScal();

            System.out.println("Would you like to go again? y/n");

            String answer = userInput.nextLine(); //THIS DOESN'T WORK

        }
    }
}

Second Class


public class TriMethods {

    int side1;
    int side2;
    int side3;

    public TriMethods(int x, int y, int z)
    {
        side1 = x;
        side2 = y;
        side3 = z;
    }

    public int longSide()
    {
        int longSide = side1;

        if (side2 >= longSide && side2 >= side3)
        {
            longSide = side2;
        }
        else if (side3 >= side2 && side3 >= side2)
        {
            longSide = side3;
        }
        return longSide;
    }

    public void shortSide()
    {
        int shortSide = side1;

        if (side2 <= shortSide && side2 <= side3)
        {
            shortSide = side2;
        }
        else if (side3 <= side2 && side3 <= side2)
        {
            shortSide = side3;
        }

        System.out.println("The long side is " + shortSide);
    }

    public void isEqual()
    {
        if (side1 == side2 && side1 == side3 && side2 == side3)
        {
            System.out.println("This triangle is equalateral");     
        }
    }

    public void isIsos()
    {
        if (side1 == side2 && side1 != side3)
        {
            System.out.println("This triangle is isosceles");
        }
        else if (side1 == side3 && side1 != side2)
        {
            System.out.println("This triangle is isosceles");
        }
        else if (side2 == side1 && side2 != side3)
        {
            System.out.println("This triangle is isosceles");
        }
        else if (side2 == side3 && side2 != side1)
        {
            System.out.println("This triangle is isosceles");
        }
    }

    public void isScal()
    {
        if (side1 != side2 && side1 != side3 && side2 != side3)
        {
            System.out.println("This triangle is scalene");
        }
    }

    public String toString()
    {
        return (side1 + " " + side2 + " " + side3);
    }

}
Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
CuriousFellow
  • 225
  • 1
  • 3
  • 14

2 Answers2

3

try using input.nextLine(); after getting sides of the triangle, this will consume new line when you press enter.

try

public class Triangle
{
    public static void main (String[]args)

    {

        int x, y, z;
        String proceed="";
        do
        {
             Scanner input = new Scanner(System.in);
        System.out.println("Please enter the 3 sides of the triangle.");

        x = input.nextInt();
        y = input.nextInt();
        z = input.nextInt();
        boolean goAgain;
        String userInput;

        TriMethods triTest = new TriMethods(x, y, z);

        System.out.println("You entered a triangle with the sides " + triTest.toString() + ": ");

        triTest.isEqual();
        triTest.isIsos();
        triTest.isScal();
        input.nextLine();
        System.out.println("Would you like to go again? y/n");

        proceed = input.nextLine(); //THIS DOESN'T WORK

        }while (!proceed.equalsIgnoreCase("n"));
    }
}
upog
  • 4,965
  • 8
  • 42
  • 81
1

Basically your string userInput doesn't have a value. May be you want to get value into this using Scanner

Crickcoder
  • 2,135
  • 4
  • 22
  • 36