• 0

Java Expression Help


Question

I have written the following code so far, the point of it is to get input on the sides of a triangle, if any 2 sides are greater then the 3rd side the statement is valid, if not, invalid. Anyways, it is very basic, I am aware, but he wants very basic as it is an intro class. I cannot get the expression to work, can someone explain or provide help with it?

import java.util.Scanner;
public class TriEdges
{
	public static void main(String[]args)
	{
	 Scanner input = new Scanner(System.in);

	  System.out.print("Enter one of the triangles lenghts as an interger: ");
	  int Length1 = input.nextInt();
	  System.out.println("The first interger is: " + Length1);

	  System.out.print("Enter another one of the triangles lenghts as an interger: ");
	  int Length2 = input.nextInt();
	  System.out.println("The first interger is: " + Length2);

	  System.out.print("Enter the final side of the triangle as an interger: ");
	  int Length3 = input.nextInt();
	  System.out.println("The first interger is: " + Length3);

	 [b] if (Lenth1 + Length2 > Length3) || (Length2 + Length3 > Length1) || (Length1 + Length3 > Length2)[/b]
			  System.out.println("Inputs are valid!");
	  else System.out.println ("The inputs are invalid, please try again.");

	  System.out.print("The perimeter of the triangle is : " + (Length1 + Length2 + Length3));


	}
}

BOLDED is incorrect statement giving the following errors:

TriEdges.java:20: illegal start of expression
	  if (Lenth1 + Length2 > Length3) || (Length2 + Length3 > Length1) || (Length1 + Length3 > Length2)
									  ^
TriEdges.java:20: not a statement
	  if (Lenth1 + Length2 > Length3) || (Length2 + Length3 > Length1) || (Length1 + Length3 > Length2)
																	   ^
2 errors

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Enclose all the conditions in another set of brackets: if ((Lenth1 + Length2 > Length3) || (Length2 + Length3 > Length1) || (Length1 + Length3 > Length2))

You might want to test the program with invalid values such as 100, 50, 20 to see if it works as intended. ;)

Link to comment
Share on other sites

  • 0

When doing what you said I received the following errors:

TriEdges.java:20: cannot find symbol
symbol  : variable Lenth1
location: class TriEdges
	  if ((Lenth1 + Length2 > Length3) || (Length2 + Length3 > Length1) || (Length1 + Length3 > Length2))
		   ^
TriEdges.java:20: operator > cannot be applied to <nulltype>,int
	  if ((Lenth1 + Length2 > Length3) || (Length2 + Length3 > Length1) || (Length1 + Length3 > Length2))
							^
2 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

EDIT: left out a "g" in Length1 on first equation. =)

Link to comment
Share on other sites

  • 0

if ((Lenth1 + Length2 > Length3) || (Length2 + Length3 > Length1) || (Length1 + Length3 > Length2))

try that

When doing what you said I received the following errors:

typo Lenth1

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.