Results 1 to 2 of 2
  1. #1
    Yoana is offline Member
    Join Date
    Oct 2013
    Rep Power

    Default consecutive numbers!

    I need to find if the number entered by the user are consecutive or not!
    Ex:this should return a true!! This should return false!
    (1,2,3) (3,5,7)
    (3,2,4) (1,2,2)
    (-10,-8,-9) (7,7,9)

    My program seems to work ok when i enter number in order like 1,2,3 = true , and all the numbers for false seem to be working as well! my problem is when i enter number like 3,2,4 that are not in order but still are consecutive!! can someone please help me!!! i though that another if statement would be the solution but i have tray several different ones and still can't make it work !!!

    Java Code:
    import java.util.*;
    public class Consecutive{
    	public static void main (String [] args){
    		Scanner console= new Scanner(;
    		System.out.println("Enter three numbers");
    		String numbers = console.nextLine();
    System.out.println( "The numbers (" + numbers
             + ") is '" + consecutive( numbers ) + "'" );
    }//end of main
    		private static boolean consecutive(String str) {
    		         char c = str.charAt(0);
    				    for (int cc=1; cc<str.length(); cc++)
    				        if ( (c+1) != str.charAt(cc) )
    				            return false ;
        return true;
    		    }//end of consecutive method
    	}//end of classs
    Last edited by Yoana; 02-21-2014 at 10:48 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: consecutive numbers!

    So what you want to know is if a consecutive list can be formed by re-arranging the numbers. Sorting the list might help. Other possibilities would be using either a BitSet, BigInteger (using bits) or a regular Set with some appropriate supporting logic.

    Last edited by jim829; 02-22-2014 at 02:30 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. JPopupMenu Consecutive Clicks
    By PrinceSendai in forum AWT / Swing
    Replies: 2
    Last Post: 08-15-2012, 05:20 PM
  2. four consecutive characters
    By aortell24 in forum New To Java
    Replies: 3
    Last Post: 06-19-2012, 02:32 AM
  3. Consecutive null valors identifier - Suggestions
    By Bonfy in forum New To Java
    Replies: 4
    Last Post: 01-23-2012, 05:50 PM
  4. Replies: 23
    Last Post: 07-04-2011, 02:26 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts