?
Solved

Not sure about this error??

Posted on 2006-11-16
5
Medium Priority
?
188 Views
Last Modified: 2010-03-31
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.showInputDialog(inputStr+"  Enter 2004 to quit.");
      scale=JOptionPane.showInputDialog(inputStr2);
      temp=Integer.parseInt(tempStr);
      

      while (temp<=2003 && temp!=SENTINEL);
      {
                  if (scale="f"||"F")
                  {
                        fahr=temp;
                        celc=fahrenheitToCelcius(temp);
                        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.showMessageDialog(null,outputStr,"Your Conversions",JOption.PLAIN_MESSAGE);



     }
      
}


0
Comment
Question by:jasonbrandt3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Expert Comment

by:firas_fas
ID: 17962009
try replacing  if (scale="f"||"F") with :

if (scale.equals("f") || scale.equals("F"))
0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 2000 total points
ID: 17962087
>                if (scale="f"||"F")
if you want to check if scale is "f" or "F", use the following

if("f".equalsIgnoreCase(scale))
0
 

Author Comment

by:jasonbrandt3
ID: 17965431
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.showMessageDialog(null,outputStr,"Your Conversions",JOptionPane.INFORMATION_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.showInputDialog(inputStr+"  Enter 2004 to quit.");
      scale=JOptionPane.showInputDialog(inputStr2);
      temp=Integer.parseInt(tempStr);
        

      while (temp<=2003 && temp!=SENTINEL);
      {
                  if (scale.equals("f") || scale.equals("F"))
                  {
                        fahr=temp;
                        celc=fahrenheitToCelcius(temp);
                        kelv=celciusToKelvin(celc);
                        outputStr=String.format("Temperature:  "+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.showMessageDialog(null,outputStr,"Your Conversions",JOptionPane.INFORMATION_MESSAGE);



     }
      
      
}

0
 
LVL 6

Expert Comment

by:SamsonChung
ID: 17965954
Strings can not be || ...

you can use the equal method, where firas aor Ajay suggested.

other then that.... that's basiclaly your problem there.
0
 

Expert Comment

by:firas_fas
ID: 17966747
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(temp);
                    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(temp);
                    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";
               }
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question