Error in temperatur control program

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.
LVL 1
dailydream04Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
    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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dailydream04Author Commented:
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
objectsCommented:
you can declare it there, but it is only visible in the scope of the for loop.
0
dailydream04Author Commented:
That makes a lot sense.  Thanks again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.