• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

changing a String to a char

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to have the user enter A, B,C to decide their payment option and then I need to (in the same method) write code that says if the user selects a then the number of payments is 1 if b then number of payments 2 if c number of payments is 20 and pass that number to a method to be used for a calculation. but i get the error that == cannot be applied to java.lang.String, char soo how do i change a string to a char-----I know that to changea String to a double you use double.parsedouble.......is there a simialr thing for that?
public void getNumberOfPayments(Lawn lawnQuote)throws Exception
{
String userPaymentSelection;
int numberOfPayment;
userPaymentSelection = JOptionPane.showInputDialog(null,
"\n\n\tChoose the number of payments for your lawn service:"
+ "\n\tEnter A for one payment "
+ "\n\tEnter B for two payments "
+ "\n\tEnter C for three payments "
+ "\n\n\t Enter Selection (A, B, C): ");
if (userPaymentSelection == 'A' || userPaymentSelection == 'a')
{
numberOfPayment = 1;
}
else if (userPaymentSelection == 'B' || userPaymentSelection == 'b')
{
numberOfPayment = 2;
}
else if (userPaymentSelection == 'C' || userPaymentSelection == 'c')
{
numberOfPayment = 20;
}
lawnQuote.setNumberOfPayment(numberOfPayment);
}
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char ch = userPaymentSelection.charAt(0); // convert string to char
HTH
 
Valarie Brandt
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the charAt mean
I am familiar changing a sting to a char would be similar to changing a string to a double
ex: double numLength = Double.parseDouble(userLength) I thought it would be similar......
thank you
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
charAt(int index) is a method of String class,
it return the char at indexd position.
here, chararter at index zero is what you needed.
reply
    Bookmark Topic Watch Topic
  • New Topic