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 12 of 12

Thread: Need some help please

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

    Default Need some help please

    Hi Im new to everything the forum and java. But anyways I need some help with my coding. I need my program to to have a max value of 212 and convert a value from fahrenheit to celsius and then to kelvin, also feet to inches and pounds to stones. I also need to it say if the value is out of range for ex. 213 is out of range, and that a value is not valid ex. abs is not a valid entry.

    Heres my code so far.
            import java.util.Scanner;
     
    public class Project1 {
    	private static final int MAX_VALUE = 212;
     
     
     
    	public static void main(String[] args) {
     
     
    		//Declare constants
    		final double CS = (5.0 / 9);
    		final double ME = 0.3048;
    		final double KL = 0.4536;
     
    		//Declare variables
    		Scanner input = new Scanner(System.in);
    		String entry;
    		int fahrenheit, feet, pounds, count = 0, guess = -1, number;
    		double fahrenheit1, feet1, pounds1;
     
    		System.out.println(" J A ");
    		System.out.println(" Project Title ");
     
    		//Get input
    		System.out.println("Enter a temperature in fahrenheit (integer): ");
    		fahrenheit1 = input.nextInt();
    		System.out.println("Enter a distance in feet (integer): ");
    		feet1 = input.nextInt();
    		System.out.println("Enter a weight in pounds (integer): ");
    		pounds1 = input.nextInt();
     
    		number = getRandomNumber (MAX_VALUE);
    		System.out.println("Kelvin, Inches, Stones");
    		while (guess != number) {
    			System.out.println("\nEnter a Fahrenheit temperature between 0 and " + "MAX_VALUE" + ": ");
     
    		entry = input.next ();
    		if(validInput(entry)) {
    			count++;
    			guess = Integer.parseInt(entry); }
    			else if (guess <= number) {
    					System.out.println("The entered value is invalid");
    			}
     
     
    		//Conversions
    		double celsius = (CS) * (fahrenheit1 - 32);
    		double meters = (ME) * (feet1);
    		double kilograms = (KL) * (pounds1);
    		double Kelvin = (fahrenheit1 + 459.67)* 5/9;
    		double inches = (feet1 * 12);
    		double stones = (pounds1 * 0.0714285714);
     
    		//Display results
    		System.out.println("Fahrenheit "  +  fahrenheit1 +  "is"  + celsius +  " in Celsius"  +  Kelvin  + " in Kelvin ");
    		System.out.println("Feet " + feet1 + "is" + meters + "in Meters" + "is" + inches + "in Inches");
    		System.out.println("Pounds " + pounds1 + "is" + kilograms + "in Kilograms" + "is" + stones + "in Stones");
     
     
     
     
    		}
     
    	}
     
    	private static boolean validInput(String entry) {
    		// TODO Auto-generated method stub
    		boolean isValid = false;
    		return isValid = false;
    	}
     
     
     
    	private static int getRandomNumber(int MAX_VALUE) {
    		// TODO Auto-generated method stub
    		return 0; }

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Need some help please

    When posting code, please use the highlight tags to preserve formatting.

    What exactly is the problem? You've posted your requirements and your code, but where are you stuck? What does your code do so far? Does it compile? Does it throw an error?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Need some help please

    My code runs and compiles I just need it to say whether or not the value entered is in range and it will convert the entry or that it is out of range or invalid. If that makes sense.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Need some help please

    That makes sense, but what's your question? Where exactly are you stuck? What are you confused about?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Need some help please

    The entered value is invalid
    Fahrenheit 212.0is100.0 in Celsius373.15000000000003 in Kelvin
    Feet 100.0is30.48in Metersis1200.0in Inches
    Pounds 100.0is45.36in Kilogramsis7.14285714in Stones

    those are my results. No matter what I put in it still comes up with those exact results. It asks for a value in each fahrenheit, foot, and pound. My question is how do i fix this? How do I code it to where I can enter any value between 0 and the max value (212) and it runs the program and doesnt say the entered value is invalid unless it actually is. Sorry Im really new to this and even confused a little on how to word the question at times.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Need some help please

    It's a bit hard to read your code as you haven't added the highlight tags, but what is your validInput() function returning?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help please

    How do I add the highlight tags on the forums and maybe that could help? I believe if I'm answering this right, return isValid = false.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Need some help please

    There's a link in my signature that explains how to add the tags, or you can quote other posts that use them to see how to do it.

    That is what is on your return line, but what exactly does that mean? First off, remember that = is not the same as ==. But perhaps more importantly, what do you want to be returning from that function?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Member
    Join Date
    Feb 2013
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help please

    Enter a Fahrenheit temperature (integer) [0-212] : 212
    Enter a distance in feet (integer) [0-100] : 100
    Enter a weight in pounds (integer) [0-100] : 100
    Fahrenheit 212.0is100.0 in Celsius373.15000000000003 in Kelvin
    Feet 100.0is30.48in Metersis1200.0in Inches
    Pounds 100.0is45.36in Kilogramsis7.14285714in Stones

    Enter a fahrenheit temp (integer) [0-212] : 213
    The entered temp value is out of range [0-212]

    I need my program to do that.
    I fixed the quote up there too.
    Sorry if this is confusing you, im lost on how to fix it.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Need some help please

    You're going to have to fill in the validInput() method. Right now what it's doing doesn't make a whole lot of sense. You might even need to add parameters to it or create more functions, if you want to check the validity of several values.

    I recommend you take a step back and only worry about one conversion for now. Don't try to do everything at once. How do you convert Fahrenheit to celsius or kelvin? What are the valid values?

    Recommended reading: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Member
    Join Date
    Feb 2013
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help please

    Ok i'll read that and see what I can do. Thanks.

    --- Update ---

    Im still not getting anywhere. Everything I change either tells me to delete it or it gives me the same damn answer of any value i put in..

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Need some help please

    You should probably post your updated code along with any compiler or runtime errors you're getting.

    To be honest, it looks like you're mixing code from a couple different assignments. I would recommend against doing that, as it's just confusing the issues you're having. I would really advise that you start over with a smaller program that simply converts Fahrenheit to Celsius. Then add bounds checking. Only when all of that is working, then add the other conversions.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!