# Character Input Problem

• 10-18-2013, 04:09 PM
DaveMc
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?

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         } }```
• 10-18-2013, 04:16 PM
jim829
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
• 10-18-2013, 04:19 PM
Tolls
Re: Character Input Problem
Code:

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

ETA: Oh poo...
• 10-18-2013, 04:24 PM
DaveMc
Re: Character Input Problem
Thanks guys! btt embarrassing on my part.

Regards!
• 10-18-2013, 04:27 PM
jim829
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
• 10-18-2013, 06:11 PM
kjkrum
Re: Character Input Problem
Here's a problem:

Code:

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

Code:

`while(userInputChar != 'E' && userInputChar != 'V')`
• 10-21-2013, 11:32 AM
Tolls
Re: Character Input Problem
Hah!
Typical...I picked the wrong if statement as well...:)
Should have been the:
Code:

`if(userInput == "V")`
and not one of the ones involving chars.
Clearly Friday was not a good day...
• 10-21-2013, 11:44 AM
gimbal2
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 == ;)
• 10-21-2013, 12:12 PM
Tolls
Re: Character Input Problem
That's what I'm here for...sowing confusion.
:)