Turn on thread page Beta
    • Thread Starter
    Offline

    0
    ReputationRep:
    Code:
    package temperatureconversion;
    import java.util.Scanner;
     public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Please enter Conversion type: Press C for Celsius to Fahrenheit or press F for Fahrenheit to Celsius.");
            
     String Vctype = keyboard.next();
            
            if (Vctype == "f" || Vctype == "F"){
                System.out.println("Please enter fahrenheit");
                double Vfahrenheit = keyboard.nextInt();
                Vfahrenheit = (Vfahrenheit)*(9/5)+(32);
                    System.out.println(Vfahrenheit);
            }
            if (Vctype == "c" || Vctype == "C"){
                System.out.println("Please enter celcius");
                double Vcelcius = keyboard.nextInt();
                Vcelcius = (Vcelcius - 32)*(5/9);
                       System.out.println(Vcelcius) ;
            }        
        }   
    }
    Could someone please help me with the above code. Basically in the output console the program just seems to end after I hit C or F, but instead it should ask for a number then allow a number input, then calculate and finally display the calculation. Where am I going wrong?
    Offline

    10
    ReputationRep:
    Short answer :
    Vctype.compareToIgnoreCase("f") == 0
    Vctype.compareToIgnoreCase("c") == 0
    In the checks.


    Longer answer:
    Java doesn't have pointers explicitly but you can think each variable reference as a pointer. So what happens with Vctype == "f" is rather then logical comparison memory locations are compared.
    • Thread Starter
    Offline

    0
    ReputationRep:
    Hi dk, thanks for your reply mate. I've tried changing my checks to the code you mentioned but I still cannot get the checks to work. As you can see from my code, it is supposed to ask which conversion the user wants. if c is pressed, it then asks for number in celcius to convert to Fahrenheit. then does the appropriate calculations and prints which sums up the program and visa versa for f (conversion to celcius). But still no dice, it seems to completely skip past the checks and just ends the program once the input F or C is made.
    Offline

    0
    ReputationRep:
    (Original post by Kfcgravy)
    Hi dk, thanks for your reply mate. I've tried changing my checks to the code you mentioned but I still cannot get the checks to work. As you can see from my code, it is supposed to ask which conversion the user wants. if c is pressed, it then asks for number in celcius to convert to Fahrenheit. then does the appropriate calculations and prints which sums up the program and visa versa for f (conversion to celcius). But still no dice, it seems to completely skip past the checks and just ends the program once the input F or C is made.
    It's been a while since i've programmed but the cmd auto exits right? (at least iirc it does in c++) so you would have to get the input stream or loop (not recommended) it to keep the program open to display the input.
    • Thread Starter
    Offline

    0
    ReputationRep:
    No it just says "Build successful" in netbeans, which ends the loop/program. It's ok though I have got it covered. I used Vctype.equals("f"). Thanks a lot for your help mate.
 
 
 
Reply
Submit reply
Turn on thread page Beta
Updated: October 12, 2013

University open days

  • Manchester Metropolitan University
    Postgraduate Open Day Postgraduate
    Wed, 14 Nov '18
  • University of Chester
    Chester campuses Undergraduate
    Wed, 14 Nov '18
  • Anglia Ruskin University
    Ambitious, driven, developing your career & employability? Aspiring in your field, up-skilling after a career break? Then our Postgrad Open Evening is for you. Postgraduate
    Wed, 14 Nov '18
Poll
Have you ever experienced bullying?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.