• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

while loop

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im doing a program that will allow the users to enter the data input units (BTU, calorie, or joule) and give an output that is the energy in joules.

But if the user types anything other than options 1, 2, 3, or 4, the program should print an error message, and prompt the user for more input.

here is my codes so far:

My problem is i cant get the code work whenver the user enter more than option 4 it's not going back to the loop so the user can enter again, the screen just exits out..

import java.util.*;

public class project02{
//main():application entry point


public static void main(String[] args) {

//define constants
final double Bjoules = 1056; //equivalent of 1 joules to BTU
final double Cjoules = 4184; //equivalent of 1 joules to calories
final double Jjoules = 1; //equivalent of 1 joules to joules



//set up input stream

Scanner stdin = new Scanner(System.in);



//display user input

System.out.print("Enter a number between 1 to 4 and hit enter: : ");
double inputNumber = stdin.nextDouble();

while (inputNumber <=4) {
inoutNumber = stdin.nextDouble();
}

if(inputNumber == 1) {

System.out.print("Enter the Number of BTU: ");
double BTU = stdin.nextDouble();
BTU= BTU * Bjoules;
System.out.println("BTU" + " = " + BTU + " no. of joules" );
}
else if (inputNumber == 2) {

System.out.print("Enter the Number of calories: ");
double Calories = stdin.nextDouble();
Calories = Calories * Cjoules;
System.out.println("Calories "+ " = " + Calories + " no. of joules");
}
else if (inputNumber == 3) {


System.out.print("Enter the Number of Joules: ");
double Joules = stdin.nextDouble();
Joules = Joules * Jjoules;
System.out.println("Joules" + " = " + Joules + " no. of joules");

}
else if (inputNumber == 4){

System.out.println("Exit");
}
else {

System.out.println("Error");
System.out.println("Please enter a number between 1 to 4 and hit enter: : ");
}

}
}


thanks much...
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's this code doing?
 
jc abrigos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guess this is my while statement so the loop will go again when a user enters a values that is more than 4...but i know its not gonna work...how do i put in the while statement?
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start with a simple natural language description of what you want to do, perhaps like this:

I'll read a command, and
while the command isn't a request to stop,
I'll respond to it,
and then read another one
and so on ...
 
jc abrigos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah your right..
the program im doing is like that..

first the user needs to choose options 1 to 4 then if the user chooses any other options besides 1 to 4 the program will loop asking the user to enter another option because the option the user enters is invalid.

My codes in not working properly, i think my while statement is not in a proper place bec. when the user chooses other option like let say 5, the program will just says "error" and it will close it. I want my program to conitue asking the user until the user finally enters the right option..

thanks much..
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, you made it like that


and there is a logic error, if you want the user to keep inputting until he has inputted it correctly then

will make user keep inputting a double until they inputted a value that is above 4.0 (which is what you DONT want)

should be something like this


and i wonder why you don't use switch, and why you use double for the input number, because this can cause user to be able to input a double value.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Toya pointed out, you need to understand what values are NOT allowed. It looks like 1, 2, 3, 4 are allowed, so what ISN'T allowed? Notice 1.5, and 3.14 are not allowed, so this implies that "stdin.nextDouble();" should probably be "stdin.nextInt();" instead. Once you do that, how can you describe the numbers that are NOT between 1 and 4. If you are familiar with interval notation from algebra, it might help to understand the logic used here. Also an understanding of boolean logic will help.

I hope this helps in addition to Toya's comments above. Please come back with more questions if you are still stuck.

Layne
 
jc abrigos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello thanks guys but im still having problems. Now it wont calculate and it wont read the if then else statement.

