jasonbrandt3
asked on
Not sure about this error??
I cannot figure out why I'm getting these two errors:
temp.java:84: operator || cannot be applied to java.lang.String,java.lang .String
if (scale="f"||"F")
^
temp.java:84: incompatible types
found : java.lang.String
required: boolean
if (scale="f"||"F")
^
Here is my code:
import javax.swing.*;
import java.util.*;
import javax.swing.JOptionPane;
public class temp
{
public static float celciusToFahrenheit(float degCelc)
{
float degFahr;
degFahr= degCelc * 9/5 + 32;
return degFahr;
}
public static float fahrenheitToCelcius(float degFahr)
{
float degCelc;
degCelc=(degFahr-32) *5/9;
return degCelc;
}
public static float celciusToKelvin(float degCelc)
{
float degKelv;
degKelv=degCelc+273.15f;
return degKelv;
}
public static float kelvinToCelcius(float degKelv)
{
float degCelc;
degCelc = degKelv - 273.15f;
return degCelc;
}
public static void main(String[] args)
{
final int SENTINEL = 2004;
int temp;
String scale;
String tempStr;
String outputStr;
String inputStr;
String inputStr2;
float fahr;
float celc;
float kelv;
inputStr="Please enter a temperature less than or equal to 2003.";
inputStr2="Please enter the scale: c,C=celcius, f,F=fahrenheit, k,K=kelvin.";
tempStr=JOptionPane.showIn putDialog( inputStr+" Enter 2004 to quit.");
scale=JOptionPane.showInpu tDialog(in putStr2);
temp=Integer.parseInt(temp Str);
while (temp<=2003 && temp!=SENTINEL);
{
if (scale="f"||"F")
{
fahr=temp;
celc=fahrenheitToCelcius(t emp);
kelv=celciusToKelvin(celc) ;
outputStr="Temperature: "+temp+"/n"
+"Scale: "+scale+"/n"
+"Temperature in F is: "+"%2f,fahr"+"/n"
+"Temperature in C is: "+"%2f,celc"+"/n"
+"Temperature in K is: "+"%2f,kelv"+"/n";
}
}
JoptionPane.showMessageDia log(null,o utputStr," Your Conversions",JOption.PLAIN _MESSAGE);
}
}
temp.java:84: operator || cannot be applied to java.lang.String,java.lang
if (scale="f"||"F")
^
temp.java:84: incompatible types
found : java.lang.String
required: boolean
if (scale="f"||"F")
^
Here is my code:
import javax.swing.*;
import java.util.*;
import javax.swing.JOptionPane;
public class temp
{
public static float celciusToFahrenheit(float degCelc)
{
float degFahr;
degFahr= degCelc * 9/5 + 32;
return degFahr;
}
public static float fahrenheitToCelcius(float degFahr)
{
float degCelc;
degCelc=(degFahr-32) *5/9;
return degCelc;
}
public static float celciusToKelvin(float degCelc)
{
float degKelv;
degKelv=degCelc+273.15f;
return degKelv;
}
public static float kelvinToCelcius(float degKelv)
{
float degCelc;
degCelc = degKelv - 273.15f;
return degCelc;
}
public static void main(String[] args)
{
final int SENTINEL = 2004;
int temp;
String scale;
String tempStr;
String outputStr;
String inputStr;
String inputStr2;
float fahr;
float celc;
float kelv;
inputStr="Please enter a temperature less than or equal to 2003.";
inputStr2="Please enter the scale: c,C=celcius, f,F=fahrenheit, k,K=kelvin.";
tempStr=JOptionPane.showIn
scale=JOptionPane.showInpu
temp=Integer.parseInt(temp
while (temp<=2003 && temp!=SENTINEL);
{
if (scale="f"||"F")
{
fahr=temp;
celc=fahrenheitToCelcius(t
kelv=celciusToKelvin(celc)
outputStr="Temperature: "+temp+"/n"
+"Scale: "+scale+"/n"
+"Temperature in F is: "+"%2f,fahr"+"/n"
+"Temperature in C is: "+"%2f,celc"+"/n"
+"Temperature in K is: "+"%2f,kelv"+"/n";
}
}
JoptionPane.showMessageDia
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The first suggestion seemed to work. I'm getting another error. I changed some things. Here is the error I'm now getting:
(PLEASE HELP!!!!!) I thought the outputStr is initialized in the If loop........
temp.java:102: variable outputStr might not have been initialized
JOptionPane.showMessageDia log(null,o utputStr," Your Conversions",JOptionPane.I NFORMATION _MESSAGE);
^
1 error
Here is my new code:
import javax.swing.*;
import java.util.*;
import javax.swing.JOptionPane;
public class temp
{
public static float celciusToFahrenheit(float degCelc)
{
float degFahr;
degFahr= degCelc * 9/5 + 32;
return degFahr;
}
public static float fahrenheitToCelcius(float degFahr)
{
float degCelc;
degCelc=(degFahr-32) *5/9;
return degCelc;
}
public static float celciusToKelvin(float degCelc)
{
float degKelv;
degKelv=degCelc+273.15f;
return degKelv;
}
public static float kelvinToCelcius(float degKelv)
{
float degCelc;
degCelc = degKelv - 273.15f;
return degCelc;
}
public static void main(String[] args)
{
final int SENTINEL = 2004;
int temp;
String scale;
String tempStr;
String outputStr;
String inputStr;
String inputStr2;
float fahr;
float celc;
float kelv;
inputStr="Please enter a temperature less than or equal to 2003.";
inputStr2="Please enter the scale: c,C=celcius, f,F=fahrenheit, k,K=kelvin.";
tempStr=JOptionPane.showIn putDialog( inputStr+" Enter 2004 to quit.");
scale=JOptionPane.showInpu tDialog(in putStr2);
temp=Integer.parseInt(temp Str);
while (temp<=2003 && temp!=SENTINEL);
{
if (scale.equals("f") || scale.equals("F"))
{
fahr=temp;
celc=fahrenheitToCelcius(t emp);
kelv=celciusToKelvin(celc) ;
outputStr=String.format("T emperature : "+temp+"/n")
+String.format( "Scale: "+scale+"/n")
+String.format( "Temperature in F is: "+"%2f",fahr)+"/n"
+String.format( "Temperature in C is: "+"%2f",celc)+"/n"
+String.format( "Temperature in K is: "+"%2f",kelv)+"/n";
}
}
JOptionPane.showMessageDia log(null,o utputStr," Your Conversions",JOptionPane.I NFORMATION _MESSAGE);
}
}
(PLEASE HELP!!!!!) I thought the outputStr is initialized in the If loop........
temp.java:102: variable outputStr might not have been initialized
JOptionPane.showMessageDia
^
1 error
Here is my new code:
import javax.swing.*;
import java.util.*;
import javax.swing.JOptionPane;
public class temp
{
public static float celciusToFahrenheit(float degCelc)
{
float degFahr;
degFahr= degCelc * 9/5 + 32;
return degFahr;
}
public static float fahrenheitToCelcius(float degFahr)
{
float degCelc;
degCelc=(degFahr-32) *5/9;
return degCelc;
}
public static float celciusToKelvin(float degCelc)
{
float degKelv;
degKelv=degCelc+273.15f;
return degKelv;
}
public static float kelvinToCelcius(float degKelv)
{
float degCelc;
degCelc = degKelv - 273.15f;
return degCelc;
}
public static void main(String[] args)
{
final int SENTINEL = 2004;
int temp;
String scale;
String tempStr;
String outputStr;
String inputStr;
String inputStr2;
float fahr;
float celc;
float kelv;
inputStr="Please enter a temperature less than or equal to 2003.";
inputStr2="Please enter the scale: c,C=celcius, f,F=fahrenheit, k,K=kelvin.";
tempStr=JOptionPane.showIn
scale=JOptionPane.showInpu
temp=Integer.parseInt(temp
while (temp<=2003 && temp!=SENTINEL);
{
if (scale.equals("f") || scale.equals("F"))
{
fahr=temp;
celc=fahrenheitToCelcius(t
kelv=celciusToKelvin(celc)
outputStr=String.format("T
+String.format( "Scale: "+scale+"/n")
+String.format( "Temperature in F is: "+"%2f",fahr)+"/n"
+String.format( "Temperature in C is: "+"%2f",celc)+"/n"
+String.format( "Temperature in K is: "+"%2f",kelv)+"/n";
}
}
JOptionPane.showMessageDia
}
}
Strings can not be || ...
you can use the equal method, where firas aor Ajay suggested.
other then that.... that's basiclaly your problem there.
you can use the equal method, where firas aor Ajay suggested.
other then that.... that's basiclaly your problem there.
The fact that the first time you initialize the variable outputStr is inside of an if statement causes this problem.
Where you define all your variables in the main method, try replacing:
String outputStr;
with:
String outputStr="";
This should get rid of the error.
You may also want to add to your if statement block ( if you want to accept the three types of scales from user). If you want to do that then replace this:
if (scale.equals("f") || scale.equals("F"))
{
fahr=temp;
celc=fahrenheitToCelcius(t emp);
kelv=celciusToKelvin(celc) ;
outputStr="Temperature: "+temp+"/n"
+"Scale: "+scale+"/n"
+"Temperature in F is: "+ fahr+"/n"
+ "Temperature in C is: "+celc +"/n"
+ "Temperature in K is: "+ kelv +"/n";
}
with:
if (scale.equals("f") || scale.equals("F"))
{
fahr=temp;
celc=fahrenheitToCelcius(t emp);
kelv=celciusToKelvin(celc) ;
outputStr="Temperature: "+temp+"/n"
+"Scale: "+scale+"/n"
+"Temperature in F is: "+ fahr+"/n"
+ "Temperature in C is: "+celc +"/n"
+ "Temperature in K is: "+ kelv +"/n";
}else if (scale.equals("c") || scale.equals("C")){
.....
do conversion
......
}else if (scale.equals("f") || scale.equals("F")){
.....
do conversion
......
}else{
outputStr="You Did not enter a scale\n";
}
Where you define all your variables in the main method, try replacing:
String outputStr;
with:
String outputStr="";
This should get rid of the error.
You may also want to add to your if statement block ( if you want to accept the three types of scales from user). If you want to do that then replace this:
if (scale.equals("f") || scale.equals("F"))
{
fahr=temp;
celc=fahrenheitToCelcius(t
kelv=celciusToKelvin(celc)
outputStr="Temperature: "+temp+"/n"
+"Scale: "+scale+"/n"
+"Temperature in F is: "+ fahr+"/n"
+ "Temperature in C is: "+celc +"/n"
+ "Temperature in K is: "+ kelv +"/n";
}
with:
if (scale.equals("f") || scale.equals("F"))
{
fahr=temp;
celc=fahrenheitToCelcius(t
kelv=celciusToKelvin(celc)
outputStr="Temperature: "+temp+"/n"
+"Scale: "+scale+"/n"
+"Temperature in F is: "+ fahr+"/n"
+ "Temperature in C is: "+celc +"/n"
+ "Temperature in K is: "+ kelv +"/n";
}else if (scale.equals("c") || scale.equals("C")){
.....
do conversion
......
}else if (scale.equals("f") || scale.equals("F")){
.....
do conversion
......
}else{
outputStr="You Did not enter a scale\n";
}
if (scale.equals("f") || scale.equals("F"))