You could do the computations after getting the input
and validating it.
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.
You could do the computations after getting the input
and validating it.
If you don't understand my answer, don't ignore it, ask a question.
import java.util.*; public class InputValidation { public static void main(String[] args) { Scanner inputScanner = new Scanner(System.in); do { System.out.println("Enter a Fahrenheit temperature (integer): "); try { value = inputScanner.nextInt(); if (value<0 || value>212) { System.out.println("The entered value is out of range [0-212]"); } } catch (Exception e) { System.out.println("This entered value is invalid."); inputScanner.next(); } } while (value<0 || value>212); } public static int value=-1, fTemp, fDist, pWeight;//Declare variables public static double cTemp, mDist, kWeight, K, I,S, CS = (5.0 / 9), ME = 0.3048, KL = 0.4536;//Declare constants and doubles public static double fahrenheitToCelsiusToKelvin(double fahrenheit) { return cTemp = fTemp * CS;} { System.out.println(fTemp + " fahrenheit " + cTemp + " celsius " + K + " Kelvin "); } }
Do I do something like this or how would I put it after validating the fahrenheit temp along with the 2 other conversions i need done?
Is it possible for me to put all three conversions after the loop?
Make the list of steps for the program in pseudo code before coding the program.Do I do something like this or how would I put it after
When you have the list of steps, then write the code.
Don't write code before you have gotten the steps figured out.
That is a very POOR coding technique. Don't hide }s at the end of statements.return cTemp = fTemp * CS;} //<<<<< NOTE HIDDEN }
}s should be on a line by themselves
If you don't understand my answer, don't ignore it, ask a question.
import java.util.*; public class InputValidation { public static void main(String[] args) { int value=-1, fTemp, fDist, pWeight;//Declare variables double cTemp, mDist, kWeight, K, I,S; //Declare variables final double CS = (5.0 / 9), ME = 0.3048, KL = 0.4536;//Declare constants Scanner inputScanner = new Scanner(System.in); do { System.out.println("Enter a Fahrenheit temperature (integer): "); try { value = inputScanner.nextInt(); if (value<0 || value>212) { System.out.println("The entered value is out of range [0-212]"); } } catch (Exception e) { System.out.println("This entered value is invalid."); inputScanner.next(); } } while (value<0 || value>212); } }
This is what I would like to stick with, could you give me a hint other then pseudo code as to where to enter my computations and display report so they work? Or with this loop am i way off track?
From post#19:where to enter my computations and display report
Here is some pseudo code:
get input from user
validate input
use input in computations <<<<<<<<<< NOTE this comes after validate input
print out results <<<<<<<<< NOTE this comes after doing computations
If you don't understand my answer, don't ignore it, ask a question.
I understand that..I meant does it go before the } while, or would it go after the } while. I appreciate your help greatly I just am stupid at this as it is so new to me.
The while loop is for the validate step. The computation step follows the validation step.
If the computation step requires a loop, it would be a new loop, not part of the validation loop.
If you don't understand my answer, don't ignore it, ask a question.
Ok so i guess what your saying is in order to put this;
//Perform Conversions
cTemp = fTemp * CS;
mDist = fDist * ME;
kWeight = pWeight * KL;
K = (fTemp + 459.67) * 5/9;
I = fDist * 12;
S = pWeight * 0.0714285714;
//Display Report
System.out.println(fTemp + " fahrenheit " + cTemp + " celsius " + K + " Kelvin ");
System.out.println(fDist + " feet " + mDist + " meters " + I + " Inches ");
System.out.println(pWeight + " pounds " + kWeight + " kilograms " + S + "Stones ");
AFTER my loop/validation, I would need to create another loop to execute the above? Which im guessing would mean from another class or could I just make two loops on this page?
I don't see what you need a loop for.need to create another loop
I'm done for tonight. Back tomorrow.
If you don't understand my answer, don't ignore it, ask a question.