Results 1 to 16 of 16
  1. #1
    tiffany1985 is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default HELP! While loop for input validation

    Hi everyone,

    I want to create a class that converts currency. I stored 2 conversion rate as constant (final) variables. In the main method, I ask the user to enter the amount in US dollars that they want to convert. Then I ask the user to enter the currencyType ("Bitcoin" or "Chuck E Cheese").

    However I am stuck in the while loop:

    I want to use a while loop that basically does this: If the user entered anything else ( not Bitcoin or Chuck E Chesse), then tell the user to try again, and keep reading Strings until they enter a correct currency type. If the user 3 consecutive incorrect currency types, then exit the program.

    Can anyone help me? Take a look at what I have so far.. when I run the program it does not quite do what I want it to do Also, there's something wrong with one of my if statements and I want to figure out how to fix it.

    Thanks in advance

    Java Code:
    import java.util.Scanner;
    
    public class CurrencyConverter
    {
    	 public static void main(String[] args)
    	{
    		 System.out.println("----------------------------------");
    		 System.out.println("\tCurrency Converter");
    	        System.out.println("----------------------------------");
    		 
    	         //Store conversion rate as constant (final) variables
    		 final double BITCOIN = 0.0015;
    		 final int TOKEN = 10;
    	
    		//Collect User Input 
    		Scanner keyboard = new Scanner(System.in);
    		
    		double bitcoinConversion;
    		int tokenConversion;
    		
    		System.out.print("How much money in US dollars do you have? ");//store variable
    		double money = keyboard.nextDouble();
    		
    		System.out.println("Do you want to convert to 'Bitcoin' or 'Chuck E Cheese' tokens? ");
    		String currency = keyboard.next();
    		
    		
    		//Determine the type of currency to convert 
    		if (currency.equalsIgnoreCase("bitcoin"))
    		{
    			bitcoinConversion = (double) (money * BITCOIN);
    			System.out.println("$" + money + " US Dollars = " + bitcoinConversion + " bitcoins.");
    		}
    		
    		if (currency.equalsIgnoreCase("chuck e cheese"));
    		{
    			tokenConversion = (int) (money * TOKEN);
    			System.out.println("$" + money + " US Dollars = " + tokenConversion + " Chuck E Cheese tokens.");
    		}
    		
    		//Using a regular while loop. If else logic for exiting
    		
    		while (currency != "bitcoin" && currency != "chuck e cheese" )
    		{
    			System.out.println("Sorry, but " + currency + " is not a valid currency type. Try again: ");
    			currency = keyboard.next();	
    		}
    		
    	 }
    }
    This is how I want my project to look like:

    HELP! While loop for input validation-screen-shot-2014-02-25-16.10.42.png
    Last edited by tiffany1985; 02-26-2014 at 02:13 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: HELP! While loop for input validation

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: HELP! While loop for input validation

    One problem is your while loop:

    Java Code:
       while (currency != "bitcoin" || currency != "chuck e cheese" )
                {
                    System.out.println("Sorry, but " + currency + " is not a valid currency type. Try again: ");
                    currency = keyboard.next();
                }
    If the user enters 'bitcoin', your loop will continue to run, because this condition holds true:

    currency != "chuck e cheese"

    Remember, with the OR, if either of the 2 statements is true, the entire statement evaluates to true.
    Think about that. Almost there.

    Also, your logic for what to do if the user enters bitcoin or chuck e cheese is not in the right place.
    If they enter something other than chuck e cheese or bitcoin, your code jumps straight to the while loop.
    So you need to rearrange your logic. At the point in your code where you KNOW that you have either bitcoin or chuck e cheese,
    then you can process that input accordingly.

    Does that help?

  4. #4
    tiffany1985 is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Re: HELP! While loop for input validation

    Thank you for your help! I changed OR for AND in the while loop.
    Sorry to bother you but I still don't get what's wrong with the IF statements. Do I need to add an ELSE?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: HELP! While loop for input validation

    what's wrong with the IF statements
    One thing when testing one variable for a match against many other variables when only one if will be true, then you use if/else if statements. When one of the if statements is true, then none of the others will be tested. There should be an else at the end of the chain of if/else if statements to catch the case where none of the preceding if statements were true. That else should show an error message saying something unexpected happened.
    Last edited by Norm; 02-26-2014 at 02:29 AM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    tiffany1985 is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Re: HELP! While loop for input validation

    There should be an else at the end of the chain of if/else if statements to catch the case where none of the preceding if statements were true.
    From what I understand, if I write the following code, it should work but when I run the compiler and type "bitcoin," it still executes the "chuck e cheese" statement
    Java Code:
    if (currency.equalsIgnoreCase("bitcoin"))
    	{
    		bitcoinConversion = (double) (money * BITCOIN);
    		System.out.println("$" + money + " US Dollars = " + bitcoinConversion + " bitcoins.");
    	}
    		
    else if (currency.equalsIgnoreCase("chuck e cheese"));
    	{
    		tokenConversion = (int) (money * TOKEN);
    		System.out.println("$" + money + " US Dollars = " + tokenConversion + " Chuck e Cheese tokens.");
    	}

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: HELP! While loop for input validation

    Please copy the full contents of the command prompt window from when you execute the code and paste it here.

    For debugging, add a println() statement just before the first if statement that prints out the value of currency so you can see what the computer sees when it executes the if statement.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    tiffany1985 is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Re: HELP! While loop for input validation

    This is what I get when I run my program:

    HELP! While loop for input validation-screen-shot-2014-02-25-18.15.20.png

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: HELP! While loop for input validation

    Can you post the new version of the code that produced that output?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    tiffany1985 is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Re: HELP! While loop for input validation

    This is it:

    Java Code:
    import java.util.Scanner;
    
    public class CurrencyConverter
    {
    	 public static void main(String[] args)
    	{
    		 System.out.println("----------------------------------");
    		 System.out.println("\tCurrency Converter");
    	     System.out.println("----------------------------------");
    		 
    	     //Store conversion rate as constant (final) variables
    		 final double BITCOIN = 0.0015;
    		 final int TOKEN = 10;
    	
    		//Collect User Input 
    		Scanner keyboard = new Scanner(System.in);
    		
    		double bitcoinConversion;
    		int tokenConversion;
    		
    		System.out.print("How much money in US dollars do you have? ");//store variable
    		double money = keyboard.nextDouble();
    		
    		System.out.println("Do you want to convert to 'Bitcoin' or 'Chuck E Cheese' tokens? ");
    		String currency = keyboard.next();
    		
    		
    		//Determine the type of currency to convert 
    		if (currency.equalsIgnoreCase("bitcoin"))
    		{
    			bitcoinConversion = (double) (money * BITCOIN);
    			System.out.println("$" + money + " US Dollars = " + bitcoinConversion + " bitcoins.");
    		}
    		
    		else if (currency.equalsIgnoreCase("chuck e cheese"));
    		{
    			tokenConversion = (int) (money * TOKEN);
    			System.out.println("$" + money + " US Dollars = " + tokenConversion + " Chuck e Cheese tokens.");
    		}
    		
    		//Using a regular while loop. If else logic for exiting
    		
    		while (currency != "bitcoin" && currency != "chuck e cheese" )
    		{
    			System.out.println("Sorry, but " + currency + " is not a valid currency type. Try again: ");
    			currency = keyboard.next();	
    			
    			
    		}
    		
    	 }
    }

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: HELP! While loop for input validation

    Where is the println() I asked you to add in post#7 so you could see what the computer sees?
    And where is the else I suggested you to add in post#5?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    tiffany1985 is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Re: HELP! While loop for input validation

    Thank you Thank you Norm!

    Ok so I have fixed some details and it seems to be working the way I want it to work. Now my question is.. What should I put inside the IF statement in line 50 so that it the program exits once the user enters an invalid currency type? I've been reading and re-reading my book chapter on while loops and can't find how to do this with strings :(

    Thanks!


    Here's the code:

    Java Code:
    import java.util.Scanner;
    
    public class CurrencyConverter
    {
    	 public static void main(String[] args)
    	{
    		 System.out.println("----------------------------------");
    		 System.out.println("\tCurrency Converter");
    	     System.out.println("----------------------------------");
    			
    	     //Conversion rates as constant final variables 
    	     final double BITCOIN = 0.0015;
    	     final int CHUCK_E_CHEESE_TOKENS = 10;
    	     
    	     //Get data from the user
    	     Scanner keyboard = new Scanner(System.in);
    	     
    	     System.out.println("How much money in US dollars do you want to convert? ");
    	     double userMoney = keyboard.nextDouble();
    	     //System.out.println(userMoney);
    	     
    	     System.out.println("Do you want to convert to 'Bitcoin' or Chuck e Cheese 'tokens'?");
    	     String userCurrency = keyboard.next();
    	     //System.out.println(userCurrency);
    	  
    	     
    	     
    	     //Determine the type of currency to convert
    	     if (userCurrency.equalsIgnoreCase("BITCOIN") )
    	     {
    	    	 double bitcoinConvert = userMoney * BITCOIN;
    	    	 System.out.println("$" + userMoney + " US Dollars = " + bitcoinConvert + " bitcoins");
    	     }
    	     else if (userCurrency.equalsIgnoreCase("TOKENS"))
    	     {
    	    	 int chuckeConvert = (int) (userMoney * CHUCK_E_CHEESE_TOKENS);
    	    	 System.out.println("$ " + userMoney + " US Dollars = " + chuckeConvert + " Chuck E Cheese tokens");
    	     }
    			
    	     while ( !userCurrency.equalsIgnoreCase("BITCOIN") && !userCurrency.equalsIgnoreCase("TOKENS") )
    	     {
    	    	 System.out.println("Sorry, but " + userCurrency + " is not a valid currency type. Try again: ");
    	    	 userCurrency = keyboard.next();
    	    	
    	    	 
    	    	 /*
    	    	 if (user enters 3 consecutive incorrect currency types)
    	    	 {
    	    		 System.exit(0);
    	    	 }*/
    	    	
    	     }
    	}
    		
    }
    Last edited by tiffany1985; 02-26-2014 at 09:23 PM.

  13. #13
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: HELP! While loop for input validation

    You're probably going to need a retry counter, and if the retry counter reaches 3, exit.

  14. #14
    tiffany1985 is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Re: HELP! While loop for input validation

    What is a retry counter?

  15. #15
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: HELP! While loop for input validation

    What I meant was just an int that you declare (you can call it anything you want, but it represents a retry counter). When the user enters something other than the 2 valid entries, you increment that int. You can then use that int value to determine if the user has reached their maximum number of tries.

  16. #16
    tiffany1985 is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Re: HELP! While loop for input validation

    That makes sense. Thank you sehudson. I ended up using a for loop :)

Similar Threads

  1. Need help with input validation, loop logic
    By mwr1976 in forum New To Java
    Replies: 1
    Last Post: 02-06-2012, 11:07 AM
  2. Input Validation Error
    By donewithmytime in forum Advanced Java
    Replies: 1
    Last Post: 01-29-2012, 03:02 PM
  3. Input validation help
    By AdamG in forum New To Java
    Replies: 3
    Last Post: 10-17-2011, 10:29 PM
  4. Input Validation Help
    By Spyderpig in forum New To Java
    Replies: 3
    Last Post: 02-18-2011, 12:58 PM
  5. Input Validation
    By kickflipper1087 in forum New To Java
    Replies: 5
    Last Post: 11-03-2008, 06:47 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •