Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Error in temperatur control program

Posted on 2004-10-23
4
Medium Priority
?
256 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
Comment
Question by:dailydream04
[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
  • 2
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 12391129
    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
ID: 12391169
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
ID: 12391822
you can declare it there, but it is only visible in the scope of the for loop.
0
 
LVL 1

Author Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
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 about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

610 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