Forums Register Login

specific message upon name

+Pie Number of slices to send: Send
I'm trying to write a program, so when a user enters their name a specific message is displayed. However, it's been a while since I used Java and eclipse, so I'm not sure what I'm doing wrong, or what I need to do.

+Pie Number of slices to send: Send
Welcome to the Ranch

There is something very wrong about your booleans; that on line 9 can only be true false if somebody clicked the cancel button on your option pane, and that on line 11 can never be false. Why re you using option pane? That is very old‑fashioned style.
You need to tell us what is going wrong and what you expected to happen. What does the show message dialogue method return? Can you assign that to a String?

[Edit]Sorry, got a true when I should have said false.
+Pie Number of slices to send: Send
Campbell, thanks for replying. I am getting the error message "Type mismatch: cannot convert from void to String" for lines 12 and 14. As for line 9, eclipse prompted me to change to a Boolean from what I originally had, which was String n1=s1. I didn't know that option pane was old-fashioned.
+Pie Number of slices to send: Send
As for what I expect to happen, I just want to set up a way that I can have a user enter their name, and based on their name, have a particular message appear that I made to correspond to their name.
+Pie Number of slices to send: Send
 

Wally Harmz wrote:I am getting the error message "Type mismatch: cannot convert from void to String" for lines 12 and 14.



The JOptionPane.showMessageDialog returns void not String so you don't need to assign it to anything.

Edit: From the original post's code

The JOptionPane.showInputDialog returns the String which is the user's input (the name).

Then you can check this entered name against "tasha maca" and act accordingly. The n1 and s2 variables are practically pointless.

Also use the "equals" method to check strings
+Pie Number of slices to send: Send
Thanks K. Tsang. Now it runs, but when I type in her name, it comes up please try again. It seems to not be running through the if, but goes right to the else.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
You didn't use equals method to compare strings as K. Tsang suggested.
That's the problem.
+Pie Number of slices to send: Send
Thanks Pawel. Now if I want to enter multiple messages for multiple names, What should I do? Should I use a switch or something else?
+Pie Number of slices to send: Send
You can use a switch for that.
Or maybe you'll consider using a map?
+Pie Number of slices to send: Send
OK, so I was told that message boxes are out. So I used a different input, but it's not running right? No matter who's name I put in, it skips to the part about Star Wars and doesn't run anything else. Can someone see what I'm doing wrong?

+Pie Number of slices to send: Send
The code you posted doesn't compile. "default" needs a ":" after it and there are two missing close braces. After fixing that, it seems to work, but...

There are several ways this code can be improved. One is using correct indentation. Another is getting the code out of the main() method (see MainIsAPain).
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 430 times.
Similar Threads
Assigning to Doubles
Unexpected output
Convert numbers to words
sorting an array
arrays
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:01:10.