wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes operator & cannot be applied to boolean.java.lang.String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "operator & cannot be applied to boolean.java.lang.String" Watch "operator & cannot be applied to boolean.java.lang.String" New topic
Author

operator & cannot be applied to boolean.java.lang.String

matt jamison
Greenhorn

Joined: Jul 09, 2006
Posts: 7
Hello.
It is pointless to say i'm new to java because anyperson asking for help in the beginner section is. Anyway, i am writing a program to tell what type of triangle the length of 3 sides make. I keep getting operator & cannot be applied to boolean.java.lang.String when i try to compile. here is my code

/* Determines type of triangle based on length of sides.
*/
import javax.swing.JOptionPane;
public class Triangles
{
public static void main(String[] args)
{
//get the length of the sides
String sideOne;
sideOne = JOptionPane.showInputDialog("Please enter the length of Side One");
String sideTwo;
sideTwo = JOptionPane.showInputDialog("Please enter the length of Side Two");
String sideThree;
sideThree = JOptionPane.showInputDialog("Please enter the lenght of Side Three");
{
// decide what type of triangle it is.
if (sideOne == sideTwo && sideThree)
{
JOptionPane.showMessageDialog(null, "The Triangle is Equilateral");
break;
}
else if (sideOne == sideTwo || sideThree)
{
JOptionPane.showMessageDialog(null, "The Triangle is Isosceles");
//break;
}
else if ((sideOne != sideTwo || sideThree) && (sideTwo != sideOne || sideThree))
//break;
{
JOptionPane.showMessageDialog(null, "The Triangle is Scalene");
//break;
}

Thanks to anyone who helps me.

}
System.exit(0);
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

There are two different problems here; let's look at them one at a time.

First, you can't meaningfully use "==" to compare Strings; you must use the equals() method instead. So given that sideOne and sideTwo are Strings, instead of

if (sideOne == sideTwo)

you must instead write

if (sideOne.equals(sideTwo))

The second thing is that, in Java, to say

if (x equals a or b)

you must actually write

if (x equals a or x equals b)

It's this latter issue that's causing the errors. The same goes for &&; you have to write

if (x equals a and x equals b)

rather than

if (x equals a and b)

So putting this altogether, what you've written as (for example)

if (sideOne == sideTwo && sideThree)

must be written as

if (sideOne.equals(sideTwo) && sideOne.equals(sideThree))

Finally, note that the above is not a sufficient condition to ensure that the triangle is equilateral, only that it's isoceles. You need one more test to ensure that all three sides are equal.


[Jess in Action][AskingGoodQuestions]
matt jamison
Greenhorn

Joined: Jul 09, 2006
Posts: 7
thanks for the advice so far. I have a few more problems however. Here is my new code. it says i need a ')' on line 20 and 30.

/* Determines type of triangle based on length of sides.
*/
import javax.swing.JOptionPane;
public class Triangles
{
public static void main(String[] args)
{
//get the length of the sides
String sideOne;
sideOne = JOptionPane.showInputDialog("Please enter the length of Side One");
String sideTwo;
sideTwo = JOptionPane.showInputDialog("Please enter the length of Side Two");
String sideThree;
sideThree = JOptionPane.showInputDialog("Please enter the lenght of Side Three");
{
// decide what type of triangle it is.
if ((sideOne equals(sideTwo)) && (sideOne equals(sideThree)))
{
JOptionPane.showMessageDialog(null, "The Triangle is Equilateral");
break;
}
else if ((sideOne equals(sideTwo)) || (sideOne equals (sideThree)))
{
JOptionPane.showMessageDialog(null, "The Triangle is Isosceles");
break;
}
else if ((sideOne equals(!sideTwo)) || (sideOne equals(!sideThree))) && ((sideTwo equals(!sideOne)) || (sideTwo equals(!sideThree))) break;
{
JOptionPane.showMessageDialog(null, "The Triangle is Scalene");
break;
}

again thanks a lot
}
System.exit(0);
}
}
matt jamison
Greenhorn

Joined: Jul 09, 2006
Posts: 7
sorry, the lines are actually
'if ((sideOne equals(sideTwo)) && (sideOne equals(sideThree)))' and
'else if ((sideOne equals(!sideTwo)) || (sideOne equals(!sideThree))) && ((sideTwo equals(!sideOne)) || (sideTwo equals(!sideThree))) '
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

OK, two new things to explain.

I wan't too clear about when I was writing real code and when I was writing English-like pseudo-code. When you call the equals() method, or any method, you use the "dot" operator (.) to link the object to the method -- i.e.,

a.equals(b)

That's one thing. The other thing is that you're still showing that admirable creativity in arranging logical operators. You can't, unfortunately, write

a.equals(!b)

to mean "a not equal to b". You have to write

!a.equals(b)

to mean "it's not true that a equals b".
matt jamison
Greenhorn

Joined: Jul 09, 2006
Posts: 7
well, thanks for finding those problems. I still have the problem of the
close parenthesis. I'm sure it probably has nothing to do with parenthesis, but i can't think of what it could be. Thanks for being patient
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You sure you put all those dots in?
matt jamison
Greenhorn

Joined: Jul 09, 2006
Posts: 7
whoops. Thanks a lot for your help. I have it all working now.
Again thanks. I really like this JavaRanch site. Bravo. javascript: x()
beerchug
matt jamison
Greenhorn

Joined: Jul 09, 2006
Posts: 7
just when i thought i was done. I realized i had to write something in case the lengths given didn't make a triangle. I'm not sure how to add without using the + sign, which it wont work right if i do. more help please.

/* Determines type of triangle based on length of sides.
*/

import javax.swing.JOptionPane;


public class Triangles
{
public static void main(String[] args)
{
//get the length of the sides
String sideOne;
sideOne = JOptionPane.showInputDialog("Please enter the length of Side One");
String sideTwo;
sideTwo = JOptionPane.showInputDialog("Please enter the length of Side Two");
String sideThree;
sideThree = JOptionPane.showInputDialog("Please enter the lenght of Side Three");
{
// decide what type of triangle it is.
if ((sideOne + sideTwo < sideThree) || (sideTwo + sideThree < sideOne) || (sideOne + sideThree < side Two))
{
JOptionPane.showMessageDialog(null, "Not a Triangle");
}
else if ((sideOne.equals(sideTwo)) && (sideOne.equals(sideThree)))
{
JOptionPane.showMessageDialog(null, "The Triangle is Equilateral");
}
else if ((sideOne.equals(sideTwo)) || (sideOne.equals (sideThree)))
{
JOptionPane.showMessageDialog(null, "The Triangle is Isosceles");
}
else if (((!sideOne.equals(sideTwo)) && (!sideOne.equals(sideThree))) && ((!sideTwo.equals(sideOne)) && (!sideTwo.equals(sideThree))));
{
JOptionPane.showMessageDialog(null, "The Triangle is Scalene");
}

}
System.exit(0);
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

What you need to do is convert the Strings to numbers (probably type "int" if you don't need fractional lengths, or "double" if you do.)

To convert a String to a double, you'd use the static parseDouble() method in the Double class:

double aDouble = Double.parseDouble(aString);

To convert to an int, you'd do the same thing using the analagous method in the Integer class.

int anInt = Integer.parseInt(aString);

There's also the matter of exceptions to consider. If the user enters a String that isn't a valid number, those parseXXX methods will "throw an exception." Have you learned about those yet?
matt jamison
Greenhorn

Joined: Jul 09, 2006
Posts: 7
i'm not sure what you mean. Where do i put the double aDouble = Double.parseDouble(aString); in my code.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

After you get a String from the user, then the next thing you do is convert it to a double. Then you do the rest of the program with the double, rather than with the String (by the way, with doubles and ints and other numeric types, you do use the "==" operator rather than the equals() method, which doesn't exist for these numeric types.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: operator & cannot be applied to boolean.java.lang.String