Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Temperature Converter Program is not running properly

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Temperature Converter Program is not running properly

    I used netbeans to create the program below. Everything seems to run fine except my "FtoC" function doesn't run. It just returns to the initial conversion question. Any hints???

    package tempconverter;
    import java.util.Scanner;
    /**
     *
     * @author jessie
     */
    public class TempConverter {
     
        static Scanner sc= new Scanner(System.in);
     
        public static void main(String[] args) {
          int cType = 0;
          int getCType = 0;
          System.out.println("Welcome to the Temperature Converter");
     
     
          cType = getCType();   
          while (cType != 0){
            if (cType == 1) {
              FtoC();
            }  else if (cType == 2) {
              CtoF();
            } //else {
              //System.out.println("Illegal value: Please enter 1,2, or 0");
     
            //}
     
            cType = getCType();
          }
          System.out.println("Thanks for using the temperature converter");  
        }//end of main
        public static int getCType(){
            int ct;
            do {
         System.out.print("Select Conversion Type (1=F to C, 2=C to F, 0=end)"); 
            try {    
              ct = sc.nextInt();
              if (ct != 0 && ct != 1 && ct != 2){
              System.out.println("Illegal value: must be an integer of 0,1, or 2");    
              }
            } catch (Exception e){
     
     
          sc.next();
          ct = -1;
     
          } 
            }while (ct != 0 && ct != 2);
        return ct; 
     
        } //end of getCType     
     
     public static void FtoC() {         
         double c,f; 
         System.out.print("Enter your Fahrenheit temperature");
         f = sc.nextDouble();
         c = 5.0 / 9.0 * (f - 32.0);
         System.out.println("A Temp of " + f + " Fahrenheit converted to Celsius = " + c + "C");
     
     } //end of FtoC  
     public static void CtoF(){
         double f,c;
         System.out.print("Enter your Celsius temperature");
         c = sc.nextDouble();
         f = 9.0 / 5.0 * c + 32.0;
         System.out.println("A Temp of " + c + " Celsius converted to Fahrenheit = " + f + "F");
     
     }//end of CtoF
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Temperature Converter Program is not running properly

    Can you copy the contents of the console window for when you execute the program and post it here?
    Add some comments to the printout saying what is wrong with the output and show what it should be.

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Temperature Converter Program is not running properly

    Here's a portion that's running correctly and shows something similar to what should be happening. When I enter 2, it asks for the Celsius temp to convert, converts it, and goes back to the beginning.

    Welcome to the Temperature Converter
    Select Conversion Type (1=F to C, 2=C to F, 0=end) 2
    Enter your Celsius temperature 12
    A Temp of 12.0 Celsius converted to Fahrenheit = 53.6F
    Select Conversion Type (1=F to C, 2=C to F, 0=end)

    For FtoC, or Fahrenheit to Celsius (the portion not running correctly), I get the following. When I enter 1 it just cycles back around to the "make the selection" question. I don't know enough about what I'm doing to recognize my coding error.

    Welcome to the Temperature Converter
    Select Conversion Type (1=F to C, 2=C to F, 0=end) 1
    Select Conversion Type (1=F to C, 2=C to F, 0=end)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Temperature Converter Program is not running properly

    Try debugging the code by adding some println statements that show where the execution flow is going and what the values of the variables are as it executes so you can see what the computer sees as the program is executed.

    Are you saying that the code works when you enter a 2 and fails when you enter a 1?

    Look at the code that keeps the loop going and see where the value of 2 would work and the value of 1 fails.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Temperature Converter Program is not running properly

    Thanks I finally caught my error.

Similar Threads

  1. [SOLVED] Making Binary Converter script from scratch, running into math issue.
    By mwebb in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 8th, 2011, 07:47 PM
  2. GUI temp converter program troubles
    By copelandtml in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 14th, 2011, 10:28 AM
  3. J2ME converter program
    By sackling in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 6th, 2010, 09:13 AM
  4. converter program, problem with action listener
    By robertson.basil in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 05:44 AM
  5. Converting temperature program
    By ixjaybeexi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2009, 07:27 PM