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

Thread: Valid ticket id

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Valid ticket id

    String must be 6 characters, first 3 must be letters, last 3 must be digits

    Here is my code:
    import java.util.Scanner;
     
    public class ValidTicket {
    	public static void main (String[] args){
    		Scanner in = new Scanner (System.in);
    		String id;
    		int c=' ',i;
    		int letterCount=0, digitCount=0;
    		System.out.printf("Enter ticked id\n");
    		id= in.nextLine();
     
    		if (id.length() != 6)
    		{
    			System.out.printf("Invalid ticket id\n"); 
    		}
    		else if (id.length() == 6)
    		{
    			//c= id.charAt(i);
    			for (i=0; i<= id.length() -3; i++)//first 3 characters
    			{		
    				c= id.charAt(i);
    				if (Character.isUpperCase(c) || Character.isLowerCase(c))
    				{
    					letterCount++;
    				}
    			}
    			for(i=0; i> id.length() -3; i++)//last 3 characters
    			{
    				c= id.charAt(i);
    				if (Character.isDigit(c))
    				{
    					digitCount++;			
    				}
    			{		
    		}// end if
     
    	    if (letterCount == 3 && digitCount==3)
    	    {
    	    	System.out.printf("Valid ticket id\n");
    	    }
    	    else 
    	    	System.out.printf("Invalid ticket id\n");
    	    }
     
    }}}

    However when I run my program, it does not work:
    After I enter the string nothing happens.


    Please help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Valid ticket id

    nothing happens
    What does that mean?
    The code just stops executing like it is waiting for user input?
    Or the code executes and the ends without printing out anything?


    One problem I see with the code is its formatting. The {s and pairing }s should be in columns. Having three }}} on the last line makes it very hard to see the nesting logic of the statements.
    Fix the formatting to see if the statements are properly nested.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Valid ticket id

    Even when i format the nesting same problem: The code just stops executing after entering user input

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Valid ticket id

    The code just stops executing after entering user input
    Does the program exit normally
    or does it stop executing before it exits?

    Please post the newly formatted code.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Valid ticket id

    import java.util.Scanner;
     
    public class ValidTicket {
    	public static void main (String[] args){
    		Scanner in = new Scanner (System.in);
    		String id;
    		int c=' ',i;
    		int letterCount=0, digitCount=0;
    		System.out.printf("Enter ticked id\n");
    		id= in.nextLine();
     
    		if (id.length() != 6)
    		{
    			System.out.printf("Invalid ticket id\n"); 
    		}
    		else if (id.length() == 6)
    		{
    			//c= id.charAt(i);
    			for (i=0; i<= id.length() -3; i++)//first 3 characters
    			{		
    				c= id.charAt(i);
    				if (Character.isUpperCase(c) || Character.isLowerCase(c))
    				{
    					letterCount++;
    				}
    			}
    			for(i=0; i> id.length() -3; i++)//last 3 characters
    			{
    				c= id.charAt(i);
    				if (Character.isDigit(c))
    				{
    					digitCount++;			
    				}
    			{		
    		}// end if
     
    	    if (letterCount == 3 && digitCount==3)
    	    {
    	    	System.out.printf("Valid ticket id\n");
    	    }
    	    else 
    	    	System.out.printf("Invalid ticket id\n");
    	    }
     
    }
    }
    }

    Only the last 3 brackets are changed, nothing else is required in the fomatting and yesssss it stops executing before it exits

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Valid ticket id

    The posted code has three }s one above the other which hides the nesting logic. Can you fix those so each } is in the same column as the { it pairs with?

    Make sure ALL the other {s and }s are properly aligned.

    --- Update ---

    it exits
    Ok, that means that the print statements in the code did not print anything because they were not executed.
    When the code is properly formatted you should see some of the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Valid Ticket

    Hi, a valid ticket is one in which has a total of 6 characters. The first 3 characters are letters and the last 3 are numbers/digits.
    The program must determine whether or not a ticket is valid.


    Here is my solution:
    public class ValidTicket {
    	public static void main (String[] args){
    		Scanner in = new Scanner (System.in);
    		String id;
    		int c=' ',i;
    		int letterCount=0, digitCount=0;
    		System.out.printf("Enter ticked id\n");
    		id= in.nextLine();
     
    		if (id.length() != 6)
    		{
    			System.out.printf("Invalid ticket id\n"); 
    		}
    		else if (id.length() == 6)
    		{
     
    			for (i=0; i< id.length() -3; i++)//first 3 characters
    			{		
    				c= id.charAt(i);
    				if (Character.isUpperCase(c) || Character.isLowerCase(c))
    				{
    					letterCount++;
    				}
    			}
    			for(i=0; i> id.length() -3; i++)//last 3 characters
    			{
    				c= id.charAt(i);
    				if (Character.isDigit(c))
    				{
    					digitCount++;			
    				}
    			{		
    		}// end if
     
    	    if (letterCount == 3 && digitCount==3)
    	    {
    	    	System.out.printf("Valid ticket id\n");
    	    }
    	    else 
    	    	System.out.printf("Invalid ticket id\n");
    	    }
     
       }
     }
    }

    My program run, but produced no calculation as to whether the ticket was valid or not

    Here is my output screen:
    Enter ticked id
    skt111

    After i entered the ticket id nothing was processed, please assist
    Last edited by Java girl; April 22nd, 2014 at 10:46 PM.

  8. #8
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Valid Ticket

    You should look up regular expressions. It will help you immensely with this.
    Specifically, the matches method of the string class: matches(java.lang.String)
    Regular Expressions: Pattern (Java Platform SE 7)

    A regular expressions you could use:
    "abc123".matches("^[a-zA-Z]{3}[0-9]{3}$")

    If you need clarification on anything feel free to ask

  9. The Following User Says Thank You to Parranoia For This Useful Post:

    Java girl (April 24th, 2014)

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Valid Ticket

    One problem I see is the formatting of the code. The {s and }s are not properly placed making it hard to see the nesting logic.
    If you format the code, you might see the problem with the logic.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Valid Ticket

    Norm is exactly right. There is a problem with the incorrect use of braces, which would become obvious once you format/indent your code properly.

    Besides that, there is also a problem with the second for loop:
    for(i=0; i> id.length() -3; i++)//last 3 characters
    To see the problem just add a println() after the for statement, e.g.,
    for(i=0; i> id.length() -3; i++)//last 3 characters
    {
        System.out.println("DEBUG: 2nd for loop");
        ...
    Execute the program. If you provide it with a 6-character ticket ID, you should see the debug line being printed out (but will not - something you'll need to fix.)

    Btw, if this is for your own exercise (i.e., not an assignment), the regular expression approach suggested by Parranoia can help make your code a lot more compact.

  12. The Following User Says Thank You to jashburn For This Useful Post:

    Java girl (April 24th, 2014)

  13. #11
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Valid ticket id

    There is also another problem in the second for loop
    for (i = 0; i > id.length() - 3; i++)
    even correcting it
    for (i = 0; i < id.length() - 3; i++)
    You are still cycling through the first three characters rather than the last three

  14. #12
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Valid ticket id

    Thanks everyone.

  15. #13
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Valid ticket id

    Quote Originally Posted by Java girl View Post
    Thanks everyone.
    I gone through it and found that its compiling fine. But while executing showing just "Invalid ticket number". Let me go again through it.

Similar Threads

  1. HAI. CAN ANYONE HELP ME WITH THIS TICKET SYSTEM PLEASE?
    By fynaz in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 10th, 2014, 02:19 AM
  2. java code to create ticket
    By prabhat11 in forum Algorithms & Recursion
    Replies: 8
    Last Post: April 19th, 2013, 12:04 PM
  3. Parking Ticket Simulator Help (Almost done, few errors)
    By TitanVex in forum Object Oriented Programming
    Replies: 7
    Last Post: December 5th, 2011, 05:06 PM
  4. lotto ticket generator help
    By tlckl3m3elmo in forum What's Wrong With My Code?
    Replies: 19
    Last Post: July 15th, 2011, 12:39 AM