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 5 of 5

Thread: JOptionPane

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JOptionPane

    Hello everyone!

    I'm in school and just starting to handle java and I'm having an issue with some code.

    package ticketnumber;
    import java.util.Scanner;
    import javax.swing.JOptionPane;

    public class TicketNumber {

    public static void main(String[] args)
    {

    Scanner in = new Scanner(System.in);
    int num, num2, remainder1, remainder2;
    Boolean result;

    // JOptionPane.showMessageDialog(null, "Messagehere", "Title here", JOptionPane.PLAIN_MESSAGE);

    System.out.print("Enter your ticket number: ");
    num = in.nextInt();


    num2 = num/10;
    remainder1 = num%10;
    remainder2 = num2%7;
    result = (remainder1 == remainder2);

    System.out.println(num+" "+num2+" "+remainder1+" "+remainder2+" "+result);

    JOptionPane.showMessageDialog (
    null, result, "Ticket Validation", JOptionPane.PLAIN_MESSAGE);
    System.exit ( 0 );

    }

    }

    If you uncomment the first JOptionPane the code works correctly. With the comment in place the program simply hangs I'm assuming expecting more input? Any help at all would be appreciated.

    -Thanks


  2. #2
    Junior Member GeorgeEpicGen's Avatar
    Join Date
    Aug 2014
    Location
    North Wales, United Kingdom
    Posts
    3
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane

    Is there an error on line 23 where you have

    result = (remainder1 == remainder2);
    because, as far as I know, you are saying

    result is remainder1 equals remainder2

    which makes no sense.

    Am I missing something? Hope I helped.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 832 Times in 772 Posts
    Blog Entries
    5

    Default Re: JOptionPane

    Welcome to the forums. Please read the Forum Guidelines for useful information, including how to wrap your code in the code tags to preserve formatting.

    Quote Originally Posted by Kazeth
    With the comment in place the program simply hangs I'm assuming expecting more input? Any help at all would be appreciated.
    I would recommend that you avoid mixing command line (Scanner/System.out.println) and Swing (JOptionPane) when attempting to get user input and provide output to the user. This being said your code expects input from the command line (num = in.nextInt()) - and will not proceed until it receives some.


    Quote Originally Posted by GeorgeEpicGen
    Is there an error on line 23 where you have
    That syntax is perfectly fine for the compiler - result is a boolean, and 'remainder1 == remainder2' is a comparison returning a boolean data type

  4. #4
    Junior Member GeorgeEpicGen's Avatar
    Join Date
    Aug 2014
    Location
    North Wales, United Kingdom
    Posts
    3
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane

    That syntax is perfectly fine for the compiler - result is a boolean, and 'remainder1 == remainder2' is a comparison returning a boolean data type
    Aaaaaaaahhh... Okay. I have never been taught that, but it looks like a tidier, quicker version of something I can do.

    Thanks for clearing that up for me, hopefully I can find somewhere to try this.

    -George
    "All matter is merely energy condensed to a slow vibration, that it is all one consciousness experiencing itself subjectively. There is no such thing as death, life is just a dream, and we are all the imagination of ourselves."
    - Bill Hicks

  5. #5
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane

    Quote Originally Posted by copeg View Post
    Welcome to the forums. Please read the Forum Guidelines for useful information, including how to wrap your code in the code tags to preserve formatting.



    I would recommend that you avoid mixing command line (Scanner/System.out.println) and Swing (JOptionPane) when attempting to get user input and provide output to the user. This being said your code expects input from the command line (num = in.nextInt()) - and will not proceed until it receives some.




    That syntax is perfectly fine for the compiler - result is a boolean, and 'remainder1 == remainder2' is a comparison returning a boolean data type
    Thank you. This sounds like sound advice I suppose. I'm used to C++ where I've never encountered an issue like this before. Thanks again.

Similar Threads

  1. [SOLVED] JOptionPane
    By colognem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 13th, 2014, 06:45 AM
  2. [SOLVED] JOptionPane and StringBuilder
    By javaStooge in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 6th, 2014, 09:30 PM
  3. need help in JOptionPane
    By Zuckerberg in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2014, 06:08 AM
  4. JOptionPane not working?
    By Idiot_willing_to_learn in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 27th, 2013, 06:22 PM
  5. JOptionPane using If and Else
    By Liuric in forum Member Introductions
    Replies: 7
    Last Post: October 1st, 2010, 12:05 AM