-1

I'm a student and I have been tasked to make a program that takes in the three sides of a triangle and outputs the angles of the triangle in reference to the sides. I haven't programmed the equation yet but I have been messing around with the Scanner and "if" statements to start the program. Already I have a problem:

--Here is the output of the beginning part of the program. But that is where it stops. I prompt the user to type a "D" or a "R" and it won't allow the user to type in that spot. However, earlier in the program I was able to prompt the user for a character. Can someone figure out why the previous prompt works and this one does not.--

This is the SSS Triangle program to find the angles of a triangle. Do you know all the sides of a triangle but need to know the angles? (Y/N):Y

What are the lengths of the sides of your triangle? -If all the same length then don't worry about the smallest, medium, and largest- The length of the smallest side: 3 The length of the medium side: 4 The length of the longest side: 5 Would you like the angles in degrees or radians? (D/R):

--Here is the code. The last line is where I am having trouble--

public class SSSTriangle {

    public static Scanner read= new Scanner(System.in);

    public static void main(String[]args) {
        System.out.print("This is the SSS Triangle program to find the angles of a triangle. \n Do you know all the sides of a triangle but need to know the angles? (Y/N):");
        String response= read.nextLine();

        if (response.contains("N")) {
            System.out.println("Okay, have a good day!");
        }

        if (response.contains("Y")) {
            giveMeTheSides();
        }

    }

    public static void giveMeTheSides() {
        System.out.println("\nWhat are the lengths of the sides of your triangle? \n -If all the same length then don't worry about the smallest, medium, and largest-");
        System.out.print("The length of the smallest side: ");
        double a = read.nextDouble();
        System.out.print("The length of the medium side: ");
        double b = read.nextDouble();
        System.out.print("The length of the longest side: ");
        double c = read.nextDouble();

        if (a<=0||b<=0||c<=0) {
            System.out.println("Nice try! Your given sides do not produce a possible triangle.");
        }

        else {

            if ((a+b)<c) {
                System.out.println("Nice try! Your given sides do not produce a possible triangle.");       
            }

                else {

                    System.out.println("Would you like the angles in degrees or radians? (D/R): ");
                    String newResponse= read.nextLine();
Ashutosh
  • 948
  • 12
  • 31

2 Answers2

0

Change the last else statement to read.next() and your code executes. You're simply looking to get a single String response so there is no need to grab the entire line from the Scanner:

else {
       System.out.println("Would you like the angles in degrees or radians? (D/R): ");
       String newResponse = read.next();//Change to read.next()
       System.out.println("Your new response was " + newResponse); //Psuedo code to see if the output is correct. 
       }

Here is your last line of output:

Would you like the angles in degrees or radians? (D/R): 
D

Your new response was D
Simeon Ikudabo
  • 1,933
  • 1
  • 7
  • 17
-1

The issue is that the program actually does read a line and then exits. It is finding something because when you read the last double, the user enters a newline character, but it is never read (because you are only reading the double). To get around this you can simply read in another line (with the extra newline character) after the nextDouble(), in addition to your current nextLine().

System.out.print("The length of the smallest side: ");
double a = read.nextDouble();
System.out.print("The length of the medium side: ");
double b = read.nextDouble();
System.out.print("The length of the longest side: ");
double c = read.nextDouble();
read.nextLine(); // Discard the extra newline

if (a<=0||b<=0||c<=0) {
...
        else {

            System.out.println("Would you like the angles in degrees or radians? (D/R): ");
            String newResponse= read.nextLine();
SupremeSteak1
  • 136
  • 11