• 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

comparing a values with user input

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I know this is probably an easy one but....
I have to get a char from the user and then (in the same method) depending on what the user enter decides what the payment option will be but when I goto compile I get an error that says "found java.lang.string required a char." and it has a carrot under the null soooo what am I not understanding here?
thank you
valarie
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ March 17, 2004: Message edited by: Dirk Schreckmann ]
 
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find it's best to start with the JavaDoc on these things. JOptionPane.showDialogInput(...) returns a String, not a char.
you could either change the type of userPaymentSelection to String (and then get the first character with charAt(). Alternatively, use charAt immediately:

Incidentally, you can omit the first parameter (null) to showInputDialog, as the method is overloaded.
Hope this helps.

-Tim
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char userPaymentSelection;
userPaymentSelection = JOptionPane.showInputDialog...
userPaymentSelection = (char) System.in.read();

So, userPaymentSelection is first declared as type char.
Then, you tried to assign a String value to it. (Note that JOptionPane.showInputDialog returns a String).
Is that problem becoming clearer? (Hint: You cannot assign a String value to a char type variable.)
After that problem is resolved, I'm quite sure why you would bother to get some user input with JOptionPane.showInputDialog, and then immediately overwrite that input with something new from System.in.read(). Were you perhaps thinking to use a different variable to capture the JOptionPane.showInputDialog result?
 
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
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);
}
[ March 18, 2004: Message edited by: Valarie Brandt ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic