Jump to content

adding strings/elements

- - - - -

  • Please log in to reply
2 replies to this topic

#1
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts
I need to check if one element in my array is larger than another, also if one is less than. how do i do this?
this is what i've got:


import B102.*;


public class Triangle {


	public static void main(String[] args) {

		Triangle T = new Triangle();

		T.print();

		T.calc();

		

	}


	public void calc() {

		String[] Side = input.split (" ");

		String[] sides= new String[3];

		

		Side [0]=input;

		Side[1]=Side[1];

		Side[2]=Side[2];

		

		 if(((Side[0] + Side[1]) > Side[2]) || ((Side[0] + Side[2]) > Side[1]) ||((Side[1] + Side[0]) > Side[2]) || ((Side[1] + Side[2]) > Side[0]) ||((Side[2] + Side[0]) > Side[1]) || ((Side[2] + Side[1]) > Side[0]))

	     {

	         Screen.out.println("The triangle is valid and ");


	      {

	      if(((Side[0] == Side[1]) && (Side[0] == Side[2])) || ((Side[1]==Side[2])&& (Side[1]==Side[0])) || ((Side[2] == Side[0])&&(Side[2] == Side[1])))

	          Screen.out.println("equilateral.\n");

	      else

	          if(((Side[0] == Side[1])&&(Side[0]>Side[2]))|| ((Side[0]==Side[2])&&(Side[0]>Side[2])) || ((Side[1]==Side[2])&&(Side[1]>Side[0])) || ((Side[1]==Side[0])&&(Side[1]>Side[2])) || ((Side[2] ==Side[0])&&(Side[2] > Side[1])) ||((Side[2] == Side[1])&&(Side[2]>Side[0])))

	              Screen.out.println("isosceles.\n");

	          else

	              if(((Side[0] != Side[1]) && ( Side[0] != Side[2])) ||((Side[1] != Side[0]) &&(Side[1] != Side[2]))||((Side[2] != Side[0]) && (Side[2] != Side[1])))

	                  Screen.out.println("scalene.\n");

	      }

	     }

	  else

	      Screen.out.println("input error.");


	}


	public void print() {

	String input;

	Screen.out.println("Please enter the three sides: ");

	input = Keybd.in.readLine();

	}

}

i know there's something wrong in this but i dont know what.

please help

#2
Tejal

Tejal

    Newbie

  • Members
  • Pip
  • 9 posts

pindo said:

I need to check if one element in my array is larger than another, also if one is less than. how do i do this?
What does your array contain? Integers, Strings or something else? It looks like Side array is String array. So do you mean to say you want to sort it? Like a to z ?

Tejal


#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
I would use Integer objects and equals method, since you're just checking what kind of triangle it is.

    Integer firstSide = Integer.valueOf(Side[0]);

    Integer secondSide = Integer.valueOf(Side[1]);

    Integer thirdSide = Integer.valueOf(Side[2]);
    boolean firstEqualsSecond = firstSide.equals(secondSide);

            secondEqualsThird = secondSide.equals(thirdSide);

            firstEqualsThird = firstSide.equals(thirdSide);
Then just perform your logic code based on those boolean values. Only run the equals methods three times that way, though it's honestly not THAT big of an 'optimization' since Integer comparison is super cheap, I just thought boolean was more readable.

		String[] Side = input.split (" ");

		String[] sides= new String[3];

		

		Side [0]=input;

		Side[1]=Side[1];

		Side[2]=Side[2];
Don't do what you're doing here. after you've assigned the value of Side as the input split on spaces, you're set. You don't need to assign Side[0] to an array illegally, nor assign Side[1] and [2] to themselves, which the compiler will just optimize into oblivion. Remove everything but the first line, then...
	public void print() {

	String input;

	Screen.out.println("Please enter the three sides: ");

	input = Keybd.in.readLine();

	}
You can't use the local variable input in your other method. Declare input outside of this method, and assign to it here.
	public void print() {

	Screen.out.println("Please enter the three sides: ");

	input = Keybd.in.readLine();

	}


String input;
See what happens from there.
Wow I changed my sig!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users