Solved

Error in temperatur control program

Posted on 2004-10-23
251 Views
Last Modified: 2010-03-31
Hi Expert,

There are two errors in the following program.

Here is the code:

import javax.swing.*;

public class TempControl            {

      final static float[] tempLevel = {20, 55, 65}; //defining temp. levels

      final static int level1 = 1; //below level 1, turn on both lower and upper furnaces

      final static int level2 = 2; //below level 2 and above level 1 turn on upper furnace

      final static int level3 = 3; //below level 3 and above level 2 do nothing

      final static int level4 = 4; //above level 3, turn on the air conditioner

      public static void main(String args[]){

            int temp = 0;

            boolean done = false; //the is the flag that signals the exit from the while loop

            while (!done) {

                  String response = JOptionPane.showInputDialog(null, "Please enter a temperature (press Exit to close):");

                  try {

                        temp = Integer.parseInt(response);

                        switch(getInstruction(temp))  {

                              case level1:

                                    JOptionPane.showMessageDialog(null,"Turn on the lower and upper furnaces");

                                    break;

                              case level2:

                                    JOptionPane.showMessageDialog(null,"Turn on the upper furnace");

                                    break;

                              case level3:

                                    JOptionPane.showMessageDialog(null,"Please, don't go wasting my money turning on anything");

                                    break;

                              case level4:

                                    JOptionPane.showMessageDialog(null,"Turn on the A/C");

                        }


                  } catch (NumberFormatException e) {

                        if (response.equalsIgnoreCase("exit")) done = true;

                        else JOptionPane.showMessageDialog(null,"Please, enter numbers only");



                  }



            }

            System.exit(0);



      }

      //Method to set temperature level range

      public static int getInstruction(int temp){

            for(int i =0; i < tempLevel.length; i++) {

                  if (temp < tempLevel[i]) {

                        return i + 1;

                  }

            }

            return i + 1;
      }
}

Here's the errors I got when I tried to compile it:

TempControl.java:109: cannot resolve symbol
symbol  : variable i
location: class TempControl
            return i + 1;
                   ^
TempControl.java:109: incompatible types
found   : java.lang.String
required: int
            return i + 1;
                     ^
2 errors


Do you know why I'm getting the errors and how I can fix it?  Thank you.
0
Question by:dailydream04
    4 Comments
     
    LVL 92

    Accepted Solution

    by:
        public static int getInstruction(int temp){
           int i = 0;
                for(i =0; i < tempLevel.length; i++) {

                      if (temp < tempLevel[i]) {

                            return i + 1;

                      }

                }

                return i + 1;
          }
    0
     
    LVL 1

    Author Comment

    by:dailydream04
    It works.

    I thought that I can declarate the counter i in the for loop, but obviously I was wrong.  Thank you so much.
    0
     
    LVL 92

    Expert Comment

    by:objects
    you can declare it there, but it is only visible in the scope of the for loop.
    0
     
    LVL 1

    Author Comment

    by:dailydream04
    That makes a lot sense.  Thanks again.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    877 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now