Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Please help with this... I think I have a minor coding error

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    18
    My Mood
    Nerdy
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Please help with this... I think I have a minor coding error

    So this little program I made is supposed to ask you to enter a number.. it can be any random number that ends with 4. For this example lets use 1234.
    Then it prompts you to enter the second number... This one has to be exactly 4.

    What it does is it takes your first input and uses the modulus operator to find the remainder of 1234 which is 4.
    Then it takes the remainder(4) and compares it with your second input(4)...
    If the two match then it should display the message dialog box that says "you are good to go".

    Now this is where my problem is.. it does not display any message dialog box like it should (this gives me the impression that the IF statement is false, but it can't be false... I think).
    Please help me get through this little bit. Thanks in advance!

    import javax.swing.JOptionPane;
    import javax.swing.JFrame;
     
    public class deksels
    {
    public static void main(String[] args) throws Exception
    {
     
    //Declaration of strings
    String inputOne;
    String inputTwo;
     
    inputOne=JOptionPane.showInputDialog("Please enter the first number: ");
    inputTwo=JOptionPane.showInputDialog("Please verify the second number: ");
     
    int copyOne=Integer.parseInt(inputOne);//this converts the first input string into an integer
    int done= copyOne%10;//this is the modulus to retrieve the remainder of the first input
    String doneAgain=Integer.toString(done);//this converts the first input back from integer to string
     
     
    //I believe the following code is where my problem is but i just cant figure out where i messed up
     
    if(doneAgain+inputTwo=="44")//this is the if condition, assuming your first input was 1234 and your second input is 4
    {
    JOptionPane.showMessageDialog(null, "You are good to go!", "number game",JOptionPane.INFORMATION_MESSAGE);//if the statement 
     
    is true then a message dialog is displayed
    }
    {
    System.exit(0);//exits the system
    }
    }
    }
    Last edited by Leprechaun_hunter; April 12th, 2011 at 07:23 AM.


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Posts
    18
    My Mood
    Nerdy
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Please help with this... I think I have a minor coding error

    I'm sorry... it is not supposed to match the first input with the second one. It is supposed to put the two together which should give you 44... then matches it with the string 44... if they match then a message dialog box should appear, else it should just exit automatically...

    Soorry

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,256
    My Mood
    Hungover
    Thanks
    142
    Thanked 618 Times in 529 Posts

    Default Re: Please help with this... I think I have a minor coding error

    String is an Object. Don't use == to compare Objects. Use .equals() instead. There's an article on that somewhere around here, let me see if I can find it...
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Leprechaun_hunter (April 12th, 2011)

  5. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Please help with this... I think I have a minor coding error

    Quote Originally Posted by KevinWorkman View Post
    String is an Object. Don't use == to compare Objects. Use .equals() instead. There's an article on that somewhere around here, let me see if I can find it...
    Common Java Mistakes
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  6. The Following User Says Thank You to JavaPF For This Useful Post:

    Leprechaun_hunter (April 12th, 2011)

  7. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,256
    My Mood
    Hungover
    Thanks
    142
    Thanked 618 Times in 529 Posts

    Default Re: Please help with this... I think I have a minor coding error

    Heh. Beat me to it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #6
    Junior Member
    Join Date
    Apr 2011
    Posts
    18
    My Mood
    Nerdy
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Please help with this... I think I have a minor coding error

    Ohhh... hehe. Thanks a million you guys now I get it.. I have been using "==" and "="... She's working perfectly!

Similar Threads

  1. Coding error
    By chemy G in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 2nd, 2010, 08:36 AM
  2. NEED SERIOUS HELP CODING THIS PROGRAMMING ASAP
    By remy27 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 20th, 2010, 03:03 PM
  3. Help me in java coding
    By smallmac in forum Java Theory & Questions
    Replies: 5
    Last Post: August 2nd, 2010, 09:50 AM
  4. Java coding help
    By Javalover1 in forum The Cafe
    Replies: 0
    Last Post: April 12th, 2010, 08:11 PM
  5. Sudoku coding help...please
    By sketch_flygirl in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 5th, 2009, 09:07 PM