Results 1 to 7 of 7
  1. #1
    lk1001 is offline Member
    Join Date
    Feb 2010
    Posts
    8
    Rep Power
    0

    Default Loops and display problem

    Hey all,

    so new java developer here and I am trying to create a program that will show this to the user.

    Please enter the temperature you would like to convert - 0
    Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2
    0.00000 degrees Celsius = 32.000 degrees Fahrenheit.
    Would you like to continue? ( 0 = No ; 1 = Yes ) - 1
    Please enter the temperature you would like to convert 32.0
    Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 1
    32.000 degrees Fahrenheit = 0.00000 degrees Celsius.
    Would you like to continue? ( 0 = No ; 1 = Yes ) 0.0
    Good Bye!


    I have hit some road blocks in my development and I was hoping someone could point me in the right direction.

    As this is a school project, I would rather do it myself than have someone rewrite all my code. Thanks


    Here is my code ( I havent modified the output to match the required output as I was just plugging things in first) Also for this I need celcius to have 5 decimal points and Fahrenheit to have 3)



    mport java.util.Scanner;



    public class Lab{


    public static void main (String [] args){

    Scanner input = new Scanner(System.in);
    {
    double celsius;
    double farenheit;
    int degrees;


    System.out.print("Please enter the temperature you would like to convert : ");
    double temp = input.nextDouble ();


    char response = 0;
    while (response == 'Y')
    {
    System.out.print("Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2");
    degrees = input.nextInt ();



    if((degrees == '1') || (degrees == '1'))
    {
    farenheit = 9/5 * (temp) + 32;
    System.out.println("Temperature = " +temp + 'C');
    System.out.println("Temperature in Farenheit = " +farenheit +'F');
    }

    else if ((degrees == 'F') || (degrees == 'f'))
    {
    celsius = 5/9 * (temp - 32);
    System.out.println("Temperature = " +temp + '1');
    System.out.println("Temperature in Celsius = " +celsius + '2');
    }
    else
    {
    System.out.println("Good Bye");
    //System.out.println("You entered" +degrees);
    System.out.println("Enter 1 for Celsius or 2 for Farenheit");
    System.out.print("Enter temperature: ");
    temp = input.nextDouble ();



    }

    System.out.print("Would you like to enter another temperature? Y or N: ");

    //System.out.print("Enter <strong class="highlight">a</strong> temperature: ");
    //temp = Input.readDouble ();

    }

    }
    } // End Main
    } // End Class DegreesConverter

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Hey, I 've ust tried to run your code and even I am stuck. Im not sure what is meant to happen. No matter what value you enter the program just quits. :confused:

    Seems that you are having some confusion with your variables too: You ask the user for an input and its just "fluffy".
    Java Code:
    double temp = input.nextDouble ();
    what is the purpose of temp?

    I think the main problem is that you use three variables. why dont you just ask the user to select 1 for F and 0 for C.Then depending of the selection enter temperature in F and convert it to C and vice versa and get rid of variable "degrees".

    This looks interesting too:
    Java Code:
    System.out.print("Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2");
    degrees = input.nextInt ();
    
    
    
    if((degrees == '1') || (degrees == '1'))
    {
    farenheit = 9/5 * (temp) + 32;
    System.out.println("Temperature = " +temp + 'C');
    System.out.println("Temperature in Farenheit = " +farenheit +'F');
    }
    
    else if ((degrees == 'F') || (degrees == 'f'))
    {
    celsius = 5/9 * (temp - 32);
    System.out.println("Temperature = " +temp + '1');
    System.out.println("Temperature in Celsius = " +celsius + '2');
    }
    Seems like your code expects the user to enter "1" or "F" :confused: while you asked the user to input 1 or 2. It may be just a typo. I dont know. Plus if you declare a char variable, java understands the value as ASCII character - so it converts it to a decimal value(sort off). I guess it would be easier if you declare it as String instead of char.

    Seems like the code is too complicated and "fluffy" for the intended purpose. If you have some sort of specification that you are following that would be interesting to see too.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  3. #3
    lk1001 is offline Member
    Join Date
    Feb 2010
    Posts
    8
    Rep Power
    0

    Default

    Thanks for your help,

    I too am having trouble running the program. I made some changes as best I know how based on your reply, but the program will terminate after the first line.

    What I am trying to build is a program that will ask the user for a temp

    Store that

    Ask the user to input 1 for Fahrenheit and 2 for Celsius

    (Use five decimal places for Fahrenheight and three for Celsius)

    Calculate the temp based on user input

    Display User input and calculation (32.000 degrees Fahrenheit = 0.00000 degrees Celsius.)

    Ask the user if they want to calculate another temp (Y or N)

    Y restarts the program while N ends with good bye

    Here are my modifications, any help would be greatly appreciated as I am beyond lost now.

    import java.util.Scanner;



    public class Lab4{


    public static void main (String [] args){

    Scanner input = new Scanner(System.in);
    {
    double celsius;
    double farenheit;
    int degrees;


    System.out.print("Please enter the temperature you would like to convert : ");
    double temp = input.nextDouble ();


    char response = 0;
    while (response == 'Y')
    {
    System.out.print("Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2");
    degrees = input.nextInt ();



    if((degrees == '1') || (degrees == '1'))
    {
    farenheit = 9/5 * (temp) + 32;
    System.out.println("Temperature = " +temp + '1');
    System.out.println("Temperature in Farenheit = " +farenheit +'1');
    }

    else if ((degrees == '2') || (degrees == '2'))
    {
    celsius = 5/9 * (temp - 32);
    System.out.println("Temperature = " +temp + '1');
    System.out.println("Temperature in Celsius = " +celsius + '2');
    }
    else
    {

    System.out.println("Enter 1 for Celsius or 2 for Farenheit");
    System.out.print("Enter temperature: ");
    temp = input.nextDouble ();


    }

    System.out.print("Would you like to enter another temperature? Y or N: ");

    if (response == 'N')
    System.out.println("Good Bye");
    //System.out.println("Good Bye);

    //System.out.print("Enter <strong class="highlight">a</strong> temperature: ");
    //temp = Input.readDouble ();

    }

    }
    } // End Main
    } // End Class DegreesConverter

  4. #4
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    What I am trying to build is a program that will ask the user for a temp
    <variable name> = ScannerInput; <-- you automatically store it in a <variable name>

    Ask the user to input 1 for Fahrenheit and 2 for Celsius
    ok so this time you need a different <variable name1> to store 1 or 2. I guess int would do. What do you think? So its a simple if statement.

    If <variable name1> == something
    {
    DO THIS and display output
    }
    else if <variable name1> == something
    {
    DO THIS and display output
    }

    Ask the user if they want to calculate another temp (Y or N)
    This will require a loop. So the idea is that: while <variable name> is Y run the code. Hint: loop will have to cover the whole code (if you know what i mean).
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  5. #5
    lk1001 is offline Member
    Join Date
    Feb 2010
    Posts
    8
    Rep Power
    0

    Default

    First of all, thank you so much for your help.

    Secondly,

    Is this a good start to looping the whole thing together?

    import java.util.Scanner;

    public class Lab4{

    public static void main (String [] args){

    Scanner input = new Scanner(System.in);
    {
    char Result;
    int Degrees;
    int decision;

    if decision ==1{

    System.out.print("Good Bye")

    else decision == 0{

    System.out.print("Please enter the temperature you would like to convert - ");
    Temp = input.nextDouble();

  6. #6
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Well you havent looped anything yet. If statements do not loop. Read about While, Do Until, Do While, For loops.

    Java Code:
    import java.util.Scanner;
    
    public class Lab4{
    
    public static void main (String [] args)
    {
        <someVariable> = 1;
        Scanner input = new Scanner(System.in);
        <someVariable = input;
        
       while{<someVariable>==1)
      {
         //run your code
       }
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  7. #7
    lk1001 is offline Member
    Join Date
    Feb 2010
    Posts
    8
    Rep Power
    0

    Default

    Again thank you so much for all of your help

    I have reread the areas on loops, and I know I have some issues here, but any chance you could tell me what is wrong here?

    i rewrote based on my reading and your suggestions

    import java.util.Scanner;

    public class Lab4{

    private static double farenheit;
    private static double celsius;


    public static void main (String [] args){

    Scanner input = new Scanner(System.in);
    {
    int Degrees;
    int decision = 0;
    double Temp;

    System.out.print("Please enter the temperature you would like to convert - ");
    Temp = input.nextDouble();

    System.out.print("Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2");
    Degrees = input.nextInt ();

    if((Degrees == '1') || (Degrees == '1')){{
    farenheit = 9/5 * (Temp) + 32;
    System.out.println("(Temp) + Degrees Celsius = (farenheight) + degrees Farenheight");
    System.out.print("Would you like to continue? ( 0 = No ; 1 = Yes ) – 0.0");}}

    else if(Degrees =='0');
    celsius = 5/9 * (Temp - 32);
    System.out.println("(Temp) degrees Farenheight = (celsius) Degrees Celsius");}
    System.out.print("Would you like to continue? ( 0 = No ; 1 = Yes ) – 0.0");
    int decision = (int) input.nextDouble();}


    {

    char decision = 0;
    while (decision =='1'){

    System.out.print("Good Bye");

    if(decision =='0'){
    Last edited by lk1001; 02-26-2010 at 05:42 PM.

Similar Threads

  1. Struts 2 paging problem using display tag...?
    By prabhurangan in forum Web Frameworks
    Replies: 1
    Last Post: 01-15-2014, 10:00 AM
  2. Problem with ordering for loops
    By ScaryJello in forum New To Java
    Replies: 3
    Last Post: 03-31-2009, 09:20 PM
  3. Date n Time Display problem
    By kapilverma32 in forum Advanced Java
    Replies: 6
    Last Post: 02-10-2009, 02:53 PM
  4. Problem with display the character
    By romina in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:43 PM
  5. Problem with display in Netbeans 5.5
    By Albert in forum NetBeans
    Replies: 1
    Last Post: 07-13-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •