• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

specific message upon name

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 69823
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wally Harmz
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wally Harmz
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 3648
16
Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Wally Harmz
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wally Harmz
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Bartender
Posts: 2230
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't use equals method to compare strings as K. Tsang suggested.
That's the problem.
 
Wally Harmz
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paweł Baczyński
Bartender
Posts: 2230
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a switch for that.
Or maybe you'll consider using a map?
 
Wally Harmz
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
    Bookmark Topic Watch Topic
  • New Topic