Welcome to Dream.In.Code
Become a Java Expert!

Join 149,757 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,482 people online right now. Registration is fast and FREE... Join Now!




Input Validation - While Loops

 
Reply to this topicStart new topic

Input Validation - While Loops

Lady Athena
13 Feb, 2007 - 07:02 AM
Post #1

New D.I.C Head
*

Joined: 29 Jan, 2007
Posts: 25


My Contributions
I'm pretty sure it's going to be something simple but I can't figure it out. I need to finish the loop but I don't know how. I need the data entered by the user to be all positive non-zero numbers and for an error to show up if they do enter something below or equal to zero. Then I need the user to be prompted again until they enter a correct integer to get the final product, which is a sentence telling them the data they entered as the boiling point and the freezing point. Thanks in advance.

CODE
import java.io.*;
public class FreezeBoil2
{
  public static void main(String[]args) throws Throwable
  {
    BufferedReader br;
    String Element;
    String line;
    br=new BufferedReader(new InputStreamReader(System.in));
    double FREEZE_PT;
    double BOIL_PT;
    double avgTemp;
    
    System.out.println();
    System.out.print("Enter Element:");
    Element=br.readLine();

    System.out.print("Enter FREEZE_PT:");
    line=br.readLine();
    FREEZE_PT=Double.parseDouble(line);
    if(FREEZE_PT <= 0){    
    System.out.println("Error: Only positive non-zero integers");
    }

    System.out.print("Enter BOIL_PT:");
    line=br.readLine();
    BOIL_PT=Double.parseDouble(line);
    if(BOIL_PT <= 0){
    System.out.println("Error: Only positive non-zero integers");
    }
    
    System.out.print(Element + " freezes at " + FREEZE_PT);
    System.out.println(" and boils at " + BOIL_PT + " degrees.");

    avgTemp=FREEZE_PT+BOIL_PT;
    avgTemp=avgTemp/2.0;
    System.out.println("Halfway between is "+avgTemp+" degrees.");
  }
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Input Validation - While Loops
13 Feb, 2007 - 07:07 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
you mention that you need to finish a loop, but you don't actually have any loops present in your code.

For each section you are prompting the user for a number, you'll need to perform the check an re prompt on error - something like:
CODE

System.out.print("Enter BOIL_PT:");
line=br.readLine();
BOIL_PT=Double.parseDouble(line);
while(BOIL_PT <= 0){
    System.out.println("Error: Only positive non-zero integers");
   System.out.print("Enter BOIL_PT:");
   line=br.readLine();
   BOIL_PT=Double.parseDouble(line);
}


User is online!Profile CardPM
+Quote Post

Lady Athena
RE: Input Validation - While Loops
13 Feb, 2007 - 07:13 AM
Post #3

New D.I.C Head
*

Joined: 29 Jan, 2007
Posts: 25


My Contributions
Oh man sleep.gif you're right. I got it now. Thanks so much =)
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 06:06AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month