Results 1 to 9 of 9
  1. #1
    DaveMc is offline Member
    Join Date
    Dec 2012
    Location
    Kerry, Ireland
    Posts
    8
    Rep Power
    0

    Default Character Input Problem

    Hey all,
    I am working on this project and I am stuck on the part where the user enters in "V" or "E" to determine on which company they are using. I get stuck inside the while loop no matter what character I enter in, correct or not. ANy one have any ideas?


    Java Code:
    import java.util.Scanner;
    
    public class CallRates {
    
    	public static void main(String[] args) {
    		
    		Scanner input = new Scanner(System.in);
    		
    		String userInput;
    		char userInputChar;
    		int usaVartec, australiaVartec, russiaVartec;
    		int usaEircom, australiaEircom, russiaEircom;  
    		final int USA_VARTEC_RATE = 6, RUSSIA_VARTEC_RATE = 24, AUSTRALIA_VARTEC_RATE = 8;  // vartec constant rates
    		final int USA_EIRCOM_RATE = 19, RUSSIA_EIRCOM_RATE = 92, AUSTRALIA_EIRCOM_RATE = 85;
    		int totalMinsVartec;
    		int totalMinsEircom;
    		int averageCostVartec;
    		int averageCostEircom;
    		int totalCostUSA, totalCostRussia, totalCostAustralia;
    		
    		
    		do
    		{
    			System.out.println("Which service do you use: V/E or Q to quit");   // prompts the user for a choice
    			userInput = input.nextLine();     // gets the input
    			userInputChar = userInput.charAt(0);
    			
    			while(userInputChar != 'E' || userInputChar != 'V')   // Test to see if the user actually entered in E or V, if not it repeats the input process
    			{
    				System.out.println("Invalid choice choose again\n\n");
    				System.out.println("Which service do you use: V/E or Q to quit");   
    				userInput = input.nextLine();                            
    			}
    			
    			if(userInput == "V")  // Vartec calculations
    			{
    				// takes in the number of mins for each country
    				System.out.println("Enter the total number of mins calling USA: ");
    				usaVartec = input.nextInt();
    				System.out.println("Enter the total number of mins calling Australia: ");
    				australiaVartec = input.nextInt();
    				System.out.println("Enter the total number of mins calling Russia: ");
    				russiaVartec = input.nextInt();
    				
    				totalCostAustralia = australiaVartec*AUSTRALIA_VARTEC_RATE;
    				totalCostRussia = russiaVartec*RUSSIA_VARTEC_RATE;
    				totalCostUSA = usaVartec*USA_VARTEC_RATE;
    				
    				totalMinsVartec = totalCostAustralia+totalCostRussia+totalCostUSA;
    				averageCostVartec = totalMinsVartec/3;
    				
    				System.out.println("The total cost of your calls to each country using Vartec is: " + totalMinsVartec);
    				System.out.println("The total average cost of your calls was: " + averageCostVartec);			
    			}
    			else   // obviously the Eircom calculations
    			{
    				System.out.println("Enter the total number of mins calling USA: ");
    				usaEircom = input.nextInt();
    				System.out.println("Enter the total number of mins calling Australia: ");
    				australiaEircom = input.nextInt();
    				System.out.println("Enter the total number of mins calling Russia: ");
    				russiaEircom = input.nextInt();
    				
    				totalCostAustralia = australiaEircom*AUSTRALIA_EIRCOM_RATE;
    				totalCostRussia = russiaEircom*RUSSIA_EIRCOM_RATE;
    				totalCostUSA = usaEircom*USA_EIRCOM_RATE;
    				
    				totalMinsEircom = totalCostAustralia+totalCostRussia+totalCostUSA;
    				averageCostEircom = totalMinsEircom/3;
    				
    				System.out.println("The total cost of your calls to each country using Eircom is: " + totalMinsEircom);
    				System.out.println("The total average cost of your calls was: " + averageCostEircom);				
    			}
    			
    		}while(userInputChar != 'Q');   // Test to see if the user entered q, if so the do-while loop finishes!
    		
    			System.out.println("Thank you for using the service");  // executes when the loop finishes
    
    	}
    
    }
    Last edited by DaveMc; 10-18-2013 at 04:13 PM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: Character Input Problem

    A couple things are wrong. When you enter the while loop, you read in userInput but your while loop uses userInputChar. So
    once you enter the loop you will be there forever since userInputChar never changes. You also need to change the || to &&. Otherwise, the
    condition will always be true.

    Later on you check for equality to "V" using ==. You need to change the double quotes to single quotes.

    Regards,
    Jim
    Last edited by jim829; 10-18-2013 at 04:20 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Character Input Problem

    Java Code:
    while(userInputChar != 'Q');
    Don't compare Strings (or Objects in general) using ==.
    Always use the equals() method.

    ETA: Oh poo...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    DaveMc is offline Member
    Join Date
    Dec 2012
    Location
    Kerry, Ireland
    Posts
    8
    Rep Power
    0

    Default Re: Character Input Problem

    Thanks guys! btt embarrassing on my part.

    Regards!

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: Character Input Problem

    Also, you have a slight logic/control problem. Once inside while loop, you don't check for Q yet your prompt offers it as an option and the user
    could have typed it in at the very beginning but they would still enter the loop.

    Let's say you fix that. So now the first, outer do/while loop runs. If they enter Q you still execute the else conditional when the entered input is not
    V. Is that really what you want to do?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Character Input Problem

    Here's a problem:

    Java Code:
    while(userInputChar != 'E' || userInputChar != 'V')
    The char will always be not 'E' or not 'V'. What you probably mean is this:

    Java Code:
    while(userInputChar != 'E' && userInputChar != 'V')
    Get in the habit of using standard Java naming conventions!

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Character Input Problem

    Hah!
    Typical...I picked the wrong if statement as well...:)
    Should have been the:
    Java Code:
    if(userInput == "V")
    and not one of the ones involving chars.
    Clearly Friday was not a good day...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Character Input Problem

    EDIT: Crud I'm terribly misreading that last post. If you don't see the history of the thread it seems like you condone comparing objects using == ;)
    Last edited by gimbal2; 10-21-2013 at 11:47 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Character Input Problem

    That's what I'm here for...sowing confusion.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 09-29-2013, 12:26 PM
  2. What is end of input character?
    By justme in forum New To Java
    Replies: 1
    Last Post: 01-28-2013, 02:41 PM
  3. Replies: 2
    Last Post: 08-21-2010, 10:53 AM
  4. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  5. Problem with '/' character in HTML and JSP
    By Marcus in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-04-2007, 05:32 AM

Posting Permissions

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