public static void main(String[] args) {

//define constants
final double Bjoules = 1056; //equivalent of 1 joules to BTU
final double Cjoules = 4184; //equivalent of 1 joules to calories
final double Jjoules = 1; //equivalent of 1 joules to joules



//set up input stream

Scanner stdin = new Scanner(System.in);



//display user input

//System.out.print("Enter a number between 1 to 4 and hit enter: : ");
//double inputNumber = stdin.nextDouble();

//while (inputNumber <=4) {
//inputNumber = stdin.nextDouble();
//}
//else {

System.out.print("Enter a number between 1 and 4 : ");
double inputNumber = stdin.nextInt();

while (inputNumber < 1 || inputNumber > 4){



if(inputNumber == 1) {

System.out.print("Enter the Number of BTU: ");
double BTU = stdin.nextInt();
BTU= BTU * Bjoules;
System.out.println("BTU" + " = " + BTU + " no. of joules" );
}
else if (inputNumber == 2) {

System.out.print("Enter the Number of calories: ");
double Calories = stdin.nextInt();
Calories = Calories * Cjoules;
System.out.println("Calories "+ " = " + Calories + " no. of joules");
}
else if (inputNumber == 3) {


System.out.print("Enter the Number of Joules: ");
double Joules = stdin.nextInt();
Joules = Joules * Jjoules;
System.out.println("Joules" + " = " + Joules + " no. of joules");

}
else if (inputNumber == 4){

System.out.println("Exit");
}


}
}}
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it looks the the conditions for your while loop is not correct ...
you only enter the loop if your input is less than 1 or greater than 4

it should be greater than or equal to 1 and less than or equal to 4
[ October 17, 2005: Message edited by: Marco Davids II ]
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want to exit your program if the user enters the number 4, therefore that is all that your while loop should check for (inputNumber != 4). All other validation should be done in the while loop - check for 1, 2 or 3 and do the appropriate thing if they are entered, otherwise print an error message. You will also need to put the code that asks the user to enter a number inside your while loop otherwise if will loop indefinitely on the first value entered and you also need to change the type of inputNumber to an int.

Make those changes and if you still have problems post your new code without all the commented out parts and using code tags (click the code button on the reply page). Both of these make it easier to read your code.
 
jc abrigos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what ive done so far im still gettting an error reading the while loop..help..thanks much ..


[ October 17, 2005: Message edited by: jc abrigos ]
 
Marshal
Posts: 80138
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really want your inputNumber to be a double when you are calling nextInt()? You can get away with it because of automatic promotion (sometime called an implicit cast).
Actually you don't want to use floating point numbers with == operators if you can possibly avoid it; what will happen if your input is calculated as
3.00000000000000000000001 or 2.99999999999999999999 rather than 3?

You need to work out about a repetition. What starts the repetition off? What changes in the repeated loop, and what would stop it.
See whether you can work it out before it prints out the millionth time.

And remember there are three kinds of repetition (if you include for(Type element : myArray), 3 1/2 types of repetition . One sort of repetition is particularly suitable for an input which you will want to repeat, either until it gives an "exit" input, or after repeated attempts, until you get a "correct" input.
In which case you might do well to enlarge your loop to include the multiple choices at the end. There might be a better way to run your choices, too.

Try that and see whether it works better.

CR
 
Kenneth Albertson
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's good to work things out for yourself, and you learn a lot that way, but you shouldn't have to re-invent the wheel every day. Several people have given good suggestions, but you have not understood them, so it is time for some clearer instructions.

The structure of the program that you are trying to write is a very basic and important pattern that you will use over and over again. Your teacher should have shown you an example. If he or she didn't, they aren't doing a very good job. If they did, but you didn't study it, then you aren't doing a very good job.

Here is the code that you need to get your command menu working. Remember that this is very basic, and the program will crash and burn if you enter anything other than an integer. But it will do for a beginner.What should you do next? Study this code carefully. Make sure that you understand every line of it. Run it over and over again, with different inputs, and different sequences of inputs. Make small changes to it, and run it again, to confirm that the changes have the effect that you expect.

ONLY once you are completely sure that you understand what this code does, and how it works, should you start to replace the "Responding to option" blocks. Do these ONE AT A TIME. Make sure that each one works correctly before you start the next one.

This is a very important way of working that should also have been demonstrated to you (same comments as before). You SHOULD NOT write your programs all at once. You should always start with the smallest and simplest possible version of what you want to do (like the control menu), and make sure that is working correctly before continuing. Then you add a little more, and test it again. If it doesn't work, you know that the error must be in the 4 lines that you just added.

Now you try this code and tell us where you get to.
 
jc abrigos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi kym, thanks for your help..Thanks you for explaining it to me real good...I fig it out..thanks for all you helped me..
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic