Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Converting Boolean into String

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Converting Boolean into String

    I have this project due and its asking that i print out what type of triangle it is when the user inputs 3 sides. I have most of it done and working, but it pops up different windows instead of using one window for everything. The assignment says it needs all the final info to be in one window. The boolean is coming from another method. I'm unsure how to get it into a string (Or if that's what i have to do). The method must return a boolean true/false. Any guidance/help would be appreciated. Thanks!


    import javax.swing.*;
     
    public class Triangle {
     
    	public static void main(String[] args) {
     
    		int side1 = getSides();
    		int side2 = getSides();
    		int side3 = getSides();
     
    		boolean rightTriangle = isRight( side1, side2, side3 );
    		boolean scaleneTriangle = isScalene( side1, side2, side3 );
    		boolean isoscelesTriangle = isIsosceles( side1, side2, side3 );
    		boolean equilateralTriangle = isEquilateral( side1, side2, side3 );
    		double areaOfT = findArea( side1, side2, side3 );
     
    		if ( rightTriangle == true ) {
     
    			JOptionPane.showMessageDialog( null, "This is a Right Traingle");
    		}
    		if ( scaleneTriangle == true ) {
     
    			JOptionPane.showMessageDialog( null, "This is a Scalene Traingle");
    		}
    		if ( isoscelesTriangle == true ) {
     
    			JOptionPane.showMessageDialog( null, "This is an Isosceles Traingle");
    		}
    		if ( equilateralTriangle == true ) {
     
    			JOptionPane.showMessageDialog( null, "This is an Equilateral Traingle");
    		}
     
    		JOptionPane.showMessageDialog(null, "Side 1 is: " + side1 + "\nSide 2 is: " + side2 + "\nSide 3 is: " + side3 + "\nThe area of the traingle is: " + areaOfT );
     
    	} // ends main method
     
    	public static int getSides() {
     
    		int testside = Integer.parseInt( JOptionPane.showInputDialog("Enter Side: "));
     
    		while ( testside < 0 ) {
     
    				testside = Integer.parseInt( JOptionPane.showInputDialog("INVALID INPUT MUST BE GREATER THAN 0!\n\nEnter another number: "));				
    			} // ends while loop
     
    		return testside;		
    	} // ends getInput method
     
    	public static boolean isRight( int side1, int side2, int side3 ) {
     
    		double sqrSide1 = Math.pow( side1,2 );
    		double sqrSide2 = Math.pow( side2,2 );
    		double sqrSide3 = Math.pow( side3,2 );
    		double sqr12 = Math.pow( side1,2 ) + Math.pow( side2,2 );
    		double sqr13 = Math.pow( side1,2 ) + Math.pow( side3,2 );
    		double sqr23 = Math.pow( side2,2 ) + Math.pow( side3,2 );
     
    		if ( sqr12 == sqrSide3 || sqr13 == sqrSide2 || sqr23 == sqrSide1 ) {
     
    			return true;
    		} // ends true if
    		else {
     
    			return false;
    		} // ends false else
     
    	} // ends isRight method
     
    	public static boolean isScalene( int side1, int side2, int side3 ) {
     
    		if ( side1 != side2 && side1 != side3 && side2 != side3 ) {
     
    			return true;
    		}  // ends true if
    		else {
     
    			return false;
    		} // ends false else
     
    	} // ends isScalene method
     
    	public static boolean isIsosceles( int side1, int side2, int side3 ) {
     
    		if ( side1 == side2 || side1 == side3 || side2 == side3 ) {
     
    			return true;
    		} // ends true if
    		else {
     
    			return false;
    		} // ends false else
     
    	} // ends isIsosceles method
     
    	public static boolean isEquilateral( int side1, int side2, int side3 ) {
     
    		if ( side1 == side2 && side1 == side3 && side2 == side3 ) {
     
    			return true;
    		} // ends true if
    		else {
     
    			return false;
    		} // ends false else
     
    	} // ends isEquilateral method
     
    	public static double findArea( int side1, int side2, int side3 ) {
     
    		double x = .5 * ( side1 + side2 + side3 );
    		double area = Math.sqrt( x * ( x - side1 ) * ( x - side2 ) * ( x - side3 ) );
     
    		return area;
    	} // ends findArea method
     
    } // ends public class Triangle


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Converting Boolean into String

    What do you want in the String? Could you give two examples? One for the boolean being true and one for it false

    Look at the Boolean class's toString method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Converting Boolean into String

    Comparing a boolean to true/false like this:

    if ( rightTriangle == true )

    is unnecessary. This:

    if ( rightTriangle )

    is sufficient.

    If only one of the 'if' statements will be true, then an if/else construct may be more appropriate. Since you're getting more than one result, either there are multiple answers possible, or the logic that determines triangle type is wrong.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Converting Boolean into String

    Well lets say someone enters 3,4, 5 as the sides, thats a right triangle . In the output i want it to say Side 1 is: 3..... Side 2: is 4..... Side 3: is 5....It is a right Traingle.....The area is 6.0

    Right now it is set up where a window pops up saying its a right triangle. and when you click ok another window pops up saying what the sides and area are.

Similar Threads

  1. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM
  2. Boolean and String Trouble...
    By Souljahgirl101 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 3rd, 2012, 08:17 PM
  3. Converting a String to an Int? Is it possible?
    By Gravity Games in forum Java Theory & Questions
    Replies: 2
    Last Post: July 14th, 2012, 11:21 PM
  4. String or Boolean help
    By __NightWhisper__ in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 18th, 2012, 07:45 PM
  5. Converting to String
    By darek9576 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 13th, 2010, 06:09 PM