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: I dont see why this program is not doing what it is supposed to

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

    Default I dont see why this program is not doing what it is supposed to

    This program is supposed to request you to enter a number... Then requires you to confirm the same number but only this time without the last digit.

    Like for example: You enter number, e.g. 1234567... then it asks you to verify the number (but this time you only enter the first 6 digits of the same number) e.g. 123456.
    Then it will take the last digit of the first entry and put it together with at the end of the second entry so it can give you the full 7 digit number, i.e. 1234567.
    If the numbers match it will tell you the ticket is valid.. if not it will tell you the ticket is invalid:


    import javax.swing.JOptionPane;
    import javax.swing.JFrame;
     
    public class Validation
    {
    public static void main(String[] args) throws Exception
    {
    //Declaration of strings
    String inputOne;
    String inputTwo;
     
    //Declaration of long numbers
    int ticket;
    int confirm;
     
    //Declaration of integers
     
     
    inputOne=JOptionPane.showInputDialog("Please enter the 
     
    ticket number: ");
    inputTwo=JOptionPane.showInputDialog("Please confirm the 
     
    ticket number: ");
     
    ticket=Integer.parseInt(inputOne);
    confirm=Integer.parseInt(inputTwo);
    int remainder = ticket%10;
    if ((confirm + remainder)==ticket)
    {
    JOptionPane.showMessageDialog(null, "The ticket number you 
     
    just entered is valid", "Ticket Validator", 
     
    JOptionPane.INFORMATION_MESSAGE);
    }
    else
    {
    JOptionPane.showMessageDialog(null, "The ticket number you 
     
    just entered is invalid", "Ticket Validator", 
     
    JOptionPane.INFORMATION_MESSAGE);
    }
    {
    System.exit(0);
    }
    }
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: I dont see why this program is not doing what it is supposed to

    Care to tell us what issues you're having? Any exceptions being thrown? etc etc
    More details you provide better response you're likely to get.

    - Assuming you're only having logical issues, what you're doing in that code is actually working yourself away from the solution.
    First things first, using your example. If complete ticket number is 1234567, what your doing is adding, so 123456 + 7 will give you 123463.

    A very simple way of doing this, it to keep the values as Strings, and use Integer.valueOf(String s) method when getting the remainder.
    Then add remainder to the end of your inputTwo String, then compare using .equals instead of == operator.
    Last edited by newbie; April 11th, 2011 at 05:23 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    Leprechaun_hunter (April 11th, 2011)

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

    Default Re: I dont see why this program is not doing what it is supposed to

    Awesome.. thanks.. i tried highlighting every word individually.. but this should work

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: I dont see why this program is not doing what it is supposed to

    if ((confirm + remainder)==ticket)
    Of course this doesn't work. If first number is 12345 and second number is 1234 then 1234 + 5 = 1239 not 12345. It is much easier if you divide the first number by 10 and compare to second number.

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

    Default Re: I dont see why this program is not doing what it is supposed to

    I edited the code and this is what I have:
    import javax.swing.JOptionPane;
    import javax.swing.JFrame;
     
    public class Validation
    {
    public static void main(String[] args) throws Exception
    {
    //Declaration of strings
    String inputOne;
    String inputTwo;
     
    //Declaration of integers
    int ticket;
    int confirm;
     
    inputOne=JOptionPane.showInputDialog("Please enter the ticket number: ");
    inputTwo=JOptionPane.showInputDialog("Please confirm the ticket number: ");
     
     
    ticket=Integer.parseInt(inputOne);
    confirm=Integer.parseInt(inputTwo);
    int remainder=ticket%10;
     
    String newOne=Integer.toString(confirm);
    String newTwo=Integer.toString(remainder);
    String newThree=Integer.toString(ticket);
     
    if ((newOne + newTwo) == newThree)
    {
    JOptionPane.showMessageDialog(null, "The ticket number you just entered is valid", "Ticket 
     
    Validator",JOptionPane.INFORMATION_MESSAGE);
    }
    else
    {
    JOptionPane.showMessageDialog(null, "The ticket number you just entered is invalid", "Ticket 
     
    Validator",JOptionPane.INFORMATION_MESSAGE);
    }
    {
    System.exit(0);
    }
    }
    }

    when it asks me to enter the first number I entered 123456, then it prompted me to enter second number and I entered 12345.
    It still tells me the number is invalid.
    I have a funny feeling that I did something wrong here...

  7. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: I dont see why this program is not doing what it is supposed to

    Yeah, There is no need for newOne.. etc.
    This could easily be done in 15 lines of code.
    Like I said previously, Just use inputOne and inputTwo, no need for ticket and confirm.
    Just when setting up the remainder variable, say Integer.valueOf(inputOne) % 10 and later on compare with the equals method.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. i'm getting this error, dont know why? please help
    By amr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 17th, 2010, 06:14 AM
  2. WHY this code dont work?
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: December 9th, 2010, 10:47 AM
  3. I dont get it....please help
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 30th, 2010, 03:16 AM
  4. Replies: 1
    Last Post: March 15th, 2010, 10:03 PM
  5. Dont laugh at me
    By Neblin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 3rd, 2009, 08:18 AM