Welcome to Dream.In.Code
Become a Java Expert!

Join 150,170 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,282 people online right now. Registration is fast and FREE... Join Now!




Having some trouble with variables :\

 
Reply to this topicStart new topic

Having some trouble with variables :\

Sucklefish
23 Aug, 2008 - 01:45 PM
Post #1

New D.I.C Head
*

Joined: 23 Aug, 2008
Posts: 6

Basically what I am trying to do is have a user enter either a Y or an N and return an output depending on what they answered.

This is what I have so far.

CODE

import javax.swing.*;

public class fish

{
    public static void main(String[] args)
    {
    string input, y, n;

    do
    {
        input = JOptionPane.showInputDialog("Do you like Pie? Either type Y or N");
        if (input == "Y") System.out.println("I like pie too!");
        if (input == "N") System.out.println("Well pie doesn't like you either.");
    }
    }
}


the main questions are:
1. Is string the right thing to be using?
2. Is the input == "Y" syntax correct

any help would be greatly appriciated

thanks

sucklefish
User is offlineProfile CardPM
+Quote Post

nick2price
RE: Having Some Trouble With Variables :\
23 Aug, 2008 - 02:17 PM
Post #2

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
seeing that its a char being entered, you could use a char variable. However i dont really see much of a problem using a string for a program this size. When you compare a string, you shouldnt be using ==. Strings should be compared using .equals
exp
CODE
if(input.equals("Y"))


or if you want to make sure that the input y is accepted, you can use .equalsIgnoreCase("y")

you should turn the second output to an else if statement, and then follow this with an else statement to print an error if one of these two inputs are not entered.
User is offlineProfile CardPM
+Quote Post

Sucklefish
RE: Having Some Trouble With Variables :\
23 Aug, 2008 - 05:48 PM
Post #3

New D.I.C Head
*

Joined: 23 Aug, 2008
Posts: 6

Thanks a ton nick, works fine now.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:09AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month