5 Replies - 1468 Views - Last Post: 29 November 2011 - 05:23 PM Rate Topic: -----

#1 Th3T3chGuy  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 73
  • Joined: 16-October 11

Isosceles Triangle Code Errors

Posted 29 November 2011 - 05:01 PM

I have to write a method that expects three parameters representing the three sides of a triangle. It returns true if the lengths of the 3 sides result in a isosceles triangle and returns false otherwise.

Here is what I got so far:

import java.util.Scanner;

public class Problem6{
	public static void main (String [] args){
	Scanner reader = new Scanner(System.in);
		
	int sidea, sideb, sidec;
	
	System.out.println("Enter the length of the first side:");
	sidea = reader.nextInt();
	
	System.out.println("Enter the length of the second side:");
	sideb = reader.nextInt();
	
	System.out.println("Enter the length of the third side:");
	sidec = reader.nextInt();
	
	int isoscelesTriangle = isIsosceles(sidea, sideb, sidec);
	
	System.out.println("The area of the square is: " + isoscelesTriangle + " cm^2.");
	}
		
public static int isIsosceles(int sidea, int sideb, int sidec){
	
	int isoscelesTriangle;
		if (sidea == sideb || sideb == sidec || sidea == sidec) && !(sidea == sideb == sidec){	
			System.out.println("True");
		
		else{
			System.out.println("False");
	return isoscelesTriangle;	
		}
		}
	}
}	


These are the erros I get when I compile:

---------- Capture Output ----------
"C:\Program Files\Java\jdk1.7.0\bin\javac.exe" Problem6.java
Problem6.java:26: error: illegal start of expression
		if (sidea == sideb || sideb == sidec || sidea == sidec) && !(sidea == sideb == sidec){	
		                                                        ^
Problem6.java:26: error: ';' expected
		if (sidea == sideb || sideb == sidec || sidea == sidec) && !(sidea == sideb == sidec){	
		                                                                                     ^
Problem6.java:29: error: 'else' without 'if'
		else{
		^
3 errors
Terminated with exit code 1


Is This A Good Question/Topic? 0
  • +

Replies To: Isosceles Triangle Code Errors

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Isosceles Triangle Code Errors

Posted 29 November 2011 - 05:07 PM

This line:

if (sidea == sideb || sideb == sidec || sidea == sidec) && !(sidea == sideb == sidec)

simplified says:

if ( this ) && !(that)

which is not a correct if statement. It should read:

if ( (this) && !(that) )

Fix that and then move to the next error.
Was This Post Helpful? 0
  • +
  • -

#3 Swillis57  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 31
  • Joined: 14-January 11

Re: Isosceles Triangle Code Errors

Posted 29 November 2011 - 05:11 PM

You forgot some parenthesis in the if-statements. Add one '(' before the "sidea" variable in each statement, then add one ')' at the end of the conditionals.

As for the "else" error, there's no curly-brace closing the previous if-block, add one "}" before the else.
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Isosceles Triangle Code Errors

Posted 29 November 2011 - 05:16 PM

Also you are returning a value that has not been initialized: return isoscelesTriangle;
But why you return int? it looks like it should return boolean
Was This Post Helpful? 0
  • +
  • -

#5 Th3T3chGuy  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 73
  • Joined: 16-October 11

Re: Isosceles Triangle Code Errors

Posted 29 November 2011 - 05:18 PM

Ok my revised code looks like this:

import java.util.Scanner;

public class Problem6{
	public static void main (String [] args){
	Scanner reader = new Scanner(System.in);
		
	int sidea, sideb, sidec;
	
	System.out.println("Enter the length of the first side:");
	sidea = reader.nextInt();
	
	System.out.println("Enter the length of the second side:");
	sideb = reader.nextInt();
	
	System.out.println("Enter the length of the third side:");
	sidec = reader.nextInt();
	
	int isoscelesTriangle = isIsosceles(sidea, sideb, sidec);
	
	System.out.println("The area of the square is: " + isoscelesTriangle + " cm^2.");
	}
		
public static int isIsosceles(int sidea, int sideb, int sidec){
	
	int isoscelesTriangle;
		if ((sidea == sideb || sideb == sidec || sidea == sidec) && !(sidea == sideb == sidec)){	
			System.out.println("True");
		
	}else{
			System.out.println("False");
	return isoscelesTriangle;	
		}
		
	}
}	


But now I'm getting a new error:

---------- Capture Output ----------
"C:\Program Files\Java\jdk1.7.0\bin\javac.exe" Problem6.java
Problem6.java:26: error: incomparable types: boolean and int
		if ((sidea == sideb || sideb == sidec || sidea == sidec) && !(sidea == sideb == sidec)){	
		                                                                             ^
1 error
Terminated with exit code 1

Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Isosceles Triangle Code Errors

Posted 29 November 2011 - 05:23 PM

Recheck your if statement. Hint: rephrase a == b == c.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